Reading a 16 bit RAW image from Grasshopper3 (Point Grey Research)
Show older comments
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
Guillaume
on 22 Jan 2015
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
Answers (3)
Guillaume
on 21 Jan 2015
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
Image Analyst
on 22 Jan 2015
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.
Guillaume
on 22 Jan 2015
Oh! I missed that.
Image Analyst
on 20 Jan 2015
0 votes
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
Mathews John
on 21 Jan 2015
Edited: Mathews John
on 21 Jan 2015
Image Analyst
on 21 Jan 2015
Is there any header and do you know the format/layout of it? Or at least how many bytes it is?
Image Analyst
on 21 Jan 2015
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
Mathews John
on 21 Jan 2015
Image Analyst
on 21 Jan 2015
Maybe try
ima = fread(fin, [col row],'*uint16');
Mathews John
on 21 Jan 2015
Image Analyst
on 21 Jan 2015
Well, what can I say? Just extract/crop out the stuff you need and throw away the stuff you don't need.
Mathews John
on 21 Jan 2015
Witek Jachimczyk
on 21 Jan 2015
0 votes
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
Categories
Find more on Point Grey Hardware in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!