繁体   English   中英

OpenCV 求平方中心 C++

[英]OpenCV Finding square center c++

首先,我是 OpenCV 的完全新手,并且是 C++ 代码的初学者/合理的。 但是 OpenCV 对我来说是新的,我尝试通过做项目和东西来学习。

现在,对于我的新项目,我试图在图片中找到正方形的中心。 就我而言,图片中只有 1 个正方形。 我想进一步构建 OpenCV 的 square.cpp 示例。

对于我的项目,有两件事我需要帮助,

1:检测到窗口边缘为正方形,我不想要这个。 例子

2:我怎样才能从正方形数组中得到 1 个正方形的中心?

这是示例“square.cpp”中的代码

// The "Square Detector" program.
// It loads several images sequentially and tries to find squares in
// each image

#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"

#include <iostream>

using namespace cv;
using namespace std;

static void help(const char* programName)
{
    cout <<
        "\nA program using pyramid scaling, Canny, contours and contour simplification\n"
        "to find squares in a list of images (pic1-6.png)\n"
        "Returns sequence of squares detected on the image.\n"
        "Call:\n"
        "./" << programName << " [file_name (optional)]\n"
        "Using OpenCV version " << CV_VERSION << "\n" << endl;
}


int thresh = 50, N = 11;
const char* wndname = "Square Detection Demo";

// helper function:
// finds a cosine of angle between vectors
// from pt0->pt1 and from pt0->pt2
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);
}

// returns sequence of squares detected on the image.
static void findSquares(const Mat& image, vector<vector<Point> >& 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:
                //     tgray(x,y) = gray(x,y) < (l+1)*255/N ? 255 : 0
                gray = gray0 >= (l + 1) * 255 / N;
            }

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

            vector<Point> approx;

            // test each contour
            for (size_t i = 0; i < contours.size(); i++)
            {
                // approximate contour with accuracy proportional
                // to the contour perimeter
                approxPolyDP(contours[i], approx, arcLength(contours[i], true) * 0.02, true);

                // square contours should have 4 vertices after approximation
                // relatively large area (to filter out noisy contours)
                // and be convex.
                // Note: absolute value of an area is used because
                // area may be positive or negative - in accordance with the
                // contour orientation
                if (approx.size() == 4 &&
                    fabs(contourArea(approx)) > 1000 &&
                    isContourConvex(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 cosines of all angles are small
                    // (all angles are ~90 degree) then write quandrange
                    // vertices to resultant sequence
                    if (maxCosine < 0.3)
                        squares.push_back(approx);
                }
            }
        }
    }
}

int main(int argc, char** argv)
{
    static const char* names[] = { "testimg.jpg", 0 };
    help(argv[0]);

    if (argc > 1)
    {
        names[0] = argv[1];
        names[1] = "0";
    }

    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;
        }

        vector<vector<Point> > squares;
        findSquares(image, squares);

        polylines(image, squares, true, Scalar(0, 0, 255), 3, LINE_AA);
        imshow(wndname, image);

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

    return 0;
}

我需要一些帮助才能开始。 我如何从称为“正方形”的数组中的 1 个正方形中获取一些信息(我很难理解数组中的确切内容;它是一个点数组吗?)

如果有不清楚的地方,请告诉我,我会尝试重新解释。

先感谢您

首先,您在谈论正方形,但实际上是在检测矩形。 我提供了一个较短的代码,以便能够更好地回答您的问题。

我阅读了图像,应用 Canny 过滤器进行二值化并检测所有轮廓。 之后,我遍历轮廓并找到那些可以精确地由四个点近似并且是凸的:

int main(int argc, char** argv)
{
    // Reading the images 
    cv::Mat img = cv::imread("squares_image.jpg", cv::IMREAD_GRAYSCALE);
    cv::Mat edge_img;
    std::vector <std::vector<cv::Point>> contours;

    // Convert the image into a binary image using Canny filter - threshold could be automatically determined using OTSU method
    cv::Canny(img, edge_img, 30, 100);

    // Find all contours in the Canny image
    findContours(edge_img, contours, cv::RETR_LIST, cv::CHAIN_APPROX_SIMPLE);

    // Iterate through the contours and test if contours are square
    std::vector<std::vector<cv::Point>> all_rectangles;
    std::vector<cv::Point> single_rectangle;
    for (size_t i = 0; i < contours.size(); i++)
    {

        // 1. Contours should be approximateable as a polygon
        approxPolyDP(contours[i], single_rectangle, arcLength(contours[i], true) * 0.01, true);

        // 2. Contours should have exactly 4 vertices and be convex
        if (single_rectangle.size() == 4 && cv::isContourConvex(single_rectangle))
        {
            // 3. Determine if the polygon is really a square/rectangle using its properties (parallelity, angles etc.)
            // Not necessary for the provided image

            // Push the four points into your vector of squares (could be also std::vector<cv::Rect>)
            all_rectangles.push_back(single_rectangle);
        }
    }

    for (size_t num_contour = 0; num_contour < all_rectangles.size(); ++num_contour) {
        cv::drawContours(img, all_rectangles, num_contour, cv::Scalar::all(-1));
    }

    cv::imshow("Detected rectangles", img);
    cv::waitKey(0); 

    return 0;
    
}

1:检测到窗口边缘为正方形,我不想要这个。

根据您的应用程序,有多种选择。 您可以使用 Canny 阈值过滤外部边界,使用不同的轮廓检索方法在findContours查找轮廓或使用找到的轮廓的面积过滤single_rectangle (例如cv::contourArea(single_rectangle) < 1000 )。

2:我怎样才能从正方形数组中得到 1 个正方形的中心?

由于代码已经在检测四个角点,您可以例如找到对角线的交点。 如果您知道图像中只有矩形,您还可以尝试使用 Hu 矩检测检测到的轮廓的所有质心。

我也很难理解数组中的确切内容; 它是一组点吗?

OpenCV 中的一个轮廓始终表示为单个点的向量。 如果要添加多个轮廓,则使用的是点向量的向量。 在您提供的示例中, squares是一个由 4 个点组成的向量的向量。 在这种情况下,您还可以使用cv::Rect向量。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM