OpenCV图像处理基础opencv

c++ opencv椭圆检测

2022-10-11  本文已影响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
上一篇下一篇

猜你喜欢

热点阅读