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

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

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值