How to convert PPM images to JPG in Matlab? - matlab

I have some PPM images (stereo) that I read with imread() and I want to save the same images in JPEG with different Quality factors.
Here is my code.
%Read PPM image
L = imread(filename_L);
%Create JPEG Q85 from PPM
filename_L85 = strcat(filename_L,'_ppm_to_jpeg.jpg');
imwrite(L,filename_L85,'JPEG','Quality',85);
And here the error I get.
Error using imwrite>parse_inputs (line 528)
The colormap should have three columns.
Error in imwrite (line 418)
[data, map, filename, format, paramPairs] = parse_inputs(varargin{:});
Error in testFinale (line 75)
imwrite(L,filename_L85,'JPEG','Quality',85);
How can I write JPEG images previously read in PPM format?
Thanks

Could it be that is just has to do with your case of 'JPEG', the documentation of imwrite specifies parameters for file type as lowercase.
Apart from that you might not even need it as the file type is derived from the extension which in this case is set explicitly to .jpg already.
So you might either go for:
imwrite(L,filename_L85,'jpeg','Quality',85);
or perhaps even easier:
imwrite(L,filename_L85,'Quality',85);

Related

How to write slice by slice from nifti images in matlab?

I read nifti images by using matlab toolbox but how we can write slice by slice into another format like .jpg or png?
I try this like:
V=niftiread('Brats17_2013_2_1_flair.nii.gz');
imshow(V(:,:,1),[]);
imwrite(V,'test.jpg')
Error using imwrite (line 442)
Cannot write signed integer data to a JPEG file.
imwrite((V(:,:,1),[]),'test.jpg');
imwrite((V(:,:,1),[]),'test.jpg');
↑
Error: Expression or statement is incorrect--possibly unbalanced (, {, or [.
just save the picture by "saveas" command. it worked correctly.
h = imshow(V(:,:,1),[]);
saveas (h,'test.jpg');
Good luck

Error: The compressed pixel data is missing item delimiters.?

I am working with few Dicom files and when i try to use dicomread('filename.dcm') in MATLAB it gives the following error:
Error using dicomread>processOffsetTable (line 943)
The compressed pixel data is missing item delimiters.
Error in dicomread>processEncapsulatedPixels (line 858)
[offsetTable, offset] = processOffsetTable(metadata);
Error in dicomread>newDicomread (line 232)
X = processEncapsulatedPixels(metadata, frames);
Error in dicomread (line 86)
[X, map, alpha, overlays] = newDicomread(msgname, frames, useVRHeuristic);
I can view this same file in dicom viewing Softwares like onis, di com viewer, Sante Dicom etc.., but when i use dicomread I cannot see see them and get this error
I have so many images of this same format and cannot start from the beginning again, Is there any way I can use this file and view it.
Refer this online help.
It is common in DICOM world that not all data sets fully comply with DICOM. Most applications (you mentioned in your question) handle the non-compliant part with assumptions and workarounds based on experience and imagination.
Try setting TF to false to read these files.
Also note the list of supported transfer syntax:
Little-endian, implicit VR, uncompressed
Little-endian, explicit VR, uncompressed
Big-endian, explicit VR, uncompressed
JPEG (lossy or lossless)
JPEG2000 (lossy or lossless)
Run-length Encoding (RLE)
GE implicit VR, LE with uncompressed BE pixels (1.2.840.113619.5.2)
Check your input image is compressed with one of the above.

Big tiff read and view in Matlab

I have downloaded a btf file (big tiff) from the links below, how can I read it and "imshow" it? is there a way to convert it to tiff format as the btf is not that common?
Link:
https://drive.google.com/file/d/0ByhuP_NuuARtSW9aeTdPUTlRdWM/view?usp=drive_web
http://www.photomacrography.net/forum/viewtopic.php?t=28990&sid=cca737a2e0bc7ea3e2e41f0d6e75f5a9
I used this code:
t = Tiff('d:/Image_687.btf','w8');
imageData = read(t);
and got this error:
Error using tifflib
Unable to retrieve PhotometricInterpretation.
Error in Tiff/getTag (line 838)
tagValue = tifflib('getField',obj.FileID,Tiff.TagID.(tagId));
Error in Tiff/read (line 1487)
photo = obj.getTag('Photometric');
Error in Untitled2 (line 2)
imageData = read(t);
The real issue with your code is the second parameter that you have passed to Tiff. As the documentation states, the second parameter indicates in what mode to open the file. You have specified w8 which the documentation states is:
open TIFF file for writing a BigTIFF file; discard existing contents.
This means that it is deleting your image before you even start! If you want to use the Tiff class, you'll want to either use no second parameter or the r parameter to open the file for reading.
t = Tiff('Image_687.btf');
t = Tiff('Image_687.btf', 'r');
That being said, in general it is better to try to load it with a higher level function such as imread. The Tiff class is a much lower-level function that can be a little harder to manipulate but may provide some needed specialty functionality.
im = imread('Image_687.btf');
size(im)
3072 4080 3
I had to do a little manipulation for display because the RGB values weren't between 0 and 255
im = double(im);
im = uint8(255 * im ./ max(im(:)));
imshow(im);

cannot import tif file into matlab

i am trying to import a .tif image into matlab with the following code
>> aa = imread('house.tif');
i get the error
Error using rtifc
TIFF library error: '_TIFFVSetField: C:\Users\user\Documents\MATLAB\house.tif: Null count
for "Tag 34022" (type 1, writecount -3, passcount 1).'.
Error in readtif (line 49)
[X, map, details] = rtifc(args);
Error in imread (line 434)
[X, map] = feval(fmt_s.read, filename, extraArgs{:});
as i am using matlab for the first time in my life i really have no idea what this error means. Please help is required in this matter.
MATLAB R2012b has a bug and it cannot read TIFF files properly. More information can be found here: http://www.mathworks.com/matlabcentral/newsreader/view_thread/326232
Probably Matlab does not support the specific type of tif. In Matlab's defence, tif is not an easy file-format to read. It supports plenty of compression schemes, multiple pages and who knows what. I'd convert the tif to png and go with that.
Update: A quick Google search revealed that "rtifc" is a Matlab mex-wrapper around libtiff. Your error appears to come from libtiff. If the latter can't read it, your tif will probably be problematic for a lot of other applications too.
Another thing you could try is use the implementation tiffread from François Nedelec's group at EMBL. http://www.embl.de/ExternalInfo/nedelec/misc/matlab/tiffread29.m. It's heavily used by biology folks all over the world. I've been using it for many years.

Image processing using MATLABR2010a

I tried to read an image and display it but i faced an error and i didn't understand it.can any one help me please, note that i use MATLAB R2010a, and the display below is the type of error.
>> imread('tas.jpg');
>> imshow('tas.jpg');
??? Attempt to call constructor image with incorrect letter case.
**Error in ==> basicImageDisplay at 9
hh = image(xdata,ydata,cdata, ...
Error in ==> imshow at 246
hh = basicImageDisplay(fig_handle,ax_handle,...**
I = imread('tas.jpg');
imshow(I);
The imread function reads the file and converts it to a RGB matrix of pixels. This is stored on the variable I. Then, you can call imshow passing this RGB matrix as a parameter ;)
edit you can call imshow with the filename as well, but it's not that useful because it does not return the matrix you will later use for processing. And as the error is thrown only on imshow, I'm guessing the imread function, for some reason, is working.
If not, just double check if the image is on the actual directory or in a directory on the path, or if it is not corrupted.
This might be the reason (from the below thread):
Reason: "current directory folder name is match with built-in function in matlab library and gives the error - Attempt to call constructor image with incorrect letter case".
Solution: change the folder name with unique name.
http://www.mathworks.com/matlabcentral/newsreader/view_thread/256922