วันพฤหัสบดีที่ 23 มีนาคม พ.ศ. 2560

C++ opencv Hough Circle

#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <stdio.h>

using namespace cv;
using namespace std;
/** @function main */
int main(int argc, char** argv)
{
Mat frame, src_gray;

VideoCapture cap(0);
while (1)
{
cap >> frame;

/// Convert it to gray
cvtColor(frame, src_gray, CV_BGR2GRAY);

/// Reduce the noise so we avoid false circle detection
GaussianBlur(src_gray, src_gray, Size(9, 9), 2, 2);

vector<Vec3f> circles;
/// Apply the Hough Transform to find the circles
HoughCircles(src_gray, circles, CV_HOUGH_GRADIENT, 1, src_gray.rows / 8, 200, 100, 0, 0);

/// Draw the circles detected
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 center
circle(frame, center, 3, Scalar(0, 255, 0), 1, 8, 0);
// circle outline
circle(frame, center, radius, Scalar(0, 0, 255), 1, 8, 0);
}
/// Show your results
namedWindow("Hough Circle Transform Demo", CV_WINDOW_AUTOSIZE);
imshow("Hough Circle Transform Demo", frame);
if (waitKey(30) == 27)
break;
}
return 0;
}

ไม่มีความคิดเห็น:

แสดงความคิดเห็น