Home > Backend Development > C++ > How to Accurately Detect and Extract Corner Points of a Sheet of Paper Using OpenCV?

How to Accurately Detect and Extract Corner Points of a Sheet of Paper Using OpenCV?

Susan Sarandon
Release: 2024-12-31 12:29:09
Original
427 people have browsed it

How to Accurately Detect and Extract Corner Points of a Sheet of Paper Using OpenCV?

How to Detect Sheets of Paper and Extract Corner Points Using OpenCV

In this article, we'll improve the widely used OpenCV square detection example to filter out extraneous results and retrieve accurate corner points of a sheet of paper from an image.

The original OpenCV example was unable to effectively filter out noise, making the output messy and challenging to process. To address this, we propose a modified implementation:

void find_squares(Mat&amp; image, vector<vector<Point> >&amp; squares)
{
    // Blur the image for enhanced edge detection
    Mat blurred(image);
    medianBlur(image, blurred, 9);

    // Convert to grayscale
    Mat gray0(blurred.size(), CV_8U), gray;

    // Detect contours for each color plane in the image
    for (int c = 0; c < 3; c++)
    {
        // Isolate a single color plane
        int ch[] = {c, 0};
        mixChannels(&amp;blurred, 1, &amp;gray0, 1, ch, 1);

        // Iterate through multiple threshold levels
        const int threshold_level = 2;
        for (int l = 0; l < threshold_level; l++)
        {
            if (l == 0)
            {
                // Use Canny instead of zero threshold to improve detection
                Canny(gray0, gray, 10, 20, 3);
                dilate(gray, gray, Mat(), Point(-1, -1)); // Remove potential holes
            }
            else
            {
                gray = gray0 >= (l + 1) * 255 / threshold_level;
            }

            // Find contours for each threshold level
            vector<vector<Point> > contours;
            findContours(gray, contours, CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE);

            // Test contours to identify valid squares
            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 
                    && abs(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);
                }
            }
        }
    }
}
Copy after login

After applying this improved implementation, the resulting squares vector will contain the largest detected square, representing the sheet of paper. To extract the paper's corner points, identify the square with the maximum area from the squares vector. The four corner points of this square are the desired corner points of the sheet of paper.

In summary, our enhanced OpenCV implementation enables reliable detection of sheets of paper by eliminating false positives and accurately extracting corner points, making it a powerful tool for image processing applications.

The above is the detailed content of How to Accurately Detect and Extract Corner Points of a Sheet of Paper Using OpenCV?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template