Ask Your Question
0

Read 16bit binary file in Java

asked 2014-03-28 06:29:54 -0600

reem gravatar image

updated 2014-03-28 06:34:14 -0600

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

edit retag flag offensive close merge delete

Comments

  • if you want a 16bit mat, use CvType.CV_16SC1 or CvType.CV_16UC1
  • imho, you want (unsigned) short[], not char[] (same size, yet different semantics)
  • don't copy it to an int[] array if you want 16 bits
  • Mat.create(rows,cols,type); // you got w/h wrong
berak gravatar imageberak ( 2014-03-28 06:37:30 -0600 )edit

but if I do it like this, I get an empty map:

Mat imgMat = Highgui.imread("a.raw",0); imgMat.convertTo(imgMat, CvType.CV_16UC1); System.out.println(imgMat.size());

And I can't jump over the header

reem gravatar imagereem ( 2014-03-28 07:00:47 -0600 )edit

imread can't read .raw files, so it returns an empty Mat

berak gravatar imageberak ( 2014-03-28 07:02:23 -0600 )edit

that's why I was reading it first in an byte-array. than I convert it to an int-array. the values of the int-array are right. all I want to do is to put my int-array to the matrix.

is this not possible? I rewrite a python script wich do exactly the same.

f = open( filename, 'rb' )

f.seek( raw.headerlen )

data = np.fromstring( f.read(), dtype=np.uint16 )

f.close()

img = cv.CreateMat( raw.width, raw.height, cv.CV_16UC1 )

cv.SetData( img, data )
reem gravatar imagereem ( 2014-03-28 07:11:52 -0600 )edit

oh, python's (outdated) cv api is using the old c'ish CvMat and IplImage. (it will be gone in the next version, so use cv2 in python instead)

not the same as cv::Mat (which is wrapped in java)

berak gravatar imageberak ( 2014-03-28 07:29:18 -0600 )edit

Sorry I really dont get it. Why is my matrix empty?

Mat.put(0,0,myArray)

-->returns an empty map and I really don't know why. I debugged it and everything seems ok.

reem gravatar imagereem ( 2014-03-28 07:39:18 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2014-04-03 10:55:54 -0600

reem gravatar image

I found the solution as following:

  • matrix was emtpy because the value of the cols wasn't set
  • The transformation of an 16 bit - raw file with 0-65k greyvalues into a 24bit - RGB JPEG works fine

If someone has any idea how I could manage the in-reading of an 16 bit unsigned int into java without using an char-Array, please tell me. The transformation is done with the following code:

img16u.convertTo(imgtmp, CvType.CV_8UC3, (double) 255.0
            / (maxVal - minVal), (double) -minVal * 255.
            / (maxVal - minVal));

Regards, reem

edit flag offensive delete link more

Question Tools

Stats

Asked: 2014-03-28 06:29:54 -0600

Seen: 1,408 times

Last updated: Apr 03 '14