c++ - 有没有办法在保持图像引用的同时从两个图像创建一个垫子

标签 c++ opencv

我正在尝试创建一个由另外两个垫子 B 和 C 组成的垫子 A,以便更改 B 或 C 也会更改 A 的一部分。

下面是一些代码示例:

// not important, i am only using it to change the Mat
void someFunction(Mat & image)
{
    for (int y = 0; y < image.rows; y++)
    {
        for (int x = 0; x < image.cols; x++)
        {
            image.at<Vec3b>(Point(x, y)) = image.at<Vec3b>(Point(x, y)) * 2;
        }
    }
}

// I am taking image and image2, and putting them poth in outputImage
// It does the same thing then hconcat.
void merge(Mat & image, Mat& image2, Mat & outputImage)
{
    for (int y = 0; y < image.rows; y++)
    {
        for (int x = 0; x < image.cols; x++)
        {
            outputImage.at<Vec3b>(Point(x, y)) = image.at<Vec3b>(Point(x, y));
            outputImage.at<Vec3b>(Point(x + image.cols, y)) = image2.at<Vec3b>(Point(x, y));
        }
    }
}

void mainFunction()
{
    // Reading image from file path
    Mat myImage = imread("img/left.jpeg");
    Mat myImageCopy = myImage;

    // Creating the Mat to hold the two other
    Mat concat(myImage.rows, myImage.cols*2, CV_8UC3,Scalar(0,0,0));

    // This is where i am doing something wrong
    // I want concat to keep a reference or a pointer with myImage
    // So that if myImage is changed, concat is also changed
    merge(myImage, myImage.clone(), concat);

    // showing the 3 Mat
    imshow("myImage", myImage);
    imshow("myImageCopy", myImageCopy);
    imshow("concat", concat);

    // I change the value of some pixel at myImage
    someFunction(myImage);

    // showing the 3 mat again, myImage and myImageCopy are both changed but concat is the same 
    imshow("myImageAfter", myImage);
    imshow("myImageCopyAfter", myImageCopy);
    imshow("concatAfter", concat);
    waitKey(0);
}

我想创建一个 Mat concat 来存储 Mat myImage 的值并通过引用进行复制,但 hconcat 对此不起作用,我尝试创建自己的函数并将其称为 merge 但它似乎没有要么工作。

我希望能够在声明后仅通过更改 myImage 来更改我的变量 concat。

我找不到任何其他类似的帖子,如果我的问题不清楚,抱歉。

最佳答案

当您合并时,您正在复制数据,因此更改两个输入图像不会更改concat

您需要使两个输入图像指向concat 数据:

...
merge(myImage, myImage.clone(), concat);

myImage = concat(cv::Rect(0, 0, myImage.cols, myImage.rows)); 
myImageCopy = concat(cv::Rect(myImage.cols, 0, myImage.cols, myImage.rows));

这样改变 myImagemyImageCopy 也会改变 concat

关于c++ - 有没有办法在保持图像引用的同时从两个图像创建一个垫子,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55731871/

相关文章:

c++ - C++ 结构中的类

c++ - 从文本文件中读取时间条目并将它们存储到 3D 数组中以便它们在 C++ 中可用

c++ - 如何使用 2D 跟踪和初始 3D 姿势估计 3D 姿势

image-processing - 如何在 OpenCV 中进行非透视图像变形?

c++ - 在vs2012中使用opencv加载和显示图像

c++ - 是否有按值传递类对象的标准程序?

c++ - c++中以下类似的语句有区别吗?

c++ - 从 CIImage 创建垫子的最佳方法?

c++ - 使用依赖项 : how to get target dependencies on host? 交叉编译

c++ - C语言蓝牙paquet分析