cv2 读取并展示图片
import cv2
img_path = r''
img = cv2.imread(img_path, cv2.IMREAD_COLOR)
box1 = ['labelname', x_min, y_min, x_max, y_max]
cv2.rectangle(img, (int(box1[1]), int(box1[2])), (int(box1[3]), int(box1[4])), (0,255,0), -1)
cv2.putText(img, str(box1[0]), (box1[1], box1[2]-2 ), cv2.FONT_HERSHEY_SIMPLEX, 0.75, (0, 0, 255), 2)
window_name = 'temp1'
cv2.namedWindow(window_name, 0)
cv2.resizeWindow(window_name, 736, 416)
cv2.imshow(window_name, img)
cv2.waitKey(0)
cv2.destroyAllWindows()
CV2 读取、展示图片,并设置展示窗口的大小
# -*- coding:utf-8 -*-
import os
import cv2
img_root_path = r' ' # 存储图片的根路径
for img_name in os.listdir(img_root_path):
img_path = os.path.join(img_root_path, img_name)
img = cv2.imread(img_path, cv2.IMREAD_UNCHANGED)
print('增强类型: {},\t尺寸: {}'.format(img_name.split('_')[0], img.shape))
### 图片太大,设置显示窗口的尺寸 ###
cv2.namedWindow('show_img', 0) # 定义窗口名称,三个函数(namedWindow、 resizeWindow、 imshow)中窗口名称要一致
cv2.resizeWindow('show_img', 736, 416) # 自己设定窗口图片的大小
### 图片太大,设置显示窗口的尺寸 ###
cv2.imshow("show_img", img)
cv2.waitKey(0) # 等待时间单位 ms ;参数为 0 时 无限制的等待用户的按键事件
cv2.destroyAllWindows()