Raw image file conversion in MATLAB
32 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
Hello All,
I am able to read in raw .CR2 image into MATLAB using imread function. I need this file to perform further calculations as a part of my project. But I am curios to know if this .CR2 file is converted to some other format internally by MATLAB and also if all the resolution is restored.
I read few articles on how to read raw images in MATLAB, which speak about converting raw files into TIFF or DNG format and then reading them into MATLAB, but I want to know how the conversion is made inside MATLAB as I could read .CR2 file without any error.
Also imfinfo(filename.CR2) throws an error.
Please help
0 commentaires
Réponses (3)
Steve Eddins
le 15 Mai 2019
Steffen,
I have seen your tech support case escalation, and I have looked at the file you sent to MathWorks.
The MATLAB function imread never compresses image data when reading image files.
Here's everything I was able to find out about your CR2 file.
The file is (mostly) in TIFF format. It contains 4 IFDs (Image File Directories).
The image described by IFD #1 is 4480x6720, 3 samples per pixel, stored in the file using OJPEG compression (indicated by TIFF compression type 6). OJEG stands for "old-style JPEG compression" and has been considered obsolete since TIFF Technical Note 2, 1995. The first image can be read using:
A = imread('IMG_0042.CR2',1);
When displaying it using imshow, a star field is visible. This is the image you get when you call imread using only one input argument, like this:
A = imread('IMG_0042.CR2');
The pixel data for image #1 uses 1,313,151 bytes in the file, for an average of 0.044 bits per pixel, suggesting the data is heavily compressed.
IFD #2 is not a valid Image File Directory. It contains only two tags: a JPEG Interchange Format tag, and a JPEG Interchange Format Length tag. Both are obsolete.
The image described by IFD #3 is small, only 284x430. It is uncompressed (TIFF compression type = 1). The image from IFD #3 can be read using:
C = imread('IMG_0042.CR2',3);
Displaying it requires scaling, however, because the pixel data type is uint16 and the maximum pixel value is much less than 65535.
The image described by IFD #4 is puzzling. It is 4544x6880, with old-style JPEG compression (TIFF compression type = 6). It has four proprietary vendor tags used for Canon CR2 files. The pixel data uses 29,740,944 bytes in the file. However, reading it in using:
D = imread('IMG_0042.CR2',4);
produces an all-green image, with the red channel pixels = 0, the green channel pixels = 135, and the blue channel pixels = 0.
I speculate that compression type 6 (OJPEG) stored in the file is actually a lie. Based on the number of bytes used in the file to store this image, I guess that it is losslessly compressed using some proprietary scheme.
1 commentaire
Steve Eddins
le 15 Mai 2019
From other information I have seen online, it seems likely that the image in IFD #4 in your file, Steffen, is compressed using a variant of lossless JPEG. This is not one of the TIFF compression types, and reading a nonstandard TIFF file such as this is not supported by MATLAB.
Also, the pixel data in IFD #4 is likely to be raw sensor data that requires some kind of vendor-specific algorithm to convert to RGB.
Image Analyst
le 12 Jan 2017
Once it's in MATLAB, at least by imread(), it's simply an array. It doesn't have a format. It only has a format while on disk. Now other readers may have a format and return something like s structure with the pixel data and other stuff like headers.
Strangely you said you read in a .CR2 file with imread() but imread() doesn't recognize that format. It recognizes these:
>> 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
2 commentaires
Image Analyst
le 12 Jan 2017
If you attach your .cr2 file I could try it. I'd expect to fail. If you want to know the internals of imread you can try
>> edit imread.m
or else ask the Mathworks. Reading .CR2 files would be an undocumented feature since the help does not mention it as being able to read that format. Nut again, once they're in MATLAB, it's simply an array without a format.
Steffen B. Petersen
le 11 Mai 2019
Hi 'Deep P'
The raw CR2 files (from Canon are modified TIFF files. imread recognizes the tiff format and is happy reading them.
One problem remains though - on my platform the displayed file in MATLAB has clearly been exposed to compression. I am currently awaiting Matkworks answer on this observation.
0 commentaires
Voir également
Catégories
En savoir plus sur Convert Image Type dans Help Center et File Exchange
Produits
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!