内容来自OpenCV-Python Tutorials 自己翻译整理 长宽比:
边界矩形的宽高比
<span class="MathJax" id="MathJax-Element-15-Frame" tabindex="0" data-mathml="AspectRation=WidthHeight" 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;">AspectRation=WidthHeight 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(img, 1, 2)
cnt = contours[1] x,y,w,h = cv2.boundingRect(cnt)#获取边界 aspect_ratio = float(w)/h#计算比率
<span class="MathJax" tabindex="0" data-mathml="AspectRation=WidthHeight" 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(aspect_ratio) 面积比
轮廓面积与边界矩形的面积比 <span class="MathJax" id="MathJax-Element-25-Frame" tabindex="0" data-mathml="Extent=ObjectAreaBoundingRectangleArea" 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;">Extent=ObjectAreaBoundingRectangleArea 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(img, 1, 2)
cnt = contours[1] area = cv2.contourArea(cnt)#获取轮廓面积 x,y,w,h = cv2.boundingRect(cnt)#获取边界 rect_area = w*h extent = float(area)/rect_area <span class="MathJax" tabindex="0" data-mathml="Extent=ObjectAreaBoundingRectangleArea" 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(extent) 凸包面积比
轮廓面积与凸包面积的比 <span class="MathJax" id="MathJax-Element-31-Frame" tabindex="0" data-mathml="Solidity=ContourAreaConvexHullArea" 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;">Solidity=ContourAreaConvexHullArea 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(img, 1, 2)
cnt = contours[1] area = cv2.contourArea(cnt) hull = cv2.convexHull(cnt) hull_area = cv2.contourArea(hull) solidity = float(area)/hull_area <span class="MathJax" tabindex="0" data-mathml="Solidity=ContourAreaConvexHullArea" 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(solidity) 轮廓直径
与轮廓面积相等的圆形的直径 <span class="MathJax" id="MathJax-Element-41-Frame" tabindex="0" data-mathml="EquivalentDiameter=4×ContourAreaπ" 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;">EquivalentDiameter=4×ContourAreaπ−−−−−−−−−−√ 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(img, 1, 2)
cnt = contours[1] area = cv2.contourArea(cnt) equi_diameter = np.sqrt(4*area/np.pi) <span class="MathJax" tabindex="0" data-mathml="EquivalentDiameter=4×ContourAreaπ" 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(equi_diameter) 长短轴方向 对象的方向,下面的方法还会返回长轴和短轴的长度
(x,y),(MA,ma),angle = cv2.fitEllipse(cnt) 蒙板与像素
有时我们需要目标对象在图像里所有像素信息 (这里没使用灰度图)
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(img, 1, 2)
cnt = contours[1] mask = np.zeros(img.shape,np.uint8) cv2.drawContours(mask,[cnt],0,255,-1)
pixelpoints = np.transpose(np.nonzero(mask))#像素 此处使用了两种方法,分别为numpy的函数和使用opencv的函数。结果相同,但是numpy给出的是行列值,而opencv给出的是(x,y)形式
最大值和最小值及位置 要用到mask 像素蒙板
min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(img,mask = mask) 平均颜色和平均灰度
可以使用相同的蒙板求一个对象的平均颜色或平均灰度
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(img, 1, 2)
cnt = contours[1] mask = np.zeros(img.shape,np.uint8) mean_val = cv2.mean(img,mask = mask)
print(mean_val) 极值点 就是求图像中某个对象的最高、最低、最左、最右的点
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(img, 1, 2)
cnt = contours[1] 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])
print(leftmost)
|