加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

在opencv-python中检测星形

发布时间:2020-12-16 22:53:58 所属栏目:Python 来源:网络整理
导读:我需要检测形状并计算图像中每个形状的出现.我最初检测到轮廓并对它们进行近似,并计算每个轮廓中的顶点.我的代码如下所示: import cv2import numpy as np import collectionsimport sysimg = cv2.imread(str(sys.argv[1]),0)ret,thresh = cv2.threshold(img

我需要检测形状并计算图像中每个形状的出现.我最初检测到轮廓并对它们进行近似,并计算每个轮廓中的顶点.我的代码如下所示:

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

我的代码不能用于检测此图像中的星星:

Image with stars and other basic shapes

我应该在代码中做出哪些更改?

最佳答案
对我有用的是比较形状周边区域的平方根.一颗恒星约为0.145(/ – .0015,因为有些边缘没有完美出现).六边形为0.255,三角形为.21,正方形为.247,五边形为.250.

圆度也起作用(三角形在0.26到.27之间),并且它的区别相似(六边形为.83,三角形为.55-.56,正方形为.77,五边形为.78) )

下面是它的C代码(我的PC上没有python,但想法是一样的):

#include "stdafx.h"
#include 

两种方式 – 使用圆形或我的sqrt(区域)/ arclength方法 – 导致:

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读