c++ - 找到 4 个特定的角像素并将它们与扭曲透视一起使用

标签 c++ opencv image-processing

我正在研究 OpenCV,我想知道您将如何构建一个简单版本的透视变换程序。我有一个平行四边形的图像,它的每个角都由一个具有特定颜色的像素组成,图像中没有其他颜色。我想遍历所有像素并找到这 4 个像素。然后我想将它们用作新图像中的角点,以扭曲原始图像的视角。最后我应该有一个放大的正方形。

Point2f src[4]; //Is this the right datatype to use here?
int lineNumber=0;
//iterating through the pixels
for(int y = 0; y < image.rows; y++)
{
    for(int x = 0; x < image.cols; x++)
    {
        Vec3b colour = image.at<Vec3b>(Point(x, y));
    if(color.val[1]==245 && color.val[2]==111 && color.val[0]==10) { 
        src[lineNumber]=this pixel // something like Point2f(x,y) I guess
        lineNumber++;
    }
    }
}
/* I also need to get the dst points for getPerspectiveTransform 
and afterwards warpPerspective, how do I get those? Take the other 
points, check the biggest distance somehow and use it as the maxlength to calculate 
the rest? */

您应该如何使用 OpenCV 来解决问题? (我只是想我不是在用“正常而聪明的方式”)另外我该怎么做下一步,即使用多个像素作为“标记”并计算多个像素中间的平均点点。有什么比遍历每个像素更有效的方法吗?

基本上是这样的:

enter image description here

最佳答案

从带有彩色圆圈作为标记的图像开始,例如:

enter image description here

请注意,这是一个 png 图像,即具有保留实际颜色的无损压缩。如果您使用像 jpeg 这样的有损压缩,颜色会稍微改变,并且您无法像此处那样使用完全匹配的方式对它们进行分割。

您需要找到每个标记的中心。

  1. 使用 inRange 分割(已知)颜色
  2. findContours 找到给定颜色的所有连通分量
  3. 找到最大的 blob,这里使用 max_element 完成带有 lambda 函数,以及 distance .您可以为此使用 for 循环。
  4. 找到最大 Blob 的质心,此处使用 moments 完成.最后,您也可以在这里使用循环。
  5. 将中心添加到您的顶点。

您的目标顶点就是目标图像的四个角。

然后您可以使用 getPerspectiveTransformwarpPerspective找到并应用扭曲。

生成的图像是:

enter image description here

代码:

#include <opencv2/opencv.hpp>
#include <vector>
#include <algorithm>
using namespace std;
using namespace cv;

int main()
{
    // Load image
    Mat3b img = imread("path_to_image");

    // Create a black output image
    Mat3b out(300,300,Vec3b(0,0,0));

    // The color of your markers, in order
    vector<Scalar> colors{ Scalar(0, 0, 255), Scalar(0, 255, 0), Scalar(255, 0, 0), Scalar(0, 255, 255) }; // red, green, blue, yellow

    vector<Point2f> src_vertices(colors.size());
    vector<Point2f> dst_vertices = { Point2f(0, 0), Point2f(0, out.rows - 1), Point2f(out.cols - 1, out.rows - 1), Point2f(out.cols - 1, 0) };

    for (int idx_color = 0; idx_color < colors.size(); ++idx_color)
    {
        // Detect color
        Mat1b mask;
        inRange(img, colors[idx_color], colors[idx_color], mask);

        // Find connected components
        vector<vector<Point>> contours;
        findContours(mask, contours, RETR_EXTERNAL, CHAIN_APPROX_NONE);

        // Find largest
        int idx_largest = distance(contours.begin(), max_element(contours.begin(), contours.end(), [](const vector<Point>& lhs, const vector<Point>& rhs) {
            return lhs.size() < rhs.size();
        }));

        // Find centroid of largest component
        Moments m = moments(contours[idx_largest]);
        Point2f center(m.m10 / m.m00, m.m01 / m.m00);

        // Found marker center, add to source vertices
        src_vertices[idx_color] = center;
    }

    // Find transformation
    Mat M = getPerspectiveTransform(src_vertices, dst_vertices);

    // Apply transformation
    warpPerspective(img, out, M, out.size());

    imshow("Image", img);
    imshow("Warped", out);
    waitKey();

    return 0;
}

关于c++ - 找到 4 个特定的角像素并将它们与扭曲透视一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33883505/

相关文章:

c++ - 在 C++ 中读取图像数组(使用 opencv)

c++ - 在标题中定义 cv::Mat 后跟另一个 Mat 避免了多个 channel

python - 点检测和圆区域选择

android - Android 增强现实应用程序的图像识别

c++ - 如何将指针序列化为 int 数组?

c++ - 每次循环迭代后递减迭代器显示奇怪的行为

matlab - 如何检测图像是纹理还是基于图案的图像?

image - 转换为灰度是图像预处理的必要步骤吗?

c++ - 如何使用 quazip 压缩目录/文件夹?

C++11 向后兼容性(空整数常量到指针的转换)