Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Efficient matrix copying in OpenCV

Tags:

opencv

I have no idea for how to implement matrix implementation efficiently in OpenCV. I have binary Mat nz(150,600) with 0 and 1 elements. I have Mat mk(150,600) with double values. I like to implement as in Matlab as

sk = mk(nz);

That command copy mk to sk only for those element of mk element at the location where nz has 1. Then make sk into a row matrix. How can I implement it in OpenCV efficiently for speed and memory?

like image 264
batuman Avatar asked Jan 20 '26 12:01

batuman


1 Answers

You should take a look at Mat::copyTo and Mat::clone. copyTo will make an copy with optional mask where its non-zero elements indicate which matrix elements need to be copied.

mk.copyTo(sk, nz);

And if you really want a row matrix then call sk.reshape() as member sansuiso already suggested. This method ...

creates alternative matrix header for the same data, with different number of channels and/or different number of rows.

like image 185
bkausbk Avatar answered Jan 22 '26 00:01

bkausbk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!