OpenCV3学习笔记整理(一期)
Posted 数据轩
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了OpenCV3学习笔记整理(一期)相关的知识,希望对你有一定的参考价值。
一
概述
OpenCV 3是一种先进的计算机视觉库,可以用于各种图像和视频处理操作,通过opencv 3能很容易的实现一些有前景且功能先进的应用(比如:人脸识别或目标跟踪等)。理解与计算机视觉相关的算法、模型以及opencv 3 API背后的基本概念,有助于开发现实世界中的各种应用程序(比如:安全和监视领域的工具)。
二
常用文件操作接口
2.1 静态图片的操作
img = cv2.imread(SrcPicPathName, param1)
SrcPicPath: 图片路径带名字
Param1:read mode,总共有以下几种(一般可省略,默认返回BGR图像)
IMREAD_UNCHANGED = -1,//!< If set, return the loaded image as is (with alpha channel, otherwise it gets cropped).
IMREAD_GRAYSCALE = 0, //!< If set, always convert image to the single channel grayscale image.
IMREAD_COLOR = 1, //!< If set, always convert image to the 3 channel BGR color image.
IMREAD_ANYDEPTH = 2,//!< If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit.
IMREAD_ANYCOLOR = 4, //!< If set, the image is read in any possible color format.
IMREAD_LOAD_GDAL = 8,//!< If set, use the gdal driver for loading the image.
IMREAD_REDUCED_GRAYSCALE_2 = 16, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/2.
IMREAD_REDUCED_COLOR_2 = 17,//!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/2.
IMREAD_REDUCED_GRAYSCALE_4 = 32, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/4.
IMREAD_REDUCED_COLOR_4 = 33, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/4.
IMREAD_REDUCED_GRAYSCALE_8 = 64, //!< If set, always convert image to the single channel grayscale image and the image size reduced 1/8.
IMREAD_REDUCED_COLOR_8 = 65, //!< If set, always convert image to the 3 channel BGR color image and the image size reduced 1/8.
IMREAD_IGNORE_ORIENTATION = 128 //!< If set, do not rotate the image according to EXIF's orientation flag.
cv2.imread最终返回BGR格式的图像,一般的色彩空间是red-green-blue即RGB,但cv2使用的色彩空间却是BGR格式的,字节顺序相反
cv2.imread会删除所有alpha通道的信息(透明度)
img拥有三个属性:(img.share、img.size、img.datatype)
shape:numpy返回包含宽度、高度和通道数(如果图像是彩色的)的数组,这在调试图像类型时很有用;如果图像是单色或灰度的,将不包含通道值
size:该属性是指图像像素的大小。
datatype:该属性会得到图像的数据类型(通常为一个无符号整数类型的变量和该类型占的位数,比如uint8类型)
建议使用numpy数组索引操作方法,如获取感兴趣区域:my_roi = img[0:100, 100:200]
img.item(x, y, ch):
X: 表示img某个像素的x行坐标
Y:表示img某个像素的y列坐标
Ch:表示img的某个通道
img.itemset((x,y,ch), val):
表示将x行y列ch通道的像素值设定为val
img.copy():
对原始图片的拷贝
cv2.imwrite('DstPicNameAndFormat', img):
将读到的或已经处理了的img写入到特定路径并制定格式,如.jpg,.png等等
img = cv2.resize(img,(1280,720),
interpolation=cv2.INTER_AREA):
重新调整图片大小,第一个参数是原始图片,第二个参数是缩放后的图像大小元祖,最后一个参数是插值方式,分别有如下几种插值方式:
CV_INTER_NN --> 最近邻插值,
CV_INTER_LINEAR --> 双线性插值 (缺省使用)
CV_INTER_AREA --> 使用象素关系重采样。当图像缩小时候,该方法可以避免波纹出现。当图像放大时,类似于 CV_INTER_NN 方法..
CV_INTER_CUBIC --> 立方插值.
cv2.imshow(“my img”, img):
显示名为“my img”的img图片
cv2.waitKey():
Waitkey的参数为等待键盘触发的时间,单位为毫秒,其返回值是-1(表示没有键被按下)或ACSII码
总所周知的bug,在一些系统中,waitkey返回的值比ASCII码还要大,可通过如下tip只取低八位来保证只获取ASCII码:
keycode = cv2.waitKey(1)if keycode != -1:
keycode &= 0xFF
Opencv的窗口只有在调用waitKey()函数时才会更新,waitKey()函数只有在opencv窗口成为活动窗口时,才能捕获输入信息。
cv2.destroyAllwindows():
释放所有由opencv创建的所有窗口
cv2.destroyWindows('my window'):
只释放由opencv创建且名为“my window”的窗口
Cv2.namedWindow(’my window‘):
创建一个名为“my window”的窗口,图片可show到这个窗口上
动态视频文件的读写操作
(opencv提供了VideoCapture类和VideoWriter类来支持各种格式的视频文件,其中AVI格式不同系统都是支持的):
videoCapture = cv2.VideoCapture(SrcVideopathandname)SrcVideopathandname:如'MyInputVid.avi',视频的路径和名字
fps = videoCapture.get(cv2.CAP_PROP_FPS) # 获取视频帧率
size = (int(videoCapture.get(cv2.CAP_PROP_FRAME_WIDTH)), int(videoCapture.get(cv2.CAP_PROP_FRAME_HEIGHT))) # 获取视频宽高
2.2 常用的视频属性
CAP_PROP_POS_MSEC =0, //!< Current position of the video file in milliseconds.
CAP_PROP_POS_FRAMES =1, //!< 0-based index of the frame to be decoded/captured next.
CAP_PROP_POS_AVI_RATIO =2, //!< Relative position of the video file: 0=start of the film, 1=end of the film.
CAP_PROP_FRAME_WIDTH =3, //!< Width of the frames in the video stream.
CAP_PROP_FRAME_HEIGHT =4, //!< Height of the frames in the video stream.
CAP_PROP_FPS =5, //!< Frame rate.CAP_PROP_FOURCC =6, //!< 4-character code of codec. see VideoWriter::fourcc .
CAP_PROP_FRAME_COUNT =7, //!< Number of frames in the video file.
CAP_PROP_FORMAT =8, //!< Format of the %Mat objects returned by VideoCapture::retrieve().
CAP_PROP_MODE =9, //!< Backend-specific value indicating the current capture mode.
CAP_PROP_BRIGHTNESS =10, //!< Brightness of the image (only for those cameras that support).
CAP_PROP_CONTRAST =11, //!< Contrast of the image (only for cameras).
CAP_PROP_SATURATION =12, //!< Saturation of the image (only for cameras).
CAP_PROP_HUE =13, //!< Hue of the image (only for cameras).
CAP_PROP_GAIN =14, //!< Gain of the image (only for those cameras that support).
videoWriter = cv2.VideoWriter('MyOutputVid.avi', cv2.
VideoWriter_fourcc('I','4','2','0'), fps, size)
Param1:视频文件名,必须指定
Param2:视频编解码器,必须指定,常用的有:
'I','4','2','0':该选项是一个未压缩的YUV颜色编码,兼容性好,但产生文件较大,文件扩展名为.avi
'P','T','M','I':该选项是MPEG-1编码类型,文件扩展名为.avi
'X','V','T','D':该选项是MPEG-4编码类型,得到的视频大小处于平均值,文件扩展名为.avi
'T','H','E','O':该选项是Ogg Vorbis,文件扩展名为.ogv
'F','L','V','1':该选项是一个flash视频,文件扩展名为.flv
success, frame = videoCapture.read() #根据videoCapture抓取到的图像逐帧写到videoWriter指向的文件内while success: # Loop until there are no more frames.
videoWriter.write(frame)
success, frame = videoCapture.read()
END
数据轩
科技 | 应用 | 趋势
www.jx-lab.com
以上是关于OpenCV3学习笔记整理(一期)的主要内容,如果未能解决你的问题,请参考以下文章
《OpenCV3编程入门》学习笔记三:HighGUI图形用户界面