Convert MatOfKeyPoint to bytes[] and converse

asked 2015-07-14 21:00:16 -0600

ndk076 gravatar image

updated 2015-07-15 03:00:54 -0600

Hi, I need to convert MatOfKeyPoint to bytes[] and converse in OpenCV Java.

Does anyone have experiment with it before?

I really need it.!!!!

Thanks!

edit retag flag offensive close merge delete

Comments

might work the usual way:

int nbytes = m.total() * m.elemSize();
byte[] bytes = new byte[nbytes];
m.get(0,0, bytes);
...
m.put(0,0,bytes);
berak gravatar imageberak ( 2015-07-16 00:33:56 -0600 )edit

Thank berak for your answer. But when I used it, I got the other issue.

Mat data type is not compatible at: m.get(0,0, bytes);

ndk076 gravatar imagendk076 ( 2015-07-19 00:19:20 -0600 )edit

apologies, did not actually test it ;(

this would work:

MatOfKeyPoint m = new MatOfKeyPoint(
    new KeyPoint(3,3,3),
    new KeyPoint(4,4,4)
);

int nelems = m.rows() * 7;
float[] data = new float[nelems];
m.get(0,0, data );
//...
m.put(0,0,data );

but iirc(SO) - you wanted to save it to a database, and byte[] is required, correct ?

so, i'm a bit at loss, what to do. ;(

berak gravatar imageberak ( 2015-07-19 02:14:04 -0600 )edit

Yes, I use SerializationUtils.serialize(data) to convert to byte[]. But I got Mat data type is not compatible again at : m.put(0,0,data ); when I pass float[] to this fucntion:

MatOfKeyPoint mat = new MatOfkeyPoint(); ...... mat.put(0,0,data);

ndk076 gravatar imagendk076 ( 2015-07-19 03:36:33 -0600 )edit

yes true, new MatOfkeyPoint() does not allocate any data, so you can't put() anything (whatever type)

MatOfKeyPoint m = new MatOfKeyPoint(
    new KeyPoint(3,3,3),
    new KeyPoint(4,4,4)
);
// save for later use (should go into your db as well !)
int _rows = m.rows();
int _cols = m.cols();
int _type = m.type();

float[] data = new float[_rows * 7]; // special case for MatOfKeyPoint !
m.get(0,0, data );

MatOfKeyPoint m2 = new MatOfKeyPoint();
m2.create(_rows,_cols,_type); //from db
m2.put(0,0,data); // from db
berak gravatar imageberak ( 2015-07-19 04:02:04 -0600 )edit

Great!!! Thanks for your support.

ndk076 gravatar imagendk076 ( 2015-07-19 07:03:59 -0600 )edit