1

I am trying to create a Mat A composed of two other Mat B and C so that changing B or C also change a part of A.

Here's an example with some code:

// 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);
}

I want to create a Mat concat that will store the value of Mat myImage and it's copy by reference, but hconcat doesn't work for that, i tried to make my own function and called it merge but it didn't seem to work either.

I want to be able to change my variable concat after it's declaration by only changing myImage.

I couldn't find any other similar post and sorry if my question is not clear.

Entoren
  • 13
  • 2
  • You can't. You can write a custom structure which holds both and update the result image – Moia Apr 17 '19 at 16:10
  • also, you can simplify your "merge" function using two "copyTo", see [here](https://stackoverflow.com/a/33262398/5008845) – Miki Apr 17 '19 at 16:11
  • @Miki it worked thanks, I was thinking of solving it the wrong way – Entoren Apr 17 '19 at 16:25

1 Answers1

1

When you merge you're making a copy of the data, so changing the two input images won't change concat.

You need to make the two input images to point to the concat data:

...
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));

so that changing myImage or myImageCopy will change also concat

Miki
  • 40,887
  • 13
  • 123
  • 202