我正在尝试创建由其他两个Mat B和C组成的Mat 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不适用于此,我尝试创建自己的函数并称其为合并,但似乎没有也可以工作。
我希望能够在声明后通过仅更改myImage来更改变量concat。
我找不到其他类似的帖子,对不起,如果我的问题不清楚。
答案 0 :(得分:1)
merge
是在复制数据,因此更改两个输入图像不会更改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));
以便更改myImage
或myImageCopy
也会更改concat