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
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
imread can't read .raw files, so it returns an empty Mat
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' )
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)
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.