ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

【CV】import cv2

2022-08-15 15:31:30  阅读:156  来源: 互联网

标签:image CAP cv2 PROP only import CV


记录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

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有