Permute of Image in OpenCV Java Preset Or Conversion of Mat to Java array.

250 views
Skip to first unread message

ashish bhutani

unread,
Mar 2, 2016, 4:45:43 PM3/2/16
to javacpp
Hi

Is there any equivalent of permute (http://in.mathworks.com/help/matlab/ref/permute.html?requestedDomain=www.mathworks.com) for image in OpenCV Java Preset ? I am not able to find one.

My image is represented as org.bytedeco.javacpp.Mat I can use permute provided by Nd4j. But to use Nd4j, I will need to convert Mat to int[]/float[] and that too is not clear to me.

Any chance someone can help ?

I have tried below approaches for conversion of Mat to int[] but they doesn't work.


private static int[] MatToFloatArray1(Mat mat) {
org.bytedeco.javacpp.BytePointer matData = mat.data();
byte[] d = new byte[matData.capacity()];
return toIntArray(d);
}


private static int[] MatToFloatArray2(Mat mat) {
org.bytedeco.javacpp.BytePointer matData = mat.data();
IntBuffer intBuffer = matData.asBuffer().asIntBuffer();
return intBuffer.array();

}
private static int[] toIntArray(byte[] d) {
IntBuffer intBuf =
ByteBuffer.wrap(d)
.order(ByteOrder.BIG_ENDIAN)
.asIntBuffer();
int[] array = new int[intBuf.remaining()];
return array;


}

Samuel Audet

unread,
Mar 3, 2016, 12:02:57 AM3/3/16
to javacpp

There's nothing that I know of for that in OpenCV, I'm afraid. We can use Mat.createBuffer() to access the raw data and use that with ND4J though.

Samuel

2016/03/03 6:45 "ashish bhutani" <bhutani....@gmail.com>:
--
You received this message because you are subscribed to the Google Groups "javacpp" group.
To unsubscribe from this group and stop receiving emails from it, send an email to javacpp-proje...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

ashish bhutani

unread,
Mar 3, 2016, 2:45:56 AM3/3/16
to javacpp...@googlegroups.com
Thanks Samuel.

I was trying createBuffer() to access the raw data but could not make it work. Below is the code.

ByteBuffer buffer =  resizedImage.<ByteBuffer>createBuffer();
byte[] byteArray = new byte[buffer.remaining()];
int[] intArray = toIntArray(d);


private static int[] toIntArray(byte[] d) {
IntBuffer intBuf = ByteBuffer.wrap(d)
.order(ByteOrder.BIG_ENDIAN)
.asIntBuffer();
int[] array = new int[intBuf.remaining()];
return array;

}

Arrays byteArray and intArray has all entries as 0. 
Any possibility you can provide a help here ? Thanks. 

One of the comment there is to use mixChannel function to achieve permute functionality. But I can't find any mixChannel equivalent in Java OPenCV preset.

Samuel Audet

unread,
Mar 3, 2016, 5:22:16 AM3/3/16
to javacpp

You'll probably need to convert the bytes to ints here...

2016/03/03 16:45 "ashish bhutani" <bhutani....@gmail.com>:
Reply all
Reply to author
Forward
0 new messages