Converting depth frames to OpenCV Mat CV_16UC1 in Java on Android
I'm trying to convert a D435 depth frame to a 16U Mat in OpenCV for further processing. I don't want to lose bits through colorization. I've looked at cpp examples and have come up with code that looks right to me, but the last line here ends in an exception as if there is something wrong with the buffer - though the buffer data looks reasonable to me. Any hints or examples of how this is done successfully?
Here is a code fragment where the last line results in java.lang.UnsupportedOperationException: Mat data type is not compatible: 2:
DepthFrame depth = f.as(Extension.DEPTH_FRAME);
//put the depth frame data into a Mat for further processing
buffer = new byte[depth.getDataSize()];
depth.getData(buffer);
cvDepth = new Mat(depth.getHeight(),depth.getWidth(), CvType.CV_16UC1);
cvDepth.put(0,0,buffer); //exception here: java.lang.UnsupportedOperationException: Mat data type is not compatible: 2
-
Hi Karim. The link below has an example of a Java depth frame to CV_16U1 conversion script.
https://github.com/IntelRealSense/librealsense/issues/5185#issuecomment-550266254
-
MartyX Grover Thanks for the rapid response! This solved my issue. In context, here is the working version of the prior code fragment:
DepthFrame depth = f.as(Extension.DEPTH_FRAME);
final float depthValue = depth.getDistance(depth.getWidth()/2, depth.getHeight()/2);
deflector = depthValue;
//extract depth data
int size = depth.getDataSize();
buffer = new byte[size];
depth.getData(buffer);
//transform the buffer to make it compatible with CV_16UC1
short[] shorts = new short[size/2];
ByteBuffer.wrap(buffer).order(ByteOrder.LITTLE_ENDIAN).asShortBuffer().get(shorts);
//put the depth frame data into a Mat for further processing
cvDepth = new Mat(depth.getHeight(),depth.getWidth(), CvType.CV_16UC1);
cvDepth.put(0,0,shorts); -
You are very welcome, Karim - it's great to hear that you were successful. Thanks very much for sharing your code fragment with the RealSense community!
Please sign in to leave a comment.
Comments
3 comments