c++ opencv椭圆检测

作者: 一路向后 | 来源:发表于2022-10-11 21:46 被阅读0次

    1.源码实现

    #include <iostream>
    #include <math.h>
    #include <opencv2/opencv.hpp>
    
    using namespace std;
    using namespace cv;
    
    int main(int argc, char **argv)
    {
        Mat src, dst;
        Mat out;
    
        src = imread("1.png");
    
        //中值滤波
        medianBlur(src, out, 3);
        cvtColor(out, out, CV_BGR2GRAY);
    
        //提取轮廓
        std::vector<std::vector<Point>> contours;
    
        findContours(out, contours, CV_RETR_LIST, CV_CHAIN_APPROX_NONE);
    
        //找椭圆
        Mat elliImg(out.size(),CV_8UC3, Scalar(0));
        int accumulate;
    
        for(int i=0; i< contours.size(); i++)
        {
            size_t count = contours[i].size();
    
            if(count < 40 || count > 1000) continue;
    
            Mat pointsf;
            Mat(contours[i]).convertTo(pointsf,CV_32F);
    
            RotatedRect box = fitEllipse(pointsf);
    
            ellipse(src, box, Scalar(0,0,255), 2, CV_AA);
        }
    
        imwrite("2.png", src);
    
        return 0;
    }
    

    2.编译源码

    $ g++ -o ellipse ellipse.cpp -std=c++11 -I/usr/local/opencv3/include -L/usr/local/opencv3/lib -lopencv_core -lopencv_imgproc -lopencv_imgcodecs -Wl,-rpath=/usr/local/opencv3/lib
    

    3.原图及结果

    1.png
    2.png

    相关文章

      网友评论

        本文标题:c++ opencv椭圆检测

        本文链接:https://www.haomeiwen.com/subject/fpizartx.html