A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

内容来自OpenCV-Python Tutorials 自己翻译整理

目标
轮廓的面积、周长、重心、边界
相关函数


图像矩可以计算图像的质心,面积等等。
图像的矩

函数 cv2.moments()会计算图像的矩,并返回一个字典
(findContours应该返回三个参数,样例里的代码只返回两个,报错了)

import cv2
import numpy as np

img = cv2.imread('3.jpg',0)
ret,thresh = cv2.threshold(img,127,255,0)
img,contours,hierarchy = cv2.findContours(thresh, 1, 2)

cnt = contours[0]
M = cv2.moments(cnt)
print(M)
结果

{'m00': 0.0, 'm10': 0.0, 'm01': 0.0, 'm20': 0.0, 'm11': 0.0, 'm02': 0.0, 'm30': 0.0, 'm21': 0.0, 'm12': 0.0, 'm03': 0.0, 'mu20': 0.0, 'mu11': 0.0, 'mu02': 0.0, 'mu30': 0.0, 'mu21': 0.0, 'mu12': 0.0, 'mu03': 0.0, 'nu20': 0.0, 'nu11': 0.0, 'nu02': 0.0, 'nu30': 0.0, 'nu21': 0.0, 'nu12': 0.0, 'nu03': 0.0}
1
(结果居然全是0,轮廓找的不好)

根据这些矩的值可以得到重心
<span class="MathJax" id="MathJax-Element-1-Frame" tabindex="0" data-mathml="Cx=M10M00" role="presentation" style="box-sizing: border-box; outline: 0px; display: inline; line-height: normal; text-align: left; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0px; min-height: 0px; border: 0px; word-break: break-all; position: relative;">Cx=M10M00Cx=M10M00
<span class="MathJax" id="MathJax-Element-2-Frame" tabindex="0" data-mathml="Cy=M01M00" role="presentation" style="box-sizing: border-box; outline: 0px; display: inline; line-height: normal; text-align: left; word-spacing: normal; word-wrap: normal; white-space: nowrap; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0px; min-height: 0px; border: 0px; word-break: break-all; position: relative;">Cy=M01M00Cy=M01M00

cx = int(M['m10']/M['m00'])

cy = int(M['m01']/M['m00'])

1

2

轮廓面积

cv2.contourArea()函数可以计算面积,也可以使用矩 M[‘m00’]


import cv2

import numpy as np


img = cv2.imread('3.jpg',0)

ret,thresh = cv2.threshold(img,127,255,0)

img,contours,hierarchy = cv2.findContours(thresh, 1, 2)


cnt = contours[1]

area = cv2.contourArea(cnt)#计算面积

<span class="MathJax" tabindex="0" data-mathml="Cy=M01M00" role="presentation" style="box-sizing: border-box; outline: 0px; display: inline; line-height: normal; text-align: left; word-wrap: normal; float: none; direction: ltr; max-width: none; max-height: none; min-width: 0px; min-height: 0px; border: 0px; word-break: break-all; position: relative;">

print(area)

轮廓周长


使用 cv2.arcLength()计算,第二个参数表示轮廓是闭合(True)还是打开的


import cv2

import numpy as np


img = cv2.imread('3.jpg',0)

ret,thresh = cv2.threshold(img,127,255,0)

img,contours,hierarchy = cv2.findContours(thresh, 1, 2)


cnt = contours[1]

perimeter = cv2.arcLength(cnt,True)


print(perimeter)

轮廓近似

将得到的轮廓近似为更少点组成的形状,使用 Douglas-Peucker algorithm

例如要在图像中寻找一个矩形,由于种种原因,不能得到一个完整矩形,现在可以使用此函数,函数的第二个参数epsilon是从原始轮廓到近似轮廓的最大距离,epsilon对结果影响很大。


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

approx = cv2.approxPolyDP(cnt,epsilon,True)


凸包

使用cv2.convexHull()来计算凸包,所谓凸包,打个比方,给你一个木板,在木板上面钉一大堆钉子,然后找一根皮筋在上面一围,形成的图形就是凸包。


函数介绍


hull = cv2.convexHull(points[, hull[, clockwise[, returnPoints]]


参数


points为传入轮廓

hull为输出,通常不需要

clockwise设置为真顺时针输出凸包

returnPoints默认为真,返回凸包上的点

import cv2

import numpy as np


img = cv2.imread('3.jpg',0)

ret,thresh = cv2.threshold(img,127,255,0)

img,contours,hierarchy = cv2.findContours(thresh, 1, 2)


cnt = contours[1]

hull = cv2.convexHull(cnt)


print(hull)


结果如下


[[[ 44 217]]


[[ 43 218]]


[[ 39 221]]


[[ 36 223]]


[[ 35 223]]


[[ 33 221]]


[[ 28 212]]


[[ 26 208]]


[[ 23 201]]


[[ 18 187]]


[[ 17 183]]


[[ 17 181]]


[[ 26 181]]


[[ 32 184]]


[[ 39 188]]


[[ 42 190]]


[[ 43 191]]



[[ 44 202]]]

将returnPoints设置为False则会输出点的索引


import cv2

import numpy as np


img = cv2.imread('3.jpg',0)

ret,thresh = cv2.threshold(img,127,255,0)

img,contours,hierarchy = cv2.findContours(thresh, 1, 2)


cnt = contours[1]

hull = cv2.convexHull(cnt,returnPoints=False)



print(hull)

结果如下


[[36]

[35]

[33]

[31]

[30]

[29]

[21]

[19]

[13]

[ 3]

[ 1]

[ 0]

[54]

[48]

[42]

[40]

[39]


[37]]

凸检测


检测一个曲线是否是凸的

返回bool值


k = cv2.isContourConvex(cnt)

1

边界矩形(包围盒)


直边界包围盒(AABB包围盒)

就是找到图形对象最高点、最低点、最左点、最右点,画出一个矩形边界

使用函数 cv2.boundingRect()计算

(x,y)为矩形左上角的坐标,(w,h)是矩形的宽和高


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

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

1

2

旋转边界包围盒(OBB包围盒)


矩形包围盒的面积最小,考虑到了对象的旋转(原理使用到到了PCA方法,可以去搜)

使用函数cv2.minAreaRect()获得

返回Box2D结构,包含左上角坐标(x,y)矩形宽,高(w,h),以及旋转角度


可以通过cv2.boxPoints()函数绘制


rect = cv2.minAreaRect(cnt)

box = cv2.boxPoints(rect)

box = np.int0(box)

cv2.drawContours(img,[box],0,(0,0,255),2)


绿色的就是直边界包围盒,红色的是最小矩形包围盒

最小外接圆
使用函数 cv2.minEnclosingCircle()得到
返回圆心和半径

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

center = (int(x),int(y))

radius = int(radius)

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

椭圆拟合
旋转边界的内切圆

ellipse = cv2.fitEllipse(cnt)

cv2.ellipse(img,ellipse,(0,255,0),2)

直线拟合

根据图像中的点拟合出一条直线


rows,cols = img.shape[:2]

[vx,vy,x,y] = cv2.fitLine(cnt, cv2.DIST_L2,0,0.01,0.01)

lefty = int((-x*vy/vx) + y)

righty = int(((cols-x)*vy/vx)+y)

cv2.line(img,(cols-1,righty),(0,lefty),(0,255,0),2)


5 个回复

倒序浏览
奈斯,优秀
回复 使用道具 举报
回复 使用道具 举报
回复 使用道具 举报
回复 使用道具 举报
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马