霍夫圆

霍夫圆

示例代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;


int main() {


    Mat frame = imread("../assets/water_coins.jpg", IMREAD_COLOR);
    imshow("frame", frame);

    Mat grayImg;
    cvtColor(frame, grayImg, COLOR_BGR2GRAY);
    imshow("grayimg", grayImg);

    vector<Vec3f> circles;
//      HoughCircles(grayImg,circles,HOUGH_GRADIENT,1,150,100,30,50,90);
    HoughCircles(grayImg, circles, HOUGH_GRADIENT, 1, 10, 100, 30, 5, 30);


    for (int i = 0; i < circles.size(); ++i) {
        Vec3f c = circles.at(i);
        Point center(c[0], c[1]);
        float radius = c[2];
        cout << radius << endl;
        circle(frame, center, radius, Scalar(0, 255, 0), 2, LINE_AA);
    }


    imshow("rest", frame);

    waitKey(0);
    destroyAllWindows();
    return 0;
}