c++ opencv橢圓檢測

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.原圖及結果


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章