3

我正在尝试使用 Iphone 检测带有 openCV 的彩色球体。对于第一个测试用例,我使用了一个带有给定代码的黄色大理石:

cv::Mat thresholdHSV;
cv::Mat imgHSV;

cv::cvtColor(inputFrame, imgHSV, CV_BGR2HSV);

cv::inRange(imgHSV,cv::Scalar(20,100,100),cv::Scalar(30,255,255),thresholdHSV);

std::vector<std::vector<cv::Point> > contours;

findContours(thresholdHSV.clone(), contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);


//Draw them
cv::Mat destinationSource = cv::Mat::zeros(inputFrame.size(), inputFrame.type());
drawContours(destinationSource, contours, -1, cv::Scalar(255,255,255), CV_FILLED);

这给了我已经很好的结果: 在此处输入图像描述

但是我需要以某种方式检测圆形。理想情况下,我想应用 HoughCircle,但是我收到 OpenCv 错误:“参数错误(源图像必须是 8 位单通道)。

我也试过申请

HoughCircles(thresholdHSV, detectedCircles, CV_HOUGH_GRADIENT, 1, thresholdHSV.rows / 8, 200, 100, 0, 0);

但我根本没有得到任何结果。

如何在destinationSource 图像上应用HoughCircle,或者有没有其他方法可以检测圆形?(我还必须考虑何时有更多相同颜色的球体彼此非常接近,因为 findContours 只会找到一个计数)

任何帮助都非常感谢,并感谢您的时间。

4

1 回答 1

3

该错误表明您的输入图像应该是单通道 8 位图像,因此彩色图像不适用。

下面是一个用 HoughCircles 进行圆检测的小代码(但在 Python 中,但你会理解它)。

import cv2
import numpy as np
import sys

img = cv2.imread('img.jpg',0)
if img==None:
    print "cannot open ",filename

else:
    img = cv2.medianBlur(img,5)
    cimg = cv2.cvtColor(img,cv2.COLOR_GRAY2BGR)
    circles = cv2.HoughCircles(img,cv2.cv.CV_HOUGH_GRADIENT,1,10,param1=100,param2=30,minRadius=5,maxRadius=50)
    circles = np.uint16(np.around(circles))
    for i in circles[0,:]:
        cv2.circle(cimg,(i[0],i[1]),i[2],(0,255,0),1) # draw the outer circle
        cv2.circle(cimg,(i[0],i[1]),2,(0,0,255),3) # draw the center of the circle

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

下面是输出:

在此处输入图像描述

您可以在这里找到 C++ 代码:https ://github.com/Itseez/opencv/blob/master/samples/cpp/houghcircles.cpp

于 2013-04-19T17:10:28.913 回答