【发布时间】:2015-12-10 13:35:53
【问题描述】:
我需要检测形状并计算图像中每个形状的出现次数。我最初检测轮廓并对其进行近似,并计算每个轮廓中存在的顶点。我的代码如下所示:
import cv2
import numpy as np
import collections
import sys
img = cv2.imread(str(sys.argv[1]),0)
ret,thresh = cv2.threshold(img,127,255,0)
contours,hierarchy = cv2.findContours(thresh,1,2)
no_of_vertices = []
i = 0
mask = np.zeros(img.shape,np.uint8)
for contour in contours:
cnt = contour
area = cv2.contourArea(cnt)
if area>150:
epsilon = 0.02*cv2.arcLength(cnt,True)
approx = cv2.approxPolyDP(cnt,epsilon,True)
no_of_vertices.append(len(approx))
counter = collections.Counter(no_of_vertices)
a,b = counter.keys(),counter.values()
i=0
while i<len(counter):
print a[i],b[i]
i = i + 1
我的代码无法检测这张图片中的星星:
我应该对代码进行哪些更改?
【问题讨论】:
-
从(密集或稀疏)轮廓,尝试 matchShape 函数:docs.opencv.org/2.4/modules/imgproc/doc/…
-
您可以使用 circarity 来检测形状:
(4*pi*area) / (perimeter^2)。例如,星形的圆形度在 0.25 左右
标签: python opencv image-processing computer-vision