当前位置:   article > 正文

mat opencv 修改roi,OpenCV更改函数内的Mat(Mat范围)

opencv mat 替换roi

I am passing a Mat to another function and changing it inside the called function. I had expected that being a more complex type it was automatically passed by reference so that the matrix would have changed in the calling function, but it doesn't. Could someone point me at the explanation of how to correctly return a changed Mat from a function?

Here's the code snippet:

void callingFunction(Mat img)

{

Mat tst(100,500,CV_8UC3, Scalar(0,255,0));

saveImg(tst, "Original image", true);

testImg(tst);

saveImg(tst, "Want it to be same as inside testImg but is same as Original", true);

}

void testImg(Mat img)

{

int rs = 50; // rows

int cs = 100; // columns

img = Mat(rs, cs, CV_8UC3, Scalar(255,0,0));

Mat roi(img, Rect(0, 0, cs, rs/2));

roi = Scalar(0,0,255); // change a subsection to a different color

saveImg(img, "inside testImg", true);

}

Thanks!

解决方案

You have to define Mat as parameter-reference (&). Here's edited code:

void callingFunction(Mat& img)

{

Mat tst(100,500,CV_8UC3, Scalar(0,255,0));

saveImg(tst, "Original image", true);

testImg(tst);

saveImg(tst, "Want it to be same as inside testImg but is same as Original", true);

}

void testImg(Mat& img)

{

int rs = 50; // rows

int cs = 100; // columns

img = Mat(rs, cs, CV_8UC3, Scalar(255,0,0));

Mat roi(img, Rect(0, 0, cs, rs/2));

roi = Scalar(0,0,255); // change a subsection to a different color

saveImg(img, "inside testImg", true);

}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/从前慢现在也慢/article/detail/1001666
推荐阅读
相关标签
  

闽ICP备14008679号