【CV】import cv2
作者:互联网
记录cv2包相关方法的调用:
图像:
img = cv2.imread(imgPath) # 读取图像,输出为3维的numpy
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # cv2.cvtColor()方法用于将图像从一种颜色空间转换为另一种颜色空间
image_resized = cv2.resize(image, target_shape) #调整形状
image_np = image_resized / 255.0 # 归一化到0~1
image_exp = np.expand_dims(image_np, axis=0)
image_transposed = image_exp.transpose((0, 3, 1, 2)) # (b,h,w,c)->(b,c,h,w)
cv2.rectangle(img, pt1, pt2, color, thickness, lineType, shift ) # 参数表示依次为: (图片,长方形框左上角坐标, 长方形框右下角坐标, 字体颜色,字体粗细)
cv2.putText(image, "%s: %.2f" % (id2class[class_id], conf), (xmin + 2, ymin - 2), cv2.FONT_HERSHEY_SIMPLEX, 0.8, color) # 图片,要添加的文字,文字添加到图片上的位置,字体的类型,字体大小,字体颜色,字体粗细
导入Image包实时显示结果:
from PIL import Image
Image.fromarray(image).show()
视频
cap = cv2.VideoCapture(video_path)
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
width = cap.get(cv2.CAP_PROP_FRAME_WIDTH)
fps = cap.get(cv2.CAP_PROP_FPS)
fourcc = cv2.VideoWriter_fourcc(*'XVID') # 用来指定格式的opencv3支持的avi格式有:I420: 未压缩YUV颜色编码,PIMI: MPEG-1编码,XVID: MPEG-4编码
total_frames = cap.get(cv2.CAP_PROP_FRAME_COUNT)
get方法参数按顺序对应下表(从0开始编号
propId –
Property identifier. It can be one of the following:
CV_CAP_PROP_POS_MSEC Current position of the video file in milliseconds or video capture timestamp.
CV_CAP_PROP_POS_FRAMES 0-based index of the frame to be decoded/captured next.
CV_CAP_PROP_POS_AVI_RATIO Relative position of the video file: 0 - start of the film, 1 - end of the film.
CV_CAP_PROP_FRAME_WIDTH Width of the frames in the video stream.
CV_CAP_PROP_FRAME_HEIGHT Height of the frames in the video stream.
CV_CAP_PROP_FPS Frame rate.
CV_CAP_PROP_FOURCC 4-character code of codec.
CV_CAP_PROP_FRAME_COUNT Number of frames in the video file.
CV_CAP_PROP_FORMAT Format of the Mat objects returned by retrieve() .
CV_CAP_PROP_MODE Backend-specific value indicating the current capture mode.
CV_CAP_PROP_BRIGHTNESS Brightness of the image (only for cameras).
CV_CAP_PROP_CONTRAST Contrast of the image (only for cameras).
CV_CAP_PROP_SATURATION Saturation of the image (only for cameras).
CV_CAP_PROP_HUE Hue of the image (only for cameras).
CV_CAP_PROP_GAIN Gain of the image (only for cameras).
CV_CAP_PROP_EXPOSURE Exposure (only for cameras).
CV_CAP_PROP_CONVERT_RGB Boolean flags indicating whether images should be converted to RGB.
CV_CAP_PROP_WHITE_BALANCE_U The U value of the whitebalance setting (note: only supported by DC1394 v 2.x backend currently)
CV_CAP_PROP_WHITE_BALANCE_V The V value of the whitebalance setting (note: only supported by DC1394 v 2.x backend currently)
CV_CAP_PROP_RECTIFICATION Rectification flag for stereo cameras (note: only supported by DC1394 v 2.x backend currently)
CV_CAP_PROP_ISO_SPEED The ISO speed of the camera (note: only supported by DC1394 v 2.x backend currently)
CV_CAP_PROP_BUFFERSIZE Amount of frames stored in internal buffer memory (note: only supported by DC1394 v 2.x backend currently)
ret, frame = cap.read() # 参数ret 为True 或者False,代表有没有读取到图片,第二个参数frame表示截取到一帧的图片
frame = cv2.cvtColor(frame,cv2.COLOR_BGR2RGB) # 图像的颜色空间的转换
cv2.imshow('image', img_raw[:, :, ::-1]) # 打开一个名为“image”的窗口并展示图片
cv2.waitKey(1) #不加这句窗口会一闪就关闭 ,参数为1连续播放,参数为0一张一张手动播放
cv2.destroyAllWindows() # 使用后释放窗口是好习惯
标签:image,CAP,cv2,PROP,only,import,CV 来源: https://www.cnblogs.com/Harukaze/p/16588447.html