c++ opencv椭圆检测

1.源码实现

#include 
#include 
#include 

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> 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椭圆检测)