C++ opencv检测圆
#include <opencv2/opencv.hpp> #include <opencv2/highgui/highgui.hpp> #include <opencv2/imgproc/imgproc.hpp> #include <iostream> using namespace cv; using namespace std; int main(int argc, char** argv) { // 读取图像 Mat src = imread(argv[1], IMREAD_COLOR); if (!src.data) { cout << "Could not open or find the image" << endl; return -1; } // 转换为灰度图像 Mat gray; cvtColor(src, gray, COLOR_BGR2GRAY); // 使用Canny边缘检测 Mat edges; Canny(gray, edges, 50, 200); // 使用Hough变换检测圆 vector<Vec3f> circles; HoughCircles(edges, circles, CV_HOUGH_GRADIENT, 1, gray.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), 2, FILLED); } // 显示结果 namedWindow("Detected circles", WINDOW_AUTOSIZE); imshow("Detected circles", src); waitKey(0); return 0; }
###
QQ 3087438119