OpenCV图像处理——轮廓

总目录

图像处理总目录←点击这里

十、轮廓

10.1、轮廓检测

10.1.1、方法原理

cv2.findContours(img,mode,method)

  • mode:轮廓检索模式
    • RETR_EXTERNAL :只检索最外面的轮廓;
    • RETR_LIST:检索所有的轮廓,并将其保存到一条链表当中;
    • RETR_CCOMP:检索所有的轮廓,并将他们组织为两层:顶层是各部分的外部边界,第二层是空洞的边界;
    • RETR_TREE:检索所有的轮廓,并重构嵌套轮廓的整个层次;
  • method:轮廓逼近方法
    • CHAIN_APPROX_NONE:以Freeman链码的方式输出轮廓,所有其他方法输出多边形(顶点的序列)。
    • CHAIN_APPROX_SIMPLE:压缩水平的、垂直的和斜的部分,也就是,函数只保留他们的终点部分
  • 输出两个参数
    • contours 轮廓的信息 ← 第一个参数
    • hierarchy 层级 ← 第二个参数

cv2.drawContours(draw_img, contours, -1, (0, 0, 255), 2)

  • draw_img 图像信息
  • contours 轮廓检测
  • -1 代表所有的轮廓都画,其他数字指定轮廓画
  • (0, 0, 255)代表轮廓颜色
  • 2 代表轮廓线条的宽度
    OpenCV图像处理——轮廓_第1张图片

10.1.2、案例

为了更高的准确率,使用二值图像cv2.THRESH_BINARY,其余l类型详细点击 → 链接

def cv_show(img,name):
    cv2.imshow(name,img)
    cv2.waitKey()
    cv2.destroyAllWindows()

img = cv2.imread('./image/contours.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)

#传入绘制图像,轮廓,轮廓索引,颜色模式,线条厚度
# 注意需要copy,要不原图会变。。。
draw_img = img.copy()
res = cv2.drawContours(draw_img, contours, -1, (0, 0, 255), 2)
cv_show(res,'res')

OpenCV图像处理——轮廓_第2张图片
原图
OpenCV图像处理——轮廓_第3张图片

10.2、轮廓特征

计算面积和周长

cnt = contours[0]
#面积  8500.5
cv2.contourArea(cnt)
#周长,True表示闭合的  437.9482651948929
cv2.arcLength(cnt,True)

其中contours[0]代表下图中的三角形的外边框(contours中的第一个元素)

contours下标代表意义
1 代表三角形内边框
2 代表六边形外边框
3 代表六边形内边框

画出contours[0]

img = cv2.imread('./image/contours.png')
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
cnt = contours[0]
res = cv2.drawContours(draw_img, [cnt], -1, (0, 0, 255), 2)
cv_show(res,'res')

OpenCV图像处理——轮廓_第4张图片

10.3、轮廓近似

10.3.1 原理

cv2.approxPolyDP(cnt,epsilon,True)

  • cnt 轮廓
  • epsilon (一般按周长的百分比 0.1*cv2.arcLength(cnt,True)
    • 百分比越小,越接近原来的图形(0.01)
    • 百分比越大,形状变化越大(0.2已经接近一条直线)
    • 具体见下面案例有说明
      OpenCV图像处理——轮廓_第5张图片

10.3.2 案例

原始轮廓

img = cv2.imread('./image/contours2.png')

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
cnt = contours[0]

draw_img = img.copy()
res = cv2.drawContours(draw_img, [cnt], -1, (0, 0, 255), 2)
cv_show(res,'res')

OpenCV图像处理——轮廓_第6张图片
轮廓近似

epsilon = 0.1*cv2.arcLength(cnt,True) 
approx = cv2.approxPolyDP(cnt,epsilon,True)

draw_img = img.copy()
res = cv2.drawContours(draw_img, [approx], -1, (0, 0, 255), 2)
cv_show(res,'res')

epsilon = 0.1*cv2.arcLength(cnt,True)

OpenCV图像处理——轮廓_第7张图片

epsilon = 0.15*cv2.arcLength(cnt,True)

OpenCV图像处理——轮廓_第8张图片
epsilon = 0.2*cv2.arcLength(cnt,True)

OpenCV图像处理——轮廓_第9张图片

原图
OpenCV图像处理——轮廓_第10张图片

10.3.3、边界矩形

原理方法

x,y,w,h = cv2.boundingRect(cnt)

获得左上角位置(xy)和宽高(wh)

img = cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)

画出矩形框

代码

img = cv2.imread('./image/contours.png')

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(gray, 127, 255, cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
cnt = contours[2]

x,y,w,h = cv2.boundingRect(cnt)
img = cv2.rectangle(img,(x,y),(x+w,y+h),(0,255,0),2)
cv_show(img,'img')

OpenCV图像处理——轮廓_第11张图片

10.3.4、外接圆

(x,y),radius = cv2.minEnclosingCircle(cnt)

获得圆心半斤

cv2.circle(img,center,radius,(0,255,0),2)

画圆

cnt = contours[0]
(x,y),radius = cv2.minEnclosingCircle(cnt)
center = (int(x),int(y))
radius = int(radius)
img = cv2.circle(img,center,radius,(0,255,0),2)
cv_show(img,'img')

OpenCV图像处理——轮廓_第12张图片

你可能感兴趣的:(深度学习,Python,#,OpenCV,opencv,图像处理,计算机视觉)