簡體   English   中英

如何使用opencv/c++檢測圖像中最大的正方形?

[英]How to detect the largest square in an image using opencv/c++?

我不知道這個問題是否已經被回答,但我是 opencv 的新手,我需要回答:)。

這是我使用的代碼:

static double angle( Point pt1, Point pt2, Point pt0 )
{
    double dx1 = pt1.x - pt0.x;
    double dy1 = pt1.y - pt0.y;
    double dx2 = pt2.x - pt0.x;
    double dy2 = pt2.y - pt0.y;
    return (dx1*dx2 + dy1*dy2)/sqrt((dx1*dx1 + dy1*dy1)*(dx2*dx2 + dy2*dy2) + 1e-10);
}
void find_squares(Mat& image, vector<vector<Point> >& squares)
{
    Mat blurred(image);
    medianBlur(image, blurred, 9);

    Mat gray0(blurred.size(), CV_8U), gray;
    vector<vector<Point> > contours;

    for (int c = 0; c < 3; c++)
    {
        int ch[] = {c, 0};
        mixChannels(&blurred, 1, &gray0, 1, ch, 1);

        const int threshold_level = 2;
        for (int l = 0; l < threshold_level; l++)
        {
            if (l == 0)
            {
                Canny(gray0, gray, 10, 20, 3); //

                dilate(gray, gray, Mat(), Point(-1,-1));
            }
            else
            {
                    gray = gray0 >= (l+1) * 255 / threshold_level;
            }

            findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

            vector<Point> approx;
            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++)
                            {
                                    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);
                    }
            }
        }
    }
}
static void drawSquares( Mat& image, const vector<vector<Point> >& squares )
{
    for( size_t i = 0; i < squares.size(); i++ )
    {
        const Point* p = &squares[i][0];
        int n = (int)squares[i].size();
        polylines(image, &p, &n, 1, true, Scalar(0,255,0), 3, LINE_AA);
    }
    imshow(wndname, image);
}
int main(void)
{
static const char* names[] = { "C:\\Users\\hugoo\\Documents\\Prog\\NexterMU\\Qt\\OpenCV\\DetectionShapeProgram\\IMG_0527.jpg",0 };

    vector<vector<Point> > squares;

    for( int i = 0; names[i] != 0; i++ )
    {
        string filename = samples::findFile(names[i]);
        Mat image = imread(filename, IMREAD_COLOR);
        if( image.empty() )
        {
            cout << "Couldn't load " << filename << endl;
            continue;
        }

        find_squares(image, squares);
        drawSquares(image, squares);

        int c = waitKey();
        if( c == 27 )
            break;
    }

    return 0;
}

原始圖像: 1結果圖像: 2

我想要的只是圖像中最大的正方形。 不是最大廣場內的其他人。

我不知道我的代碼有什么問題。 它似乎對某些人有用。

我指望你的幫助,提前謝謝你:)。

雨果。

要獲得最大的正方形,只需嘗試對peri = cv.arcLength(contour, True)中的周長值進行閾值處理,假設 peri > 500,應該只為您提供周長大於 500 的輪廓

所以在一些幫助下它現在可以工作了!

下面的解決方案可以獲得圖像中最大的正方形:


void find_squares(Mat& image, vector<vector<Point> >& squares)
{
    Mat blurred(image);
    medianBlur(image, blurred, 9);

    Mat gray0(blurred.size(), CV_8U), gray;
    vector<vector<Point> > contours;

    for (int c = 0; c < 3; c++)
    {
        int ch[] = {c, 0};
        mixChannels(&blurred, 1, &gray0, 1, ch, 1);

        const int threshold_level = 2;
        for (int l = 0; l < threshold_level; l++)
        {
            if (l == 0)
            {
                Canny(gray0, gray, 10, 20, 3); //

                dilate(gray, gray, Mat(), Point(-1,-1));
            }
            else
            {
                    gray = gray0 >= (l+1) * 255 / threshold_level;
            }

            findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

            vector<Point> approx;
            for (size_t i = 0; i < contours.size(); i++)
            {
                double peri = arcLength(contours[i], true);

                if(peri > 10000){

                    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++)
                            {
                                    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);
                    }
                }
            }
        }
    }
}

Thank you @Eyshika.

Hugo.


暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM