使用openCV和python检测对象 [英] Detect object with openCV and python

查看:439
本文介绍了使用openCV和python检测对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用OpenCV和Python检测下图中的白点。

I'm trying to detect the white dots in the following image using OpenCV and Python.

我尝试使用函数cv2.HoughCircles但没有任何成功。

I tried using the function cv2.HoughCircles but without any success.

我是否需要使用其他方法?

Do I need to use a different method?

这是我的代码:

import cv2, cv
import numpy as np
import sys

if len(sys.argv)>1:
    filename = sys.argv[1]
else:
    filename = 'p.png'

img_gray = cv2.imread(filename,cv2.CV_LOAD_IMAGE_GRAYSCALE)

if img_gray==None:
    print "cannot open ",filename

else:
    img = cv2.GaussianBlur(img_gray, (0,0), 2)
    cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
    circles = cv2.HoughCircles(img,cv2.cv.CV_HOUGH_GRADIENT,4,10,param1=200,param2=100,minRadius=3,maxRadius=100)
if circles:
    circles = np.uint16(np.around(circles))
    for i in circles[0,:]:
        cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),1) 
        cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3)   

cv2.imshow('detected circles',cimg)
cv2.waitKey(0)
cv2.destroyAllWindows()


推荐答案

如果您可以在OpenCV中重现形态重建,您可以轻松构建h-dome变换,从而显着简化任务。否则,高斯滤波的简单阈值也可能就足够了。

If you can reproduce a morphological reconstruction in OpenCV, you can easily build a h-dome transform which simplifies the task significantly. Otherwise, a simple threshold on a gaussian filtering might be enough too.

二值化[FillingTransform [GaussianFilter [f,2],0.4,Padding - > 1]]

高斯滤波在上面的代码中完成,有效地抑制了输入边界周围的噪声,否则就是h-dome变换。

The gaussian filtering was done in the code above to effectively suppress the noise around the border of the input, which would remain after the h-dome transform otherwise.

接下来是高斯滤波后的简单阈值结果( Binarize [GaussianFilter [f,2] ,0.5] )以及使用Kapur阈值方法直接二值化给出的另一个结果(参见文章使用直方图的熵进行灰度图像阈值处理的新方法(这是不再是一种新方法,它来自1985)):

Next there is the result of a simple threshold after a gaussian filtering (Binarize[GaussianFilter[f, 2], 0.5]) as well another result that is given by a direct binarization using Kapur's thresholding method (see the paper "A new method for gray-level picture thresholding using the entropy of the histogram" (which is no longer a new method, it is from 1985)):

上面的右图有很多小点(在此图像分辨率下无法看到),但是全自动。从这3个选项中,OpenCV中只存在第二个选项。

The right image above has a lot of small points all over the border (which cannot be seen at this image resolution), but is fully automatic. From these 3 options, only the second one is already present in OpenCV.

这篇关于使用openCV和python检测对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆