opencv::輪廓發現(find contour in your image)


 

輪廓發現(find contour)
輪廓發現是基於圖像邊緣提取的基礎尋找對象輪廓的方法。
所以邊緣提取的閾值選定會影響最終輪廓發現結果

 

 

//發現輪廓
cv::findContours(
    InputOutputArray  binImg,         // 輸入圖像,非0的像素被看成1,0的像素值保持不變,8-bit
    OutputArrayOfArrays  contours,    // 全部發現的輪廓對象
    OutputArray,  hierachy            // 圖該的拓撲結構,可選,該輪廓發現算法正是基於圖像拓撲結構實現。
    int mode,                         // 輪廓返回的模式
    int method,                        // 發現方法
    Point offset=Point()            // 輪廓像素的位移,默認(0, 0)沒有位移
)

//繪制輪廓
drawContours(
    InputOutputArray  binImg,         // 輸出圖像
    OutputArrayOfArrays  contours,    //  全部發現的輪廓對象
    Int contourIdx                    // 輪廓索引號
    const Scalar & color,            // 繪制時候顏色
    int  thickness,                    // 繪制線寬
    int  lineType ,                    // 線的類型LINE_8
    InputArray hierarchy,            // 拓撲結構圖
    int maxlevel,                    // 最大層數, 0只繪制當前的,1表示繪制繪制當前及其內嵌的輪廓
    Point offset=Point()            // 輪廓位移,可選
}

 

 

Mat src, dst;
int threshold_value = 100;
int threshold_max = 255;
RNG rng;
void Demo_Contours(int, void*);
int main(int argc, char** argv) {
    src = imread(STRPAHT);
    if (src.empty()) {
        printf("could not load image...\n");
        return -1;
    }

    cvtColor(src, src, CV_BGR2GRAY);
    createTrackbar("Threshold Value:", "findcontours - demo", &threshold_value, threshold_max, Demo_Contours);
    Demo_Contours(0, 0);

    waitKey(0);
    return 0;
}

void Demo_Contours(int, void*) {
    Mat canny_output;
    vector<vector<Point>> contours;
    vector<Vec4i> hierachy;
    Canny(src, canny_output, threshold_value, threshold_value * 2, 3, false);
   //發現 findContours(canny_output, contours, hierachy, RETR_TREE, CHAIN_APPROX_SIMPLE, Point(
0, 0)); dst = Mat::zeros(src.size(), CV_8UC3); RNG rng(12345); for (size_t i = 0; i < contours.size(); i++) { Scalar color = Scalar(rng.uniform(0, 255), rng.uniform(0, 255), rng.uniform(0, 255));
     //繪制 drawContours(dst, contours, i, color,
2, 8, hierachy, 0, Point(0, 0)); } imshow("output_win", dst); }

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM