3

これは、正方形検出の例の出力です。私の問題は、この正方形をフィルター処理することです

http://ozsulastik.com/ocvsquares.png

  • 最初の問題は、同じ領域に対して複数の線を描画することです。
  • 2つ目は、すべての画像ではなくオブジェクトを検出するだけです。

もう1つの問題は、すべての画像を除いて最大のオブジェクトを取得する必要があることです。

http://ozsulastik.com/ocvsquares2.png

検出用のコードは次のとおりです。

static void findSquares( const Mat& image, vector >& squares ){

squares.clear();

Mat pyr, timg, gray0(image.size(), CV_8U), gray;

// down-scale and upscale the image to filter out the noise
pyrDown(image, pyr, Size(image.cols/2, image.rows/2));
pyrUp(pyr, timg, image.size());
vector<vector<Point> > contours;

// find squares in every color plane of the image
for( int c = 0; c < 3; c++ )
{
    int ch[] = {c, 0};
    mixChannels(&timg, 1, &gray0, 1, ch, 1);

    // try several threshold levels
    for( int l = 0; l < N; l++ )
    {
        // hack: use Canny instead of zero threshold level.
        // Canny helps to catch squares with gradient shading
        if( l == 0 )
        {
            // apply Canny. Take the upper threshold from slider
            // and set the lower to 0 (which forces edges merging)
            Canny(gray0, gray, 0, thresh, 5);
            // dilate canny output to remove potential
            // holes between edge segments
            dilate(gray, gray, Mat(), Point(-1,-1));
        }
        else
        {
            // apply threshold if l!=0:
            gray = gray0 >= (l+1)*255/N;
        }

        // find contours and store them all as a list
        findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

        vector<Point> approx;

        // test each contour
        for( size_t i = 0; i < contours.size(); i++ )
        {
            approxPolyDP(Mat(contours[i]), approx, arcLength(Mat(contours[i]), true)*0.02, true);

            if( approx.size() == 4 &&
                fabs(contourArea(Mat(approx))) > 1000 &&
                isContourConvex(Mat(approx)) )
            {
                double maxCosine = 0;

                for( int j = 2; j < 5; j++ )
                {
                    // find the maximum cosine of the angle between joint edges
                    double cosine = fabs(angle(approx[j%4], approx[j-2], approx[j-1]));
                    maxCosine = MAX(maxCosine, cosine);
                }

                if( maxCosine < 0.3 )
                    squares.push_back(approx);
            }
        }
    }
}

}

4

1 に答える 1

4

findContours()のフラグを確認する必要があります。CV_RETR_EXTERNALというフラグを設定して、最も外側の輪郭のみを返すことができます(その内側のすべての輪郭は破棄されます)。これによりフレーム全体が返される可能性があるため、フレームの境界をチェックしないように検索を絞り込む必要があります。これを行うには、関数copyMakeBorder()を使用します。また、拡張機能を削除することをお勧めします。これは、線の両側に重複した輪郭が発生する可能性があるためです(拡張を削除すると、境界線が不要になる場合があります)。これが私の出力です: ここに画像の説明を入力してください

于 2013-02-20T16:32:36.053 に答える