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
C++ 在OpenCV中取消旋转一组矩形以形成方形网格_C++_Opencv_Image Processing_Computer Vision - Fatal编程技术网

C++ 在OpenCV中取消旋转一组矩形以形成方形网格

C++ 在OpenCV中取消旋转一组矩形以形成方形网格,c++,opencv,image-processing,computer-vision,C++,Opencv,Image Processing,Computer Vision,我试图映射一个包含这些点的图像,使用左上角点和右下角点绘制为矩形,尽管我确实将这些点绘制为四边形 我正在尝试将上面的方块映射到下面的方块: 我相信我应该从每个四边形获得透视变换。以下是我编写的一些代码,旨在帮助实现这一点: cv::Mat output = cv::Mat::zeros(outputSize,CV_32F); // Uses the size of the quadOutput to std::vector<std::vector<cv::Po

我试图映射一个包含这些点的图像,使用左上角点和右下角点绘制为矩形,尽管我确实将这些点绘制为四边形

我正在尝试将上面的方块映射到下面的方块:

我相信我应该从每个四边形获得透视变换。以下是我编写的一些代码,旨在帮助实现这一点:

cv::Mat output = cv::Mat::zeros(outputSize,CV_32F);


    // Uses the size of the quadOutput to
    std::vector<std::vector<cv::Point2f>> quadOutputImage = quadralateralsFromImageOutput(imageToWarp.size().height);

    cv::Mat perspectiveMatrix = cv::Mat(3,3,CV_32F);

    cv::Mat warp = cv::Mat::zeros(outputSize,CV_32F);

    for (int i = 0; i < quadOutputImage.size(); i ++) {

            // get the mapping from the quadtriangles
            perspectiveMatrix = cv::getPerspectiveTransform(quadCentroids[i],quadOutputImage[i]);

            // perform the warp with the current quadtriangles
            cv::warpPerspective(imageToWarp,warp,perspectiveMatrix,output.size());

            // copy roi to output

    }

    return warp;
cv::Mat output=cv::Mat::zeros(outputSize,cv_32F);
//使用四边形输出的大小
std::vector quadOutputImage=来自ImageOutput的四边形(ImageToArp.size().height);
cv::Mat透视矩阵=cv::Mat(3,3,cv_32F);
cv::Mat warp=cv::Mat::Zero(输出大小,cv_32F);
对于(int i=0;i
我一个方块接一个方块地做,结果显示图像一点也不正确。它似乎有点歪斜

编辑 我也试着找到单应矩阵,但结果很奇怪。这是我写的代码

    cv::Mat warpedImageGeneration(const cv::Mat& imageToWarp,cv::Size outputSize, std::vector<std::vector<cv::Point2f>> quadCentroids) {        

    // Uses the size of the quadOutput 50 by 50 squares
    std::vector<std::vector<cv::Point2f>> quadOutputImage = quadralateralsFromImageOutput(450);

    // flatten the matrices
    std::vector<cv::Point2f> flattenedQuads = flattenMatrix(quadOutputImage);
    std::vector<cv::Point2f> flattenedCentroids = flattenMatrix(quadCentroids);

    cv::Mat perspectiveMatrix = cv::Mat(3,3,CV_32F);

    cv::Mat warp = cv::Mat::zeros(450,450,CV_32F);

    perspectiveMatrix = cv::findHomography(flattenedCentroids, flattenedQuads);

    cv::warpPerspective(imageToWarp, warp, perspectiveMatrix, warp.size());

    return warp;
 }
cv::Mat warpedImageGeneration(const cv::Mat&imageToWarp,cv::Size outputSize,std::vector quadcontroids){
//使用四边形输出的大小为50×50平方
std::vector quadOutputImage=来自图像输出的四边形(450);
//展平矩阵
std::vector FlattedQuads=扁平矩阵(quadOutputImage);
std::向量展平质心=展平矩阵(四质心);
cv::Mat透视矩阵=cv::Mat(3,3,cv_32F);
cv::Mat warp=cv::Mat::Zero(450450,cv_32F);
透视矩阵=cv::findHomography(展平质心、展平四边形);
cv::warpPerspective(imageToWarp,warp,perspectiveMatrix,warp.size());
回经;
}
在示例图像上,这是我得到的未旋转结果:


我认为问题可能是源的顶点没有按四边形顺序排列,即左上角、右上角、左下角和右下角。

通过
getPerspectiveTransform
单独扭曲每个矩形将无法获得最佳结果。相反,如果您知道原始图像中每个四边形映射到扭曲中的确切坐标,请尝试定义一个包含所有这些对应关系的透视变换矩阵。具体来说,查看源图像中每个四边形的所有角点,确定它们在扭曲图像中的映射位置,并定义一个描述所有这些点的透视变换矩阵。这样,当您从源图像提供点时,它们应该精确地映射到扭曲图像中的扭曲点,其他所有内容都应该干净地插值

在实现方面,有两个
向量
容器,其中一个容器包含所有图像点,另一个
向量
容器包含所有扭曲的图像点。顺序很重要,因此请确保源
向量
容器中的每个位置对应于扭曲的
向量
容器上的相同位置

然而,你必须改变档位,改用
findHomography
getPerspectiveTransform
更一般的情况,因为您正在确定一个变换矩阵,该矩阵可以以最小的误差将一组点最佳地扭曲到另一组点
getPerspectiveTransform
仅允许指定4个点
findHomography
允许您指定任意数量的点

如果要将其修改为当前框架,则已经有代码包含输入图像和扭曲图像的每个四边形的点向量向量。您正在访问每个向量并将其指定到
getPerspectiveTransform
中,以获得每个四元体的透视变换矩阵。只需为输入指定一个向量,为包含所有四个点的扭曲图像指定一个向量,然后调用
findHomography
。显然,顺序很重要,因此请确保输入图像中四边形的每个位置与扭曲图像的输出位置相匹配

此外,请确保坐标系正确。OpenCV的
结构假定
x
水平坐标,而
y
垂直坐标。由于您已经将我建议的编辑内容放在了您的原始帖子中,为了完整起见,我将把它们放在这里,并使答案更加完整:

 cv::Mat warpedImageGeneration(const cv::Mat& imageToWarp,cv::Size outputSize, std::vector<std::vector<cv::Point2f>> quadCentroids) {

    // Uses the size of the quadOutput 50 by 50 squares
    std::vector<std::vector<cv::Point2f>> quadOutputImage = quadralateralsFromImageOutput(450);

    // flatten the matrices
    std::vector<cv::Point2f> flattenedQuads = flattenMatrix(quadOutputImage);
    std::vector<cv::Point2f> flattenedCentroids = flattenMatrix(quadCentroids);

    cv::Mat perspectiveMatrix = cv::Mat(3,3,CV_32F);

    cv::Mat warp = cv::Mat::zeros(450,450,CV_32F);

    perspectiveMatrix = cv::findHomography(flattenedCentroids, flattenedQuads);

    cv::warpPerspective(imageToWarp, warp, perspectiveMatrix, warp.size());

    return warp;
 }
cv::Mat warpedImageGeneration(常数cv::Mat&imageToWarp,cv::Size outputSize,std::vector四边形){
//使用四边形输出的大小为50×50平方
std::vector quadOutputImage=来自图像输出的四边形(450);
//展平矩阵
std::vector FlattedQuads=扁平矩阵(quadOutputImage);
std::向量展平质心=展平矩阵(四质心);
cv::Mat透视矩阵=cv::Mat(3,3,cv_32F);
cv::Mat warp=cv::Mat::Zero(450450,cv_32F);
透视矩阵=cv::findHomography(展平质心、展平四边形);
cv::warpPerspective(imageToWarp,warp,perspectiveMatrix,warp.size());
回经;
}

如果您知道原始图像中每个四边形映射到扭曲中的确切坐标,请尝试定义一个透视变换矩阵,该矩阵包含所有这些对应关系。具体来说,看看所有的玉米