Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OpenCV/C++: Copying a row/column in a Mat to another?

Tags:

c++

opencv

I know I can do this by copying each element myself, but is there a method that does it for me? I tried mat2.copyTo(mat1.row(0)) but that doesn't work.

like image 917
NOT A DOG NO SERIOUSLY Avatar asked Jul 12 '11 20:07

NOT A DOG NO SERIOUSLY


2 Answers

Try

Mat mat1row = mat1.row(0);
mat2.copyTo(mat1row);

(assuming mat2 has the same size as the destination row).

That should do the job and is more clear.

Edit: This is even shorter and recommended by the official documentation:

A.row(j).copyTo(A.row(i));

More details on this in the official documentation: http://docs.opencv.org/modules/core/doc/basic_structures.html#Mat%20Mat%3a%3arow%28int%20y%29%20const

like image 172
Ela782 Avatar answered Nov 20 '22 19:11

Ela782


Try

destMat.row(i) = (sourceMat.row(i) + 0);

It's not very pretty but it gets the job done. Also see this. Read the comments on Mat::row

like image 36
Russbear Avatar answered Nov 20 '22 19:11

Russbear