OpenCV学习笔记十一-findcounters函数
生活随笔
收集整理的這篇文章主要介紹了
OpenCV学习笔记十一-findcounters函数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
findCounters函數是個重載函數,有兩種聲明方式:
普通聲明:
findContours( InputOutputArray image, OutputArrayOfArrays contours,int mode, int method, Point offset=Point());高級一點的:
findContours( InputOutputArray image, OutputArrayOfArrays contours,OutputArray hierarchy, int mode,int method, Point offset=Point());高級一點的聲明主要是多了一個 OutputArray hierarchy:
hierarchy的作用是說明各個輪廓的繼承關系。
hierarchy也是一個向量,長度和contours相等,每個元素和contours的元素對應。hierarchy的每個元素是一個包含四個整型數的向量。即:
vector<Vec4i> hierarchy; //Vec4i is a vector contains four number of inthierarchy[i][0],hierarchy[i][1],hierarchy[i][2],hierarchy[i][3],分別表示的是第i條輪廓(contours[i])的下一條,前一條,包含的第一條輪廓(第一條子輪廓)和包含他的輪廓(父輪廓)。我在使用時發現findcounters函數檢測圓時,對同一個圓總是返回兩個圓的點集,但是我只想要一個圓的點集,發現可以用hierarchy這個變量來解決問題,總是訪問外面的圓就可以。
注意:要自己多看hierarchy函數變量的值就明白其用意了
OpenCV4.1.1+VS2017 author:Chenandong time:2019.08.11 --------------------------------------------------------------- function:輪廓邊緣查找,主要使用 findcounters ---------------------------------------------------------------#include "opencv2/imgcodecs.hpp" #include "opencv2/highgui.hpp" #include "opencv2/imgproc.hpp" #include <iostream>using namespace cv; using namespace std;Mat src_gray; int thresh = 200;RNG rng(12345);void thresh_callback(int, void*);int main(int argc, char** argv) { //加載圖像并檢測是否加載成功Mat src = imread("circle2.jpg");if (src.empty()){cout << "Could not open or find the image!\n" << endl;cout << "Usage: " << argv[0] << " <Input image>" << endl;return -1;}--------------圖像前處理--------------------//轉化為灰度圖cvtColor(src, src_gray, COLOR_BGR2GRAY);//濾波blur(src_gray, src_gray, Size(3, 3));---------------------------------------------//展示前處理后的圖像const char* source_window = "Source";namedWindow(source_window);imshow(source_window, src);//-----------------------------------------------------------------------------------------------------//先做邊緣檢測,用Canny函數完成二值化處理Mat canny_output;Canny(src_gray, canny_output, thresh, thresh * 2);//注意threash 是一個全局變量,滑動條引用了threash的地址//調用findCounter函數vector<vector<Point> > contours;vector<Vec4i> hierarchy;findContours(canny_output, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE);//繪制輪廓Mat drawing = Mat::zeros(canny_output.size(), CV_8UC3);int count1 = 0;int count2 = 0;把所有輪廓都畫出來//-------------------------------------------------------------------------------for (size_t i = 0; i < contours.size(); i++){count1 += 1;Scalar color = Scalar(0, 0, 255);drawContours(drawing, contours, (int)i, color, 1, LINE_AA, hierarchy, 0);}cout << "count1 is : \n" << count1 << endl;//------------------------------------------------------------------------只畫頂層輪廓-------------------------------------------------------------------------------//for (int index = 0; index >= 0; index = hierarchy[index][0]) {// count2 += 1;// Scalar color = Scalar(255, 0, 0);// drawContours(drawing, contours, (int)index, color, 1, LINE_AA, hierarchy, 0);//}//cout << "count2 is : \n" << count2 << endl;-----------------------------------------------------------------------------------imshow("Contours", drawing);waitKey();return 0; }//--------------------------------------------------------------------------------------------------邊緣查找,并調用滑動條函數,回調thresh_callback //const int max_thresh = 255; //createTrackbar("Canny thresh:", source_window, &thresh, max_thresh, thresh_callback);//注意&threash //thresh_callback(0, 0); // 查找邊緣并繪制邊緣 //void thresh_callback(int thresh, void*) //{ // //先做邊緣檢測,用Canny函數完成二值化處理 // Mat canny_output; // Canny(src_gray, canny_output, thresh, thresh * 2);//注意threash 是一個全局變量,滑動條引用了threash的地址 // // //調用findCounter函數 // vector<vector<Point> > contours; // vector<Vec4i> hierarchy; // findContours(canny_output, contours, hierarchy, RETR_TREE, CHAIN_APPROX_SIMPLE); // // //繪制輪廓 // Mat drawing = Mat::zeros(canny_output.size(), CV_8UC3); // int count1 = 0; // int count2 = 0; // // for (size_t i = 0; i < contours.size(); i++) // { // count1 += 1; // Scalar color = Scalar(0, 0, 255); // drawContours(drawing, contours, (int)i, color, 2, LINE_8, hierarchy, 0); // } // // for (int index = 0; index >= 0; index = hierarchy[index][0]) { // count2 += 1; // Scalar color = Scalar(255,0,0); // drawContours(drawing, contours, (int)index, color, 2, LINE_8, hierarchy, 0); // } // // printf("count1 is : \n", count1); // printf("count2 is : \n", count2); // // imshow("Contours", drawing); //} //---------------------------------------------------------------------------------------------不使用hierarchy變量時:
使用hierarchy變量的作用:
[外鏈圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片保存下來直接上傳(img-o7YmjmWy-1574154822710)(https://chenandongtime.github.io/img/findcounters2.jpg)]
參考:http://www.voidcn.com/article/p-pgtgzhaw-du.html
總結
以上是生活随笔為你收集整理的OpenCV学习笔记十一-findcounters函数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: OpenCV学习笔记十:hough变换
- 下一篇: hough变换原理