Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Read 16bit binary file in Java

Hello everyone, I'm trying now for about three days to solve the following problem. I Just want to read a binary file with Java and put all the values to an Opencv-Mat-Objekt. I know that all the datavalues in my input-image are greyvalues from 0-65k. My code looks like:

    byte[] rawFile = Files.readAllBytes(Paths.get("a.raw"));

// header is cut off, only date in file left
byte[] data = Arrays.copyOfRange(rawFile, 2048, rawFile.length);

// bytearray --> char array (16bit unsigned)
CharBuffer cBuf = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN)
            .asCharBuffer();
char[] cArray = new char[cBuf.remaining()];
cBuf.get(cArray);

int[] finalData = new int[cArray.length];
// cast  all char values to int-values
for (int i = 0; i < cArray.length; i++) {
   finalData[i] = (int) cArray[i];
    }

So far so good. My values look good in the finalData-Array. But whem I'm tryin to put it in an OpenCv-Mat-Object, the matrix is emtpy. I add the finalData-array like this to the matrix:

    Mat img16u = new Mat();
    img16u.create(raw.getWidth(), raw.getHeight(), CvType.makeType(CvType.CV_32SC1, 1));
    img16u.put(0, 0, finalData);

But I'm not sure if the CvType is right or not.

Maybe someone has any Idea how to solve this.

Regards, reem

click to hide/show revision 2
retagged

updated 2014-03-28 06:30:36 -0600

berak gravatar image

Read 16bit binary file in Java

Hello everyone, I'm trying now for about three days to solve the following problem. I Just want to read a binary file with Java and put all the values to an Opencv-Mat-Objekt. I know that all the datavalues in my input-image are greyvalues from 0-65k. My code looks like:

    byte[] rawFile = Files.readAllBytes(Paths.get("a.raw"));

// header is cut off, only date in file left
byte[] data = Arrays.copyOfRange(rawFile, 2048, rawFile.length);

// bytearray --> char array (16bit unsigned)
CharBuffer cBuf = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN)
            .asCharBuffer();
char[] cArray = new char[cBuf.remaining()];
cBuf.get(cArray);

int[] finalData = new int[cArray.length];
// cast  all char values to int-values
for (int i = 0; i < cArray.length; i++) {
   finalData[i] = (int) cArray[i];
    }

So far so good. My values look good in the finalData-Array. But whem I'm tryin to put it in an OpenCv-Mat-Object, the matrix is emtpy. I add the finalData-array like this to the matrix:

    Mat img16u = new Mat();
    img16u.create(raw.getWidth(), raw.getHeight(), CvType.makeType(CvType.CV_32SC1, 1));
    img16u.put(0, 0, finalData);

But I'm not sure if the CvType is right or not.

Maybe someone has any Idea how to solve this.

Regards, reem

Read 16bit binary file in Java

Hello everyone, I'm trying now for about three days to solve the following problem. I Just want to read a binary file with Java and put all the values to an Opencv-Mat-Objekt. I know that all the datavalues in my input-image are greyvalues from 0-65k. My code looks like:

    byte[] rawFile = Files.readAllBytes(Paths.get("a.raw"));

// header is cut off, only date in file left
byte[] data = Arrays.copyOfRange(rawFile, 2048, rawFile.length);

// bytearray --> char array (16bit unsigned)
CharBuffer cBuf = ByteBuffer.wrap(data).order(ByteOrder.LITTLE_ENDIAN)
            .asCharBuffer();
char[] cArray = new char[cBuf.remaining()];
cBuf.get(cArray);

int[] finalData = new int[cArray.length];
// cast  all char values to int-values
for (int i = 0; i < cArray.length; i++) {
   finalData[i] = (int) cArray[i];
    }

So far so good. My values look good in the finalData-Array. But whem I'm tryin to put it in an OpenCv-Mat-Object, the matrix is emtpy. I add the finalData-array like this to the matrix:

    Mat img16u = new Mat();
    img16u.create(raw.getWidth(), raw.getHeight(), CvType.makeType(CvType.CV_32SC1, 1));
    img16u.put(0, 0, finalData);

But I'm not sure if the CvType is right or not.

Maybe someone has any Idea how to solve this.

Regards, reem