我正在尝试使用 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 位,单 channel )。
我也试过申请
HoughCircles(thresholdHSV, detectedCircles, CV_HOUGH_GRADIENT, 1, thresholdHSV.rows / 8, 200, 100, 0, 0);
但我根本没有得到任何结果。
如何在 destinationSource 图像上应用 HoughCircle 或者是否有任何其他方法来检测圆形? (我还必须考虑何时有更多相同颜色的球体彼此非常接近,因为那时 findContours 只会找到一个 countour)
非常感谢您的帮助,感谢您抽出宝贵时间。
最佳答案
错误表明您的输入图像应该是单 channel 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
关于opencv - 使用 HSV 和 HoughCircles 通过 OpenCV 检测彩色球体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16102076/