Opencv-PythonPython中opencv模块cv2一些函数用法及简介

前言

最近在做卡片上的数字识别。调用caffe模块,直接用的mnist模型,不过这篇文章不讲caffe。

需要先对图片进行一系列预处理,把卡片上的数字分离出来,有点OCR的感觉。

我把这次使用到的所有opencv函数简单写一下。

1. 读取视频 cv2.VideoCapture()

参数1:可以是数字,对应摄像头编号。可以是视频名。

如果用的摄像头,下面要用循环来不断读帧。

[python]  view plain  copy
  1. c = cv2.VideoCapture(0)  
  2. while 1:  
  3.     ret, image = c.read()  
  4.     cv2.imshow("Origin", image) # 显示画面  
  5.     cv2.waitKey(1# 一定要搭配这句才出画面  

2. 等待 cv2.waitKey()

参数1:等待时间,单位毫秒。

一般与cv2.imshow()搭配使用

另一个实用的功能就是通过按键进入if条件语句

比如下面按ESC关闭窗口,退出循环,结束程序。

[python]  view plain  copy
  1. c = cv2.VideoCapture(0)  
  2. while 1:  
  3.     ret, image = c.read()  
  4.     cv2.imshow("Origin", image)  
  5.     key = cv2.waitKey(1)  
  6.     if key == 27:  
  7.         cv2.destroyAllWindows()  
  8.         break  

3. 图像加文字cv2.putText()

参数1:图像

参数2:文字内容

参数3:坐标位置

参数4:字体

参数5:字号

参数6:颜色

参数7:字体粗细

[python]  view plain  copy
  1. c = cv2.VideoCapture(0)  
  2. while 1:  
  3.     ret, image = c.read()  
  4.     cv2.putText(image,‘HandsomeHans’,(220,130),cv2.FONT_HERSHEY_SIMPLEX,4,(127,127,255),2)  
  5.     cv2.imshow("Origin", image)  
  6.     key = cv2.waitKey(1)  
  7.     if key == 27:  
  8.         cv2.destroyAllWindows()  
  9.         break  

4. 图像加框 cv2.rectangle()

参数1:图像

参数2:左上角坐标

参数3:右下角坐标

参数4:框的颜色

参数5:框的粗细


5. 提取图像轮廓 cv2.findContours()

参数1:图像

参数2:提取规则。cv2.RETR_EXTERNAL:只找外轮廓,cv2.RETR_TREE:内外轮廓都找。

参数3:输出轮廓内容格式。cv2.CHAIN_APPROX_SIMPLE:输出少量轮廓点。cv2.CHAIN_APPROX_NONE:输出大量轮廓点。

输出参数1:图像

输出参数2:轮廓列表

输出参数3:层级

[python]  view plain  copy
  1. contours_map, contours, hierarchy = cv2.findContours(image,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)  

6. 画出轮廓 cv2.drawContours()

参数1:图像

参数2:轮廓列表

参数3:轮廓索引,如果负数则画出所有轮廓。

参数4:轮廓颜色

参数5:轮廓粗细

[python]  view plain  copy
  1. cv2.drawContours(image,contours,-1,(0,0,255),2)  

7. 判断像素点是否在某一轮廓内 cv2.pointPolygonTest()

参数1:某一轮廓列表

参数2:像素点坐标

参数3:如果为True则输出该像素点到轮廓最近距离。如果为False,则输出为正表示在轮廓内,0为轮廓上,负为轮廓外。

[python]  view plain  copy
  1. result = cv2.pointPolygonTest(biggest, (w,h), False)  

8. 求轮廓面积 cv2.contourArea()

参数1:某一轮廓

[python]  view plain  copy
  1. area = cv2.contourArea(contours[i])  

9. 求包含轮廓的最小方框 cv2.minAreaRect()

参数1:某一轮廓

输出参数1:四个角点坐标和偏移角度

求最小方框并将其画出:

[python]  view plain  copy
  1. rect = cv2.minAreaRect(contours[i])  
  2. box = np.int0(cv2.boxPoints(rect)) # boxPoints()是opencv3的函数  
  3. cv2.drawContours(image,[box],0,(0,255,255),2)  

10. 求包含轮廓的正方框 cv2.boundingRect()

参数1:某一轮廓

[python]  view plain  copy
  1. x, y, w, h = cv2.boundingRect(contours[i])  

11. 图像颜色转换 cv2.cvtColor()

参数1:图像

参数2:转换方式。cv2.COLOR_BGR2GRAY:转换为灰度图。cv2.COLOR_BGR2HSV:转换为HSV颜色空间。

[python]  view plain  copy
  1. gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)  

12. 高斯平滑滤波 cv2.GaussianBlur()

参数1:图像

参数2:滤波器大小

参数3:标准差

[python]  view plain  copy
  1. gray = cv2.GaussianBlur(gray,(3,3),0#模糊图像  

13. 中值滤波 cv2.medianBlur()

参数1:图像

参数2:滤波尺寸

[python]  view plain  copy
  1. gray = cv2.medianBlur(gray,5# 填充白色噪点  

14. 图像二值化 cv2.threshold()

参数1:灰度图像

参数2:阈值

参数3:最大值

参数4:转换方式 cv2.THRESH_BINARY、cv2.THRESH_BINARY_INV、cv2.THRESH_TRUNC、cv2.THRESH_TOZERO、cv2.THRESH_TOZERO_INV

[python]  view plain  copy
  1. ret, thres = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)  

15. 扩充图像 cv2.copyMakeBorder()

参数1:图像

参数2:top扩充长度

参数3:down扩充长度

参数4:left

参数5:right

参数6:边界类型:

BORDER_CONSTANT:常量,增加的变量通通为value色 [value][value] | abcdef | [value][value][value]

BORDER_REFLICATE:直接用边界的颜色填充, aaaaaa | abcdefg | gggg
BORDER_REFLECT:倒映,abcdefg | gfedcbamn | nmabcd
BORDER_REFLECT_101:倒映,和上面类似,但在倒映时,会把边界空开,abcdefg | egfedcbamne | nmabcd
BORDER_WRAP:类似于这种方式abcdf | mmabcdf | mmabcd


参数7:常量数值

[python]  view plain  copy
  1. iimgg = cv2.copyMakeBorder(num_thres,top,down,left,right,cv2.BORDER_CONSTANT,value=0)  

16. 旋转图像 cv2.getRotationMatrix2D()

参数1:旋转中心点

参数2:旋转角度

参数3:缩放大小

输出参数1:旋转矩阵

[python]  view plain  copy
  1. rotateMatrix = cv2.getRotationMatrix2D(center=(thres.shape[1]/2, thres.shape[0]/2), angle = rect[2], scale = 1)  
  2. rotImg = cv2.warpAffine(thres, rotateMatrix, (thres.shape[1], thres.shape[0]))  

猜你喜欢

转载自blog.csdn.net/qq_37674858/article/details/80449129