OpenCV轮廓属性

1.长宽比

长宽比是对象边界矩形的宽度与高度的比值。

代码示例:

x,y,w,h = cv.boundingRect(cnt)
aspect_ratio = float(w)/h

2.范围

范围是轮廓区域与边界矩形区域的比值。

代码示例:

area = cv.contourArea(cnt) x,y,w,h = cv.boundingRect(cnt)
rect_area = w*h
extent = float(area)/rect_area

3.坚实度

坚实度是等高线面积与其凸包面积之比。

代码示例:

area = cv.contourArea(cnt)
hull = cv.convexHull(cnt)
hull_area = cv.contourArea(hull)
solidity = float(area)/hull_area

4.等效直径

等效直径是面积与轮廓面积相同的圆的直径。

代码示例:

area = cv.contourArea(cnt)
equi_diameter = np.sqrt(4*area/np.pi)

5.取向

取向是物体指向的角度。

代码示例:

(x,y),(a,b),angle = cv.fitEllipse(cnt)

函数返回值:ellipse = [ (x, y) , (a, b), angle ]

  • (x, y)代表椭圆中心点的位置
  • (a, b)代表长短轴长度,应注意a、b为长短轴的直径,而非半径
  • angle 代表了中心旋转的角度即取向

6.掩码和像素点

主要使用np.transpose()或cv.findNonZero(mask)来获取构成该对象的所有点。Numpy给出的坐标是 (行、列) 格式,而OpenCV给出的坐标是 (x,y) 格
式。

numpy.transpose(a, axes=None)

参数如下:

  • a:输入数组
  • axes:可选参数。如果指定,它必须是一个图或列表,其中包含 [0,1,…,N-1] 的排列,其中N 是轴数。默认是对数组矩阵进行反转。

详见Python numpy.transpose 详解.博主讲的很详细。

cv.findNonZero(src, idx )

参数如下:

  • src:输入图像,Mat类型
  • idx:非零点存放集合

代码示例:

import numpy as np
import cv2 as cv
img = cv.imread('C:\\Users\\dell\\Desktop\\prac files\\prac7.jpg')
img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv.namedWindow("prac", 0)
ret, dst = cv.threshold(img_gray, 127, 255, 0)
contours, hierarchy = cv.findContours(dst, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
cnt = contours[0]
mask = np.zeros(img_gray.shape,np.uint8)
cv.drawContours(mask,[cnt],0,255,-1)
#pixelpoints = np.transpose(np.nonzero(mask))
pixelpoints = cv.findNonZero(mask)
cv.polylines(img, [pixelpoints], True, (0, 0, 255), 5)
cv.imshow('prac',img)
cv.waitKey(0)
cv.destroyAllWindows()

7.最大值、最小值和它们的位置

主要使用cv.minMaxLoc()函数来实现。

cv.minMaxLoc(src, minVal, maxVal, minLoc, maxLoc, mask)

参数如下:

  • src,输入的数组,若是图像,需为单通道图像。
  • minVal,返回最小值的指针。若无需返回,此值设为 NULL。
  • maxVal,返回最大值的指针。若无需返回,此值设为 NULL。
  • minLoc,返回最小值位置的指针(二维情况下)。若无需返回,此值设为 NULL。
  • maxVal,返回最大值位置的指针(二维情况下)。若无需返回,此值设为 NULL。
  • mask,可选的掩膜操作,非零掩码元素用于标记待统计元素,需要与输入图像集有相同尺寸。

代码示例:

min_val, max_val, min_loc, max_loc = cv.minMaxLoc(img_gray, mask = mask)

8.平均颜色或平均强度

主要使用cv.mean()函数来实现。

cv.mean_val = cv.mean(image, mask)

参数如下:

  • image:输入图像
  • mask:掩膜

代码示例:

mean_val = cv.mean(im,mask = mask)

9.极端点

极点是指对象的最顶部,最底部,最右侧和最左侧的点。主要使用argmin()函数和argmax()函数来获得极端点坐标。

代码示例:

leftmost = tuple(cnt[cnt[:,:,0].argmin()][0])
rightmost = tuple(cnt[cnt[:,:,0].argmax()][0])
topmost = tuple(cnt[cnt[:,:,1].argmin()][0])
bottommost = tuple(cnt[cnt[:,:,1].argmax()][0])

10.凸性缺陷

主要使用convexityDefects()函数来实现。

convexityDefects(contour, convexhull, convexityDefects)

参数如下:

  • coutour::输入参数,检测到的轮廓
  • convexhull:输入参数,检测到的凸包
  • convexityDefects:输出参数,函数返回一个数组,存储了起始点(startPoint),结束点(endPoint),距离convexity hull最远点(farPoint)以及最远点到convexity hull的距离(depth),返回的前三个值是cnt的索引

代码示例:

import cv2 as cv
img = cv.imread('C:\\Users\\dell\\Desktop\\prac files\\prac7.jpg')
img_gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
cv.namedWindow("prac", 0)
ret, dst = cv.threshold(img_gray, 127, 255, 0)
contours, hierarchy = cv.findContours(dst, cv.RETR_TREE, cv.CHAIN_APPROX_SIMPLE)
cnt = contours[0]
hull = cv.convexHull(cnt, returnPoints=False)
defects = cv.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])
cv.line(img, start, end, [0, 255, 0], 2)
cv.circle(img, far, 5, [0, 0, 255], -1)
cv.imshow('prac', img)
cv.waitKey(0)
cv.destroyAllWindows()

11.点多边形测试

主要使用cv.pointPolygonTest()

cv.pointPolygonTest(contour, pt, measureDist)

参数如下:

  • contour:输入轮廓
  • point:输入点,tuple类型
  • measureDist:如果为True,则该函数估计从点到最近的轮廓边缘的有符号距离。否则,该功能仅检查该点是否在轮廓内。如果为False,若返回值为+1,表示点在轮廓内部;返回值为-1,表示在轮廓外部;返回值为0,表示在轮廓上

代码示例:

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

12.形状匹配

主要使用cv.matchShapes()函数来实现。

cv.matchShapes(contour1, contour2, method, parameter)

参数如下:

  • contour1:轮廓向量或者灰度图
  • contour2:轮廓向量或者灰度图
  • method:使用的比较方式,具体如下图:
    在这里插入图片描述
  • parameter:设为0

代码示例:

import cv2 as cv
img1 = cv.imread('C:\\Users\\dell\\Desktop\\prac files\\prac7.jpg')
img2 = cv.imread('C:\\Users\\dell\\Desktop\\prac files\\prac8.jpg')
img1_gray = cv.cvtColor(img1, cv.COLOR_BGR2GRAY)
img2_gray = cv.cvtColor(img2, cv.COLOR_BGR2GRAY)
ret, thresh = cv.threshold(img1_gray, 127, 255, 0)
ret, thresh2 = cv.threshold(img2_gray, 127, 255, 0)
contours, hierarchy = cv.findContours(thresh, 2, 1)
cnt1 = contours[0]
contours, hierarchy = cv.findContours(thresh2, 2, 1)
cnt2 = contours[0]
ret = cv.matchShapes(cnt1, cnt2, 1, 0.0)
print(ret)
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值