【问题标题】:How to relate an image with another image in OpenCV如何将图像与 OpenCV 中的另一个图像相关联
【发布时间】:2020-05-19 13:52:15
【问题描述】:

我在 OpenCV C++ 中做这个项目,我在其中制作给定图像的反射,就像翻转函数一样,但使用每个像素的坐标。问题是我得到的图像输出都是蓝色的,水平有一条线,我相信我的代码只影响第一个通道。

我试图做imageReflectionFinal.at<Vec3b>(r,c) = image.at<Vec3b>(r,c); 来解决它,但没有任何改变。我会在下面留下代码,提前谢谢。

    Mat image = imread("image_dir/image.jpg");
    Mat imageReflectionFinal = Mat::zeros(image.size(), image.type());
    for(unsigned int r=0; r<image.rows; r++) {
        for(unsigned int c=0; c<image.cols; c++) {
                imageReflectionFinal.at<Vec3b>(r,c) = image.at<Vec3b>(r,c);
                Vec3b sourcePixel = image.at<Vec3b>(r,c);
                imageReflectionFinal.at<Vec3b>(r, c) = (uchar)(c, -r + (220)/2);
        }
    }

【问题讨论】:

  • (uchar)(c, -r + (220)/2) -- 逗号是逗号操作符,因此这与static_cast&lt;uchar&gt;(110 - r) 相同。将每个像素的蓝色通道设置为这个值没有任何意义,因此结果。 |在同一条语句中还涉及溢出和一个错误。
  • 哎呀!如今的计算机非常有弹性。在过去,大型机会因如此多的编程错误而起火。
  • 不使用flip的原因是什么?

标签: c++ image opencv reflection


【解决方案1】:

如果你不想使用flip函数,你可以镜像改变每个rowsx-coordinates(cols)。代码如下:

#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>

using namespace cv;

int main() {

    //You can change as "Mat3b" for the 3-channel images
    Mat1b image = imread("/ur/image/directory/image.jpg",CV_LOAD_IMAGE_GRAYSCALE);
    Mat1b imageReflectionFinal = Mat::zeros(image.size(), image.type());
    for(unsigned int r=0; r<image.rows; r++) {
        for(unsigned int c=0; c<image.cols; c++) {

            imageReflectionFinal(r, c) = image(r, image.cols - 1 - c);
            //y-axis(r) doesnt change only x-axis(cols) mirroring 
        }
    }

    imshow("Result",imageReflectionFinal);
    waitKey(0);
    return 0;
}

This answer is also my reference.

【讨论】:

  • 嘿,非常感谢,我成功了,现在我正在尝试应用 x 和 y 轴镜像,我看到了您留下的链接,但无法完全理解。所以现在我创建了另一个空白图像,使图像 > x 轴镜像到 imageReflection > y 轴镜像到 imageReflectionFinal,最后得到:` imageReflection(r, c) = image(r, image.cols - 1 - c) ; imageReflectionFinal(r, c) = imageReflection(imageReflection.rows - 1 - imageReflection.cols, c);` 我还在玩它,但图像似乎总是越界。
猜你喜欢
  • 2013-07-12
  • 1970-01-01
  • 1970-01-01
  • 2020-02-03
  • 1970-01-01
  • 2023-03-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多