我对OpenCV很陌生,但我写了一个程序,可以检测学校项目视频中的圆圈。问题:在我的视频(.avi)中,有一些圈子没有被检测到,还有一些"东西"这是定义的不是圆圈,但它们被检测到。我在Windows 7上使用 OpenCV 3.0.0 ,我的编程语言是 C ++ 。您可以在下面找到代码。
我是如何与您分享视频文件的想法(可能会根据要求发送电子邮件)?
我认为问题是HoughCircles方法中的参数。 有什么建议我怎么能找到想要的圈子? (我还在视频中发布了一个截图(灰色))。
如您所见,我想跟踪飞机起点/降落车道上的圆圈。
#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
VideoCapture cap("C:/Users/Julian Büchel/Documents/Screen Video/Circle_Rotenburg.avi"); // open the video file for reading
if (!cap.isOpened()) // if not success, exit program
{
cout << "Cannot open the video file" << endl;
return -1;
}
double fps = cap.get(CV_CAP_PROP_FPS); //get the frames per seconds of the video
cout << "Frame per seconds : " << fps << endl;
namedWindow("MyVideo", CV_WINDOW_NORMAL); //create a window called "MyVideo"
namedWindow("Trackbar", CV_WINDOW_NORMAL); //create a window called "Trackbar"
resizeWindow("MyVideo", 1200, 700);
resizeWindow("Trackbar", 1200, 200);
//These are the parameters for my HoughCircles method. I added sliders to be able to adjust them.
int iSliderValue1 = 1;
createTrackbar("dp", "Trackbar", &iSliderValue1, 4);
int iSliderValue2 = 200;
createTrackbar("Min. Distance", "Trackbar", &iSliderValue2, 500);
int iSliderValue3 = 200;
createTrackbar("P1", "Trackbar", &iSliderValue3, 500);
int iSliderValue4 = 100;
createTrackbar("P2", "Trackbar", &iSliderValue4, 500);
int iSliderValue5 = 0;
createTrackbar("Min.R", "Trackbar", &iSliderValue5, 40);
int iSliderValue6 = 15;
createTrackbar("Max.R", "Trackbar", &iSliderValue6, 50);
while (1)
{
Mat frame, gray;
bool bSuccess = cap.read(frame); // read a new frame from video
if (!bSuccess) //if not success, break loop
{
cout << "Cannot read the frame from video file" << endl;
waitKey(10000);
break;
}
//Should I add the GaussianBlur? Right now you can see the circles more clear, but it takes up more CPU.
cvtColor(frame, gray, CV_RGB2GRAY);
//GaussianBlur(gray, gray, Size(9, 9), 2, 2);
vector<Vec3f> circles; //Store the circles
// Apply the Hough Transform to find the circles
// src vector method dp dist p1 p2 miR maR
//HoughCircles(gray, circles, CV_HOUGH_GRADIENT, 2, 200, 200, 100, 25, 35);
HoughCircles(gray, circles, CV_HOUGH_GRADIENT, iSliderValue1, iSliderValue2, iSliderValue3, iSliderValue4, iSliderValue5, iSliderValue6);
//Draw circles
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(gray, center, 3, Scalar(0, 255, 0), -1, 8, 0);// circle center
circle(gray, center, radius, Scalar(0, 0, 255), 3, 8, 0);// circle outside
cout << "center : " << center << "\nradius : " << radius << endl;
}
imshow("MyVideo", gray); //show the frame in "MyVideo" window
waitKey(25);//Make video slower/back to normal speed
if (waitKey(30) == 27) //wait for 'esc' key press for 30 ms. If 'esc' key is pressed, break loop
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;
}