下面给出的代码用于识别视频Feed中的圈子。 for循环没有执行。为什么会这样?
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <opencv/cv.h>
#include <iostream>
#include <stdio.h>
using namespace cv;
int main(int argc, char** argv)
{
CvCapture* capture=0;
capture=cvCaptureFromCAM(0);
if(!capture)
{
printf("Capture failure\n");
return -1;
}
IplImage* frame;
while(true)
{
frame=cvQueryFrame(capture);
if(!frame) break;
Mat src(frame);
IplImage* framegray=cvCreateImage(cvGetSize(frame),8,3);
Mat srcgray(framegray);
cvtColor(src,srcgray,CV_BGR2GRAY);
GaussianBlur(srcgray,srcgray,Size(9, 9), 2, 2 );
vector<Vec3f> circles;
HoughCircles( srcgray, circles, CV_HOUGH_GRADIENT, 1, srcgray.rows/8, 200, 100, 0, 0 );
for( size_t i = 0; i < circles.size(); i++ )
{
Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
int radius = cvRound(circles[i][2]);
circle( src, center, radius, Scalar(0,255,0), 3, 8, 0 );
}
namedWindow( "Hough Circle Transform Demo", CV_WINDOW_AUTOSIZE );
imshow( "Hough Circle Transform Demo", src );
int c=cvWaitKey(10);
if(char(c)==27)
break;
}
return 0;
}
以下给出了用于在不使用相机时测试程序的样本:|
答案 0 :(得分:2)
我错了。 cv :: houghCircles不需要边作为输入。你需要的只是好的参数。我已经检查过opencv实现,并且已经在内部进行了sobel。感谢@micka进行更正
霍夫需要边缘作为输入。我稍微修改了你的程序,并通过将网络摄像头指向监视器来验证它是否正常工作。
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <opencv/cv.h>
#include <iostream>
#include <stdio.h>
using namespace cv;
int main(int argc, char** argv)
{
CvCapture* capture=0;
capture=cvCaptureFromCAM(0);
if(!capture)
{
printf("Capture failure\n");
return -1;
}
int dp=1,mindist=100,param1=300,param2=60,minradius=0,maxradius=0;
cv::namedWindow("ctrl");
cv::createTrackbar( "dp", "ctrl", &dp, 50);
cv::createTrackbar( "mindist", "ctrl", &mindist, 1000);
cv::createTrackbar( "param1", "ctrl", ¶m1, 1000);
cv::createTrackbar( "param2", "ctrl", ¶m2, 1000);
cv::createTrackbar( "minradius", "ctrl", &minradius, 1000);
cv::createTrackbar( "maxradius", "ctrl", &maxradius, 1000);
IplImage* frame;
while(true)
{
frame=cvQueryFrame(capture);
if(!frame) break;
Mat src(frame);
IplImage* framegray=cvCreateImage(cvGetSize(frame),8,3);
Mat srcgray(framegray);
cvtColor(src,srcgray,CV_BGR2GRAY);
vector<Vec3f> circles;
HoughCircles( srcgray, circles, CV_HOUGH_GRADIENT, dp,mindist, param1, param2, minradius, maxradius );
for( size_t i = 0; i < circles.size(); i++ )
{
Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
int radius = cvRound(circles[i][2]);
circle( src, center, radius, Scalar(0,255,0), 3, 8, 0 );
}
namedWindow( "Hough Circle Transform Demo", CV_WINDOW_AUTOSIZE );
imshow( "Hough Circle Transform Demo", src );
cvReleaseImage(&framegray);
int c=cvWaitKey(1);
if(char(c)==27)
break;
}
return 0;
}
答案 1 :(得分:1)
尝试使用您的代码,它可以与您的测试图像配合使用。我不再使用IplImage将代码更改为一致的c ++ openCV语法。
void identifyCircles()
{
cv::Mat src = cv::imread("identifyCircles2.png");
cv::Mat srcgray;
cv::cvtColor(src,srcgray,CV_BGR2GRAY);
cv::GaussianBlur(srcgray,srcgray,Size(9, 9), 2, 2 );
std::vector<cv::Vec3f> circles;
cv::HoughCircles( srcgray, circles, CV_HOUGH_GRADIENT, 1, srcgray.rows/8, 200, 100, 0, 0 );
for( size_t i = 0; i < circles.size(); i++ )
{
cv::Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
int radius = cvRound(circles[i][2]);
circle( src, center, radius, cv::Scalar(0,255,0), 3, 8, 0 );
}
cv::namedWindow( "Hough Circle Transform Demo", CV_WINDOW_AUTOSIZE );
cv::imshow( "Hough Circle Transform Demo", src );
cv::imwrite("houghCirclesIdentified.png",src);
cv::waitKey(-1);
}
给我输出:
所以您的问题可能是视频流输入,那里的圈子可能不够“好”。或者您的图像未正确流式传输(图像是否显示?!?)。
如果未输入for loop
,circles.size() is <= 0
,您可能需要添加std::cout << "circles found in this image: " << circles.size() << std::endl
或if(circles.size() == 0) std::cout << "no circles found" << std::endl
进行调试。