Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In OpenCV 2.1: How to assign a matrix to a submatrix of another matrix?

Tags:

Assume I have a matrix

A = cv::Mat(3,3,CV_32F)  

and a matrix

B = cv::Mat(2,2,CV_32F). 

Let's say A has all zeros and B has all ones. I want to assign the values of B to the upper left corner of A. How can I do this?

I tried the following:

A(cv::Rect_<int>(0,0,2,2)) = B 

But this doesn't seem to work. However assigning a scalar value to the subrect of A this way does work:

A(cv::Rect_<int>(0,0,2,2)) = 1.0 

What is wrong with the first approach?

like image 703
Christian Avatar asked Oct 27 '10 08:10

Christian


1 Answers

I'd prefer a one-liner, but this does the trick:

cv::Mat tmp = A(cv::Rect(0,0,2,2)); B.copyTo(tmp); 
like image 142
Christian Avatar answered Oct 06 '22 14:10

Christian