c++ - OpenCV查找方中心C++

标签 c++ opencv

首先,我是OpenCV的一个新手,并且是使用c++代码的初学者。
但是OpenCV对我来说是新手,我尝试通过做项目和做事来学习。
现在,对于我的新项目,我试图在picture中找到正方形的中心。
在我的情况下,图片中只有1个正方形。
我想进一步建立OpenCV的square.cpp示例。
对于我的项目,有两件事需要我帮忙,
1:将窗口的边缘检测为正方形,我不希望这样做。 Example
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;
}
我需要一些帮助。
我如何从称为“平方”的数组中的一个平方中获得一些信息(我很难理解数组中到底是什么;它是点数组吗?)
如果有不清楚的地方,请告诉我,我将尝试重新解释。
先感谢您

最佳答案

首先,您谈论的是正方形,但实际上是在检测矩形。我提供了一个较短的代码,以便能够更好地回答您的问题。
我读取图像,应用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: The edge of the window is detected as a square, I do not want this.


根据您的应用程序有几种选择。您可以使用Canny阈值过滤外部边界,也可以使用其他轮廓检索方法在findContours中查找轮廓,或者使用找到的轮廓区域(例如single_rectangle)过滤cv::contourArea(single_rectangle) < 1000

2: How could I get the centre of 1 square from the squares array?


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

I am having a difficult time understand what exactly is in the array as well; is it an array of points?


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

关于c++ - OpenCV查找方中心C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64315108/

相关文章:

c++ - 为什么我要在静态多态性的简单模板上使用 CRTP?

opencv - 检测点集中的对齐点 OpenCV

c++ - 使用 AudioFileServices 在 iOS 6 中进行颗粒合成

c++ - 如何在 Asp.Net AJAX UpdatePanel 之后获取文档 CDHtmlDialog

c++ - 将 GCC 以外的其他编译器附加到 Windows 上的 Clion IDE

c++ - 哈里斯角点坐标

android - android opencv中的单应矩阵

.net - 相机外部计算错误

python - 使用 Python 计算 OpenCV 库的检测次数

C++:如何修改全局字符串?