Open CV系列学习笔记(十九)对象测量
对象测量
对象检测是指利用图像处理与模式识别等领域的理论和方法,检测出图像中存在的目标对象,确定这些目标对象的语义类别,并标定出目标对象在图像中的位置。对象检测是对象识别的前提。只有检测到对象才能对对象进行识别。
在计算机视觉研究领域,对象检测一般可以分为图形对象检测和视频对象检测,利用图像处理与模式识别等领域的理论和方法,从图像或视频中分离出有一定意义的实体——对象,如人、物体等。在对象检测中有一类通用的对象检测方法, 似物性度量(Objectness measure), 利用矩形框将图像中所有可能存在的对象区域定位出来并给出这个窗口内包含对象的概率。例如,通过贝叶斯框架将多种图像信息进行融合, 定量地计算出每个窗口包含对象的概率。
弧长与面积
多边形拟合
几何矩计算
相关API代码演示
轮廓发现
计算每个轮廓的弧长与面积, 像素单位
多边形拟合
获取轮廓的多边形拟合结果
-approxPolyDP
-contour
-epsilon越小越折线越逼近真实形状
-close – 是否为闭合区域
几何矩计算
代码:
def measure_object(image):
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)
print("threshold value : %s"%ret)
cv.imshow("binary image", binary)
dst = cv.cvtColor(binary, cv.COLOR_GRAY2BGR)
cloneImage ,contours, hireachy = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
for i, contour in enumerate(contours):
area = cv.contourArea(contour)
x, y, w, h = cv.boundingRect(contour)
rate = min(w, h)/max(w, h)
print("rectangle rate : %s"%rate)
mm = cv.moments(contour)#计算中心点
print(type(mm))
cx = mm['m10']/mm['m00']
cy = mm['m01']/mm['m00']
cv.circle(dst, (np.int(cx), np.int(cy)), 3, (0, 255, 255), -1)
cv.rectangle(dst, (x, y), (x+w, y+h), (0, 0, 255), 2)
print("contour area %s"%area)
approxCurve = cv.approxPolyDP(contour,4, True)
print(approxCurve.shape)
if approxCurve.shape[0] > 6:#判断形状并绘制
cv.drawContours(dst, contours, i, (0, 255, 0), 2)
if approxCurve.shape[0] == 4:
cv.drawContours(dst, contours, i, (0, 0, 255), 2)
if approxCurve.shape[0] == 3:
cv.drawContours(dst, contours, i, (255, 0, 0), 2)
if approxCurve.shape[0] == 5:
cv.drawContours(dst, contours, i, (255, 255, 0), 2)
if approxCurve.shape[0] == 6:
cv.drawContours(dst, contours, i, (255, 0, 255), 2)
cv.imshow("measure-contours", dst)
结果:
--------- Python OpenCV Tutorial ---------
threshold value : 137.0
rectangle rate : 0.8615384615384616
<class ‘dict’>
contour area 2510.0
(5, 1, 2)
rectangle rate : 0.9375
<class ‘dict’>
contour area 1971.0
(4, 1, 2)
rectangle rate : 0.9692307692307692
<class ‘dict’>
contour area 3010.0
(6, 1, 2)
rectangle rate : 0.6
<class ‘dict’>
contour area 1908.5
(8, 1, 2)
rectangle rate : 0.5054945054945055
<class ‘dict’>
contour area 4050.0
(4, 1, 2)
rectangle rate : 0.96
<class ‘dict’>
contour area 1799.5
(8, 1, 2)
rectangle rate : 0.6923076923076923
<class ‘dict’>
contour area 2143.0
(3, 1, 2)
rectangle rate : 0.9423076923076923
<class ‘dict’>
contour area 2447.0
(4, 1, 2)
完整代码:
import cv2 as cv
import numpy as np
def measure_object(image):
gray = cv.cvtColor(image, cv.COLOR_BGR2GRAY)
ret, binary = cv.threshold(gray, 0, 255, cv.THRESH_BINARY_INV | cv.THRESH_OTSU)
print("threshold value : %s"%ret)
cv.imshow("binary image", binary)
dst = cv.cvtColor(binary, cv.COLOR_GRAY2BGR)
cloneImage ,contours, hireachy = cv.findContours(binary, cv.RETR_EXTERNAL, cv.CHAIN_APPROX_SIMPLE)
for i, contour in enumerate(contours):
area = cv.contourArea(contour)
x, y, w, h = cv.boundingRect(contour)
rate = min(w, h)/max(w, h)
print("rectangle rate : %s"%rate)
mm = cv.moments(contour)#计算中心点
print(type(mm))
cx = mm['m10']/mm['m00']
cy = mm['m01']/mm['m00']
cv.circle(dst, (np.int(cx), np.int(cy)), 3, (0, 255, 255), -1)
cv.rectangle(dst, (x, y), (x+w, y+h), (0, 0, 255), 2)
print("contour area %s"%area)
approxCurve = cv.approxPolyDP(contour,4, True)
print(approxCurve.shape)
if approxCurve.shape[0] > 6:#判断形状并绘制
cv.drawContours(dst, contours, i, (0, 255, 0), 2)
if approxCurve.shape[0] == 4:
cv.drawContours(dst, contours, i, (0, 0, 255), 2)
if approxCurve.shape[0] == 3:
cv.drawContours(dst, contours, i, (255, 0, 0), 2)
if approxCurve.shape[0] == 5:
cv.drawContours(dst, contours, i, (255, 255, 0), 2)
if approxCurve.shape[0] == 6:
cv.drawContours(dst, contours, i, (255, 0, 255), 2)
cv.imshow("measure-contours", dst)
print("--------- Python OpenCV Tutorial ---------")
src = cv.imread("E:/picture/37.jpg")
cv.namedWindow("input image", cv.WINDOW_AUTOSIZE)
cv.imshow("input image", src)
measure_object(src)
cv.waitKey(0)
cv.destroyAllWindows()