Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
OpenCV查找方形中心c++; 首先,我是OpenCV的一个新手,在C++代码中是初学者。 但是OpenCV对我来说是新事物,我试着通过做项目和其他事情来学习_C++_Opencv - Fatal编程技术网

OpenCV查找方形中心c++; 首先,我是OpenCV的一个新手,在C++代码中是初学者。 但是OpenCV对我来说是新事物,我试着通过做项目和其他事情来学习

OpenCV查找方形中心c++; 首先,我是OpenCV的一个新手,在C++代码中是初学者。 但是OpenCV对我来说是新事物,我试着通过做项目和其他事情来学习,c++,opencv,C++,Opencv,现在,对于我的新项目,我正试图在一个平面上找到广场的中心。 就我而言,图片中只有一个正方形。 我想进一步构建OpenCV的square.cpp示例 对于我的项目,有两件事我需要一些帮助 1:窗口的边缘被检测为正方形,我不想要这个 2:如何从正方形阵列中获得1个正方形的中心 这是示例“square.cpp”中的代码 //方形探测器”程序。 //它按顺序加载多个图像,并尝试在图像中查找正方形 //每张图片 #包括“opencv2/core.hpp” #包括“opencv2/imgproc.hpp”

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

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

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

2:如何从正方形阵列中获得1个正方形的中心

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

//方形探测器”程序。
//它按顺序加载多个图像,并尝试在图像中查找正方形
//每张图片
#包括“opencv2/core.hpp”
#包括“opencv2/imgproc.hpp”
#包括“opencv2/imgcodecs.hpp”
#包括“opencv2/highgui.hpp”
#包括
使用名称空间cv;
使用名称空间std;
静态无效帮助(常量字符*程序名)
{

cout首先,你说的是正方形,但实际上你是在检测矩形。为了更好地回答你的问题,我提供了一个较短的代码

我读取图像,应用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;
    
}
int main(int argc,char**argv)
{
//读图
cv::Mat img=cv::imread(“squares\u image.jpg”,cv::imread\u GRAYSCALE);
cv::垫边_img;
矢量轮廓;
//使用Canny滤波器将图像转换为二值图像-使用OTSU方法可以自动确定阈值
cv::Canny(img,edge_img,30100);
//在Canny图像中查找所有轮廓
findContours(边缘、轮廓、cv::RETR\u列表、cv::CHAIN\u近似值\u SIMPLE);
//遍历轮廓并测试轮廓是否为正方形
std::矢量所有_矩形;
std::向量单_矩形;
对于(size_t i=0;i
1:窗口的边缘被检测为正方形,我不想要这个

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

2:如何从正方形阵列中获得1个正方形的中心

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

我很难理解数组中到底是什么;它是一个点数组吗


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

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

我读取图像,应用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;
    
}
int main(int argc,char**argv)
{
//读图
cv::Mat img=cv::imread(“squares\u image.jpg”,cv::imread\u GRAYSCALE);
cv::垫边_img;
矢量轮廓;
//使用Canny滤波器将图像转换为二值图像-使用OTSU方法可以自动确定阈值
cv::Canny(img,edge_img,30100);
//在Canny图像中查找所有轮廓
findContours(边缘、轮廓、cv::RETR\u列表、cv::CHAIN\u近似值\u SIMPLE);
//遍历轮廓并测试轮廓是否为正方形
std::矢量所有_矩形;
std::向量单_矩形;
对于(size_t i=0;i