Reading a 16 bit RAW image from Grasshopper3 (Point Grey Research)

Hi,
I have been trying to read a 16 bit RAW image, captured using a Grasshopper3 Camera (Point Grey Research), in MATLAB. All images were captured using the Flycapture2 software. The image itself is 1384x1036 and has a memory stride of 2768 (since it's a 16 bit image). After a lot of reading in various parts of the web, I figured that RAW images are supposed to be considered as a binary stream of data. So with the following code i should be able to read the RAW image:
row = 1036; col = 1384;
fin = fopen('raw13.raw','r');
ima = fread(fin, [col*2 row],'uint8');
temp = zeros(col,row);
j=1;
for i=1:2:col*2-1
temp(j,:) = ima(i,:) + ima(i+1,:)*2^8; %The first element is the lower 8bits and the second element is the higher 8bits
j = j+1;
end
imshow(temp',[0 2^16-1])
This is not creating the image I want and there seems to be a lot of overlap and a lot of data missing in the reconstructed image. A I don't think I am doing anything wrong. Is there anyway of knowing what the exact size of the RAW file would be and does anyone else have previous experience with this camera? Would be helpful if someone could help with the file format.
Thank You.

1 Comment

So, from your comment, the problem is that there's a bug in the manufacturer's software and your code is fine.
I just want to point out that you could simplify it a lot by reading the image straight as uint16:
row = 1036; col = 1384;
fin = fopen('raw13.raw','r', 'l'); %'l' optional on little-endian platform (windows)
ima = fread(fin, [col row], '*uint16'); %read as uint16 and store as uint16
fclose(fin);
imshow(fin); %because ima is uint16, you don't need to specify the range

Sign in to comment.

Answers (3)

I've been able to read RAW images successfully using multibandread. Now, one RAW image format is different from another RAW image format, so there's no guarantee it will work for you, but give it a try.
However, are you sure that the image is truly 16 bits. A quick search on the web, would indicate your camera is either 12 or 14 bits. In which case, it's possible that reading as 16 bits is wrong. Possibly try:
img = multibandread('filename', [1036 1384 1], 'ubit12=>uint16', 'bsq', 'ieee-be')
Ultimately, you need to refer to either the software documentation or the camera documentation to know the details of their RAW format. The content of the file is supposed to be a dump of the sensor data without any processing.

2 Comments

He said the manufacturer admitted that the raw files being written out in the current version are corrupt, but fortunately the pgm files are okay.

Sign in to comment.

Why not just save the image as a standard format like PNG? As far as I know all run of the mill machine vision cameras will let you save your images in a variety of standard formats. Are you absolutely sure raw is the ONLY format? I got some images from someone who used a Point Grey camera and they were in TIFF format.
Can you attach one of the raw images?

8 Comments

I need the RAW format for some further processing prior to this. I have already made a code in MATLAB to save images as a TIFF from the camera, but I really want the RAW format. You can download the raw file from this link :
Is there any header and do you know the format/layout of it? Or at least how many bytes it is?
The link Witek gave says "The Portable Grey Map (.pgm) file format can be used to correctly save 16bpp images." so why not use that? This is one that imread() can understand:
>> imformats
EXT ISA INFO READ WRITE ALPHA DESCRIPTION
-----------------------------------------------------------------------------------------
bmp isbmp imbmpinfo readbmp writebmp 0 Windows Bitmap
cur iscur imcurinfo readcur 1 Windows Cursor resources
fts fits isfits imfitsinfo readfits 0 Flexible Image Transport System
gif isgif imgifinfo readgif writegif 0 Graphics Interchange Format
hdf ishdf imhdfinfo readhdf writehdf 0 Hierarchical Data Format
ico isico imicoinfo readico 1 Windows Icon resources
j2c j2k isjp2 imjp2info readjp2 writej2c 0 JPEG 2000 (raw codestream)
jp2 isjp2 imjp2info readjp2 writejp2 0 JPEG 2000 (Part 1)
jpf jpx isjp2 imjp2info readjp2 0 JPEG 2000 (Part 2)
jpg jpeg isjpg imjpginfo readjpg writejpg 0 Joint Photographic Experts Group
pbm ispbm impnminfo readpnm writepnm 0 Portable Bitmap
pcx ispcx impcxinfo readpcx writepcx 0 Windows Paintbrush
pgm ispgm impnminfo readpnm writepnm 0 Portable Graymap
png ispng impnginfo readpng writepng 1 Portable Network Graphics
pnm ispnm impnminfo readpnm writepnm 0 Portable Any Map
ppm isppm impnminfo readpnm writepnm 0 Portable Pixmap
ras isras imrasinfo readras writeras 1 Sun Raster
tif tiff istif imtifinfo readtif writetif 0 Tagged Image File Format
xwd isxwd imxwdinfo readxwd writexwd 0 X Window Dump
I will look into .pgm then. I contacted tech support and they said the .raw files doesn't have any headers but will be 14bit data (from the ADC) and a 2 bit padding. With this new information, I'll try tweaking my own code a bit and see what happens
Maybe try
ima = fread(fin, [col row],'*uint16');
That is also not working. I tried the .pgm and that's fine. Another thing that I noticed is I am able to read much more than what should be there. Technically there should be 1036*1384*2 bytes of uint8 data. However, I am able to read much more than that. I don't understand why this is so.
Well, what can I say? Just extract/crop out the stuff you need and throw away the stuff you don't need.
Hey! Thanks for all the help. I talked to tech support thoroughly. It seems the raw file produced from the version of the software I am using is um... corrupted I suppose. The code I am using was perfect and worked perfectly when I got the raw file from a previous version.
Thank You so much anyways :)

Sign in to comment.

Indeed, knowing the format precisely is the key. I found a little bit about it here:
but it still needs to be understood thoroughly. If it's headerless, you might be able to use vision.BinaryFileReader from the Computer Vision System toolbox:
HTH,
Witek

Asked:

on 20 Jan 2015

Commented:

on 22 Jan 2015

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!