OpenCV - 轮廓:更多函数(Python实现)

凸缺陷

凸包(Convex Hull)是一个计算几何(图形学)中的概念,它的严格的数学定义为:在一个向量空间V中,对于给定集合X,所有包含X的凸集的交集S被称为X的凸包。
  在图像处理过程中,我们常常需要寻找图像中包围某个物体的凸包。凸包跟多边形逼近很像,只不过它是包围物体最外层的一个凸集,这个凸集是所有能包围这个物体的凸集的交集。
  OpenCV中有一个函数cv2.convexityDefect()可以帮助我们找到凸缺陷:

hull = cv2.convexHull(cnt,returnPoints=False)#这里必须是False
defect = cv2.convexityDefects(cnt,hull)  

它会返回一个数组,每一行的值为[起点,终点,最远的点,到最远点的近似距离]。我们可以在一张图上显示它。我们将起点和终点用一天绿线连接,在最远点画一个圆圈。记住返回的前三个值是点在轮廓中的索引,所以要到轮廓中去找到它们:

import cv2
import numpy as np
img = cv2.imread('bling.png')
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(img_gray, 127, 255,0)
#图片轮廓
_,contours,hierarchy = cv2.findContours(thresh,2,1)
cnt = contours[0]
#寻找并绘制凸包
hull = cv2.convexHull(cnt,returnPoints = False)
defects = cv2.convexityDefects(cnt,hull)
for i in range(defects.shape[0]):
    s,e,f,d = defects[i,0]
    start = tuple(cnt[s][0])
    end = tuple(cnt[e][0])
    far = tuple(cnt[f][0])
    cv2.line(img,start,end,[0,255,0],2)
    cv2.circle(img,far,5,[0,0,255],-1)
cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

在这里插入图片描述

Point Polygon Test

求解图像中的一个点到一个对象轮廓的最短距离。如果点在轮廓的外部,返回值为负。如果在轮廓上,返回值为 0。如果在轮廓内部,返回值为正。

dist = cv2.pointPolygonTest(cnt,(50,50),True)

此函数的第三个参数是 measureDist。如果设置为 True,就会计算最短距离。如果是 False,只会判断这个点与轮廓之间的位置关系(返回值为+1, -1, 0)(速度较计算距离快2-3倍)。

import cv2
import numpy as np
img = cv2.imread('bling.png')
img_gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
ret, thresh = cv2.threshold(img_gray, 127, 255,0)
#图片轮廓
_,contours,hierarchy = cv2.findContours(thresh,2,1)
cnt = contours[0]
img = cv2.drawContours(img, cnt, -1, (0,0,255), 3)#绘制轮廓
#计算并输出到轮廓的距离
dist = cv2.pointPolygonTest(cnt,(150,150),True)
print(dist)
cv2.circle(img,(150,150),5,[0,0,255],-1)#绘制查找点
cv2.circle(img,(150,150),int(abs(dist)),[0,0,255],2)#绘制相切圆
#输出位置关系
dist = cv2.pointPolygonTest(cnt,(150,150),False)
print(dist)

cv2.imshow('img',img)
cv2.waitKey(0)
cv2.destroyAllWindows()

-117.04699910719626
-1.0

在这里插入图片描述

形状匹配

函数 cv2.matchShape() 可以帮我们比较两个形状或轮廓的相似度。如果返回值越小,匹配越好。它是根据 Hu 矩来计算的。

import cv2
import numpy as np
#读取图片二值化
img1 = cv2.imread('bling.png',0)
img2 = cv2.imread('bling2.png',0)
img3 = cv2.imread('other.png',0)
ret, thresh  = cv2.threshold(img1, 127, 255,0)
ret, thresh2 = cv2.threshold(img2, 127, 255,0)
ret, thresh3 = cv2.threshold(img3, 127, 255,0)
#查找图像轮廓
_,contours,hierarchy = cv2.findContours(thresh,2,1)
cnt1 = contours[0]
_,contours,hierarchy = cv2.findContours(thresh2,2,1)
cnt2 = contours[0]
_,contours,hierarchy = cv2.findContours(thresh3,2,1)
cnt3 = contours[0]
#比较相似度并输出
ret = cv2.matchShapes(cnt1,cnt1,1,0.0)
print (ret)
ret = cv2.matchShapes(cnt1,cnt2,1,0.0)
print (ret)
ret = cv2.matchShapes(cnt1,cnt3,1,0.0)
print (ret)

0.0
0.09988522892847496
0.2118326622251766

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值