8

opencv c++ から Hierarchy と findContours を理解しようとしていますが、難しいと感じています。

ここでこの質問をしましたが、まだ明確に理解できません。以下のサンプル画像を使用して、さまざまな例で使用してみました

ここに画像の説明を入力

上の画像の場合、次のコードを実行します。

    #include "opencv2/highgui/highgui.hpp"
    #include "opencv2/imgproc/imgproc.hpp"
    #include <iostream>
    #include <stdio.h>
    #include <stdlib.h>

    using namespace cv;
    using namespace std;

    Mat src; Mat src_gray;
    int thresh = 100;
    int max_thresh = 255;
    RNG rng(12345);

    void thresh_callback(int, void* );

    /** @function main */
    int main( int argc, char** argv )
    {
            src = imread( argv[1], 1 );
            cvtColor( src, src_gray, CV_BGR2GRAY );
            blur( src_gray, src_gray, Size(3,3) );

            String source_window = "Source";
            namedWindow( source_window, CV_WINDOW_AUTOSIZE );
            imshow( source_window, src );

            createTrackbar( " Canny thresh:", "Source", &thresh, max_thresh, thresh_callback );
            thresh_callback( 0, 0 );

            waitKey(0);
            return(0);
    }

    /** @function thresh_callback */
    void thresh_callback(int, void* )
    {
            Mat canny_output;
            vector<vector<Point> > contours;
            vector<Vec4i> hierarchy;
            int rows = 0,cols = 0;

            Canny( src_gray, canny_output, thresh, thresh*3, 3 );
            findContours( canny_output, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE, Point(0, 0) );

            for(int i=0;i<hierarchy.size();i++)
            {
                    cout<<hierarchy[i]<<endl;

            }
    /*      for(int i =0;i<contours.size();i++)
            {
                    for(int j = 0;j<contours[i].size();j++)
                    {
                            cout<<"printing contours"<<endl;
                            cout<<contours[i][j]<<endl;
                    }
            }*/

            Mat drawing = Mat::zeros( canny_output.size(), CV_8UC3 );
            for( int i = 0; i< contours.size(); i++ )
            {
                    Scalar color = Scalar( rng.uniform(0, 255), rng.uniform(0,255), rng.uniform(0,255) );
                    drawContours( drawing, contours, i, color, 2, 8, hierarchy, 0, Point() );
            }

            /// Show in a window
            namedWindow( "Contours", CV_WINDOW_AUTOSIZE );
            imshow( "Contours", drawing );
    }

階層の上記の画像から得た出力は次のとおりです

    [6, -1, 1, -1]
    [-1, -1, 2, 0]
    [-1, -1, 3, 1]
    [-1, -1, 4, 2]
    [-1, -1, 5, 3]
    [-1, -1, -1, 4]
    [-1, 0, -1, -1]

hereの説明によると、7つの輪郭があります。画像に2つしか表示されていない場合、これがどのように可能かわかりません。外側の輪郭と内側の輪郭に囲まれた別の輪郭。

次に、6、5、3 という数字は何を表しているのでしょうか。説明を読んでも、根底にあるロジックを理解できません。誰かがこれを説明できれば本当に助かります。

疑問を解消するために、さまざまな画像タイプで試してみました。下はもう一つ

ここに画像の説明を入力

そしてこれに対する答えは

      [2, -1, 1, -1]
      [-1, -1, -1, 0]
      [-1, 0, -1, -1]

これで 3 つの等高線ができましたが、実際には肉眼では 1 つしかありません。

最後に、最悪のケースを試してみました。

ここに画像の説明を入力

そして出力は、

    [-1, -1, 1, -1]
    [-1, -1, -1, 0]

私は明らかにいくつかの論理が欠けていることを知っています。誰かが説明できれば、それは大きな助けになるでしょう。

いくつかのしきい値に基づいて、大、中、小の領域に基づいて輪郭を分割しようとしています。輪郭の面積を計算しようとしていますが、完全には理解できません。

私が推測できることは、可能な単一の輪郭ごとに2つの輪郭領域が描かれている可能性があるということです。1 つは境界で、もう 1 つは内側の穴またはスペースです。それはそのようなものですか?

それでも、階層の数はまだ私を混乱させます。

どんな助けでも本当に感謝しています。前もって感謝します :)。

4

0 に答える 0