网站建设情况说明,网页设计的网网页设计的网站,李沧做网站,电子商务以后的就业方向一、找轮廓 cv2.findContours()
contours,hierarchy cv2.findContours(image*,mode*,method*)
contours#xff1a;找到的所有轮廓数组#xff0c;数组内的元素为轮廓像素点坐标。
hierarchy#xff1a;轮廓间的层次关系。
image#xff1a;二值图像#xff08;cv2.t…一、找轮廓 cv2.findContours()
contours,hierarchy cv2.findContours(image*,mode*,method*)
contours找到的所有轮廓数组数组内的元素为轮廓像素点坐标。
hierarchy轮廓间的层次关系。
image二值图像cv2.threshold()。
mode轮廓检测模式常见方法如下
模式值解释RETR_EXTERNAL0只检测外部轮廓RETR_LIST1检测所有轮廓但不建立层级关系RETR_CCOMP2检测所有轮廓同时建立两个层级关系如果内部还有轮廓则此轮廓与最外层轮廓同级RETR_TREE3检测所有轮廓同时建立一个树状层级关系
method保存轮廓的方法常见方法如下
方法值解释CHAIN_APPROX_NONE1存储所有轮廓点坐标CHAIN_APPROX_SIMPLE2只保存轮廓顶点坐标CHAIN_APPROX_TC89L13使用CHAIN_APPROX_TC89L1 近视算法保存轮廓坐标CHAIN_APPROX_TC89KCOS4使用CHAIN_APPROX_TC89KCOS近视算法保存轮廓坐标
二、绘轮廓 cv2.drawContours()
img cv2.drawContours(image*,contours,contourIdx*,color*,thickness*,lineType*,hierarchy*,maxLevel*,offset*)img目标图像。
image二值图像用于填画上轮廓。
contourscv2.findContours()函数返回的轮廓列表 list。
contourIdx需要绘制的轮廓在轮廓列表中的索引。-1 表示绘制列表中的所有轮廓。
colorBGR颜色。
thickness轮廓粗细-1 表示实心。
lineType线条类型。
hierarchycv2.findContours() 输出的层次关系。
maxLevel轮廓层次关系的深度0表示绘制第0层次关系的轮廓。
offset常数值轮廓偏移量相较于原轮廓坐标
三、检测模式
3.1 外轮廓 RETR_EXTERNAL
import cv2
# 图像前处理
img cv2.imread(contours.png) # 原图
img_gray cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) # GRAY
thresh,img_threshold cv2.threshold(img_gray,150,255,cv2.THRESH_BINARY) # 二值contours,hierarchy cv2.findContours(img_threshold,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
img1 cv2.drawContours(img,contours,-1,(255,0,0),3)cv2.imshow(img,img)
cv2.imshow(img_threshold,img_threshold)
cv2.waitKey(0)
cv2.destroyAllWindows() 3.2 所有轮廓 cv2.RETR_LIST
import cv2
# 图像前处理
img cv2.imread(contours.png) # 原图
img_gray cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) # GRAY
thresh,img_threshold cv2.threshold(img_gray,150,255,cv2.THRESH_BINARY) # 二值contours,hierarchy cv2.findContours(img_threshold,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
img1 cv2.drawContours(img,contours,-1,(255,0,0),2)cv2.imshow(img,img)
cv2.imshow(img_threshold,img_threshold)
cv2.waitKey(0)
cv2.destroyAllWindows() 3.3 RETR_CCOMP
import cv2
import numpy as np# 图像前处理
img cv2.imread(m.png) # 原图
img_gray cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) # GRAY
thresh,img_threshold cv2.threshold(img_gray,150,255,cv2.THRESH_BINARY) # 二值contours,hierarchy cv2.findContours(img_threshold,cv2.RETR_CCOMP,cv2.CHAIN_APPROX_SIMPLE)
print(hierarchy)
img1 cv2.drawContours(img,contours,-1,(255,0,0),2)cv2.imshow(img,img)
cv2.imshow(img_threshold,img_threshold)
cv2.waitKey(0)
cv2.destroyAllWindows() hierarchy详细解释请参考《OpenCV计算机视觉项目实战Python版---p265》
print(hierarchy)结果
[[[ 1 -1 -1 -1][-1 0 2 -1][ 3 -1 -1 1][-1 2 -1 1]]] 3.4 RETR_TREE
import cv2
import numpy as np# 图像前处理
img cv2.imread(m.png) # 原图
img_gray cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) # GRAY
thresh,img_threshold cv2.threshold(img_gray,150,255,cv2.THRESH_BINARY) # 二值contours,hierarchy cv2.findContours(img_threshold,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
print(hierarchy)
img1 cv2.drawContours(img,contours,-1,(255,0,0),2)cv2.imshow(img,img)
cv2.imshow(img_threshold,img_threshold)
cv2.waitKey(0)
cv2.destroyAllWindows() hierarchy详细解释请参考《OpenCV计算机视觉项目实战Python版---p265》
print(hierarchy) 结果
[[[-1 -1 1 -1][ 3 -1 2 0][-1 -1 -1 1][-1 1 -1 0]]] 四、轮廓面积、周长
4.1 面积 cv2.contourArea()
area cv2.contourArea(contour*,oriented*)
area轮廓面积。
countour要计算轮廓。
oriented默认为False换回面积的绝对值。
import cv2
import numpy as np# 图像前处理
img cv2.imread(contours.png) # 原图
img_gray cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) # GRAY
thresh,img_threshold cv2.threshold(img_gray,150,255,cv2.THRESH_BINARY) # 二值contours,hierarchy cv2.findContours(img_threshold,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
img1 cv2.drawContours(img,contours,-1,(255,0,0),2)
areas []
for i in range(len(contours)):area cv2.contourArea(contours[i])areas.append(area)
print(areas)
cv2.waitKey(0)
cv2.destroyAllWindows()
[8500.5, 15986.0, 11396.0, 11560.0, 7136.5]
4.2 面积 cv2.arcLength()
arc cv2.arcLength(contours,closed*)
arc轮廓周长。
countours要计算轮廓。
closedTure表示轮廓是封闭的。
import cv2
import numpy as np# 图像前处理
img cv2.imread(contours.png) # 原图
img_gray cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) # GRAY
thresh,img_threshold cv2.threshold(img_gray,150,255,cv2.THRESH_BINARY) # 二值contours,hierarchy cv2.findContours(img_threshold,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
img1 cv2.drawContours(img,contours,-1,(255,0,0),2)
areas []
for i in range(len(contours)):arc cv2.arcLength(contours[i],closedTrue)areas.append(arc)
print(areas)
cv2.waitKey(0)
cv2.destroyAllWindows()
[437.9482728242874, 492.6173119544983, 696.3086559772491, 403.98989498615265, 558.1147834062576]