NOT A DOG NO SERIOUSLY
NOT A DOG NO SERIOUSLY

Reputation: 229

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

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.

Upvotes: 22

Views: 33054

Answers (2)

Ela782
Ela782

Reputation: 5181

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

Upvotes: 33

Russbear
Russbear

Reputation: 1261

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

Upvotes: 9

Related Questions