how to view an image saved in variable in MATLAB? - image

Multiple images are saved to variables, and I would like to view them and save them. I loaded the .mat file into MATLAB, and variables appeared in my workspace e.g. a,b,c,d; all have images stored in them. I'd like to access an image from "a".
Tried: imagesc(a,:,:,imagenumber) but get Error using ==> imageDisplayParsePVPairs at 72
Invalid input arguments.
What am I doing wrong?

imagesc should work, it all depends on what your variables size are and how you write the call to the function...
i.e.
a = eye(100,100);
imagesc(a); colormap gray
works fine;
if
a = rand(100,100,100);
imagesc(a(1,:,:));
or if a is an rgb image, a(width,height,3), then use imshow as proposed by Romeo

Try to use imshow function from Image Processing Toolbox:
imshow(a);

the syntax is wrong. If is a single image you should write
imagesc(a);
if is a (I'm assuming) RGB image
imagesc(a); colormap gray;
if grayscale.
If there are multiple images within the same variable you should use
imagesc(a(:,:,:,imagenumber))
for a RGB image
imagesc(a(:,:,imagenumber)); colormap gray;
for a grayscale

Related

MATLAB Image Processing: Getting HSV components from a colored image

I am trying to get the hue, saturation and value images from an image (originally colored). I converted the image using the rgb2hsv() function but I am confused on how to get the component images.
As Matlab mentions, the output of rgb2hsv is returned as an m-by-n-by-3 image array whose three planes contain the hue, saturation, and value components of the image.Therefore you can simply get these components as:
hsv_image = rgb2hsv(rgb_image);
hue_component= hsv_image(:,:,1);
saturation_component= hsv_image(:,:,2);
value _component= hsv_image(:,:,3);

Saving grayscale image as it appears in jet colormap

I have grayscale satellite image which is processed from spectral data (band classifications). If i use jet colormap in imshow it will show absolute colormapped image. But if i try to imwrite in particular place it is saved like a bluish image. I saw one example in matlab central, but i didnt get. can anyone help me to write my image with colorscaled image.
Matlab central link: http://www.mathworks.in/matlabcentral/answers/25026-saving-grayscale-image-as-it-appears-in-jet-colormap-of-imagesc
there accepted answer link is : http://www.mathworks.com/matlabcentral/fileexchange/7943
I have tried many times, this will show colormaped images in plots (imshow) they didnt write anywhere with colormaped. Now i want to write my image with colormaped.
example code:
I= imread('image path');
imshow(I,'colormap',jet);
imwrite(I,'path','jpg'); /not working
or
imwrite(I,jet,'path','jpg'); /not working
Please help to solve this issue.
When you use imshow the colormap is always adjusted to the range of values in your image. imwrite however assumes your image has a value range of [0,1] if you are using single or double data types. Try to scale your image to the range [0,1] before saving.
If you provide a colormap in the call to imwrite, MATLAB assumes you are using an indexed image. Thus you will have to convert the image to the indexed format first. The following snippet worked for a test image I of mine:
% scale to [0,1]
I = I - min(I(:));
I = I ./ max(I(:));
% Create indexed image
[J,~] = gray2ind(I);
% Save image
imwrite(J,jet,'path','jpg');
Solution by hbaderts worked well for me, but later I found out that some images were still scaled slightly different way from imshow.
However, I might found a reason of an original problem. Just after Matlab starts, its default colormaps (including 'jet') are set to 64 colors (64x3). Then, if any image is shown with a colormap, for example if imshow('cameraman.tif'), colormap('jet') is executed, all default colormaps become 256x3 (can be verified with jetMap=jet; before and after). Then it might happen that an image was written with a colormap different from the one applied to image figure (for example, if a figure called after imwrite).
Finally I found this solution (no image pre-scaling needed):
% Create indexed image, explicitly using 256 colors
imInd=gray2ind(im,256);
% Convert indexed image to RGB using 256-colors jet map
jetRGB=ind2rgb(imInd,jet(256));
% Save image
imwrite(jetRGB,'jet.png');
The images I used have the same color scale now, both the saved one and the one shown in figure.

Writing images using imwrite- Getting white images

I am writing a function that generates series of images. I am using the imwrite function to write each image to a file:
Ecc=sqrt(real(E(:,:,1)).^2+real(E(:,:,2)).^2+real(E(:,:,3)).^2+imag(E(:,:,1)).^2+imag(E(:,:,2)).^2+imag(E(:,:,3)).^2);
clf
Q=imagesc(nx/rad,ny/rad,Ecc);
if i==1
cl=caxis;
else
caxis(cl)
end
imwrite(Q,['Frame-',num2str(i),'.tif'],'tif');
But I am not getting the images. The files are generated just fine, but they are just white images with dimension 1x1. Any help please?
Thank you
Use imwrite on Ecc instead of Q. The output of imagesc (as I recall) is a handle to the figure, which is not what you want to write out. Write out Ecc instead.
Adding to what user3817401 has written.
Completly white images can result from data not being scaled prior to being sent to imwrite. Consider following:
Ecc = (Ecc - min(min(Ecc))) / (max(max(Ecc)) - min(min(Ecc)));
promply before imwrite. This will guarantee, that the image is in range 0-1 and should solve the problem.
The function imagesc returns a handle (you store it as Q), not scaled image data. Then, the function imwrite is interpreting Q as an image. Because it is a handle, it is just 1x1 and it's value is not meaningful as an image. Try scaling Ecc as desired and then writing that instead.

How to Save an image in matlab

I use the below command to display the image
imshow(img,[]);
when i use the following command to save the image it is saved as an empty white page
imsave;
how to save the image in this case any command would do
You are probably running into an issue with matrix type and range. If img is type double it needs to be scaled between 0 and 1.
A common issue is to load an image in uint8 (scaled between 0 and 255), convert to double in order to do some processing on it, without scaling, and then try and save it out. When you do that, MATLAB tries to convert back to uint8, and any values in the image outside the [0 1] range are clipped. On many images this means that the file comes out all white.
To get around this, use functions like im2double and im2uint8 rather than just double or uint8 when converting images.
Try at the command line the difference between:
img = imread('pout.tif');
img = double(img);
imshow(img,[]);
imsave;
and
img = imread('pout.tif');
img = im2double(img);
imshow(img,[]);
imsave;
Convert image data into an actual image and try again:
h = image(img); %Convert to object
imsave(h); %Save image object
Notice that if you close the figure window generated by image(), the object is deleted and the handle has will point to nothing. Though this may be beyond of what you are asking for.
Hope this adjustment solved your problem
First convert the image to rgb using
img1=label2rgb(img);
then again convert the image into an gray image using
img2=rgb2gray(img1);
then u can use imshow to show the image and save it using imsave
imshow(img2);
imsave();

gray levels are changed after using imsave function

I used these codes to produce an RGB image with gray levels between 50 and 170.
a='C:\Users\sepideh\Desktop\IP_abadpour\S45C-113050518040.jpg';
b=imread(a);
b=b+50;
b(b>170)=170;
and you'll see when I call functions max and min, it is proved that the gray levels are between 50 and 170.
max(max(max(b)))
ans =
170
min(min(min(b)))
ans =
50
then I used imshow and imsave functions to save the image with the name "50to170"
c=imshow(b);
d=imsave(c);
Now I read the written image in this way:
a='C:\Users\sepideh\Desktop\IP_abadpour\50to170.jpg';
b=imread(a);
This time when I call max and min functions,I see:
max(max(max(b)))
ans =
235
min(min(min(b)))
ans =
16
I mean it seems that gray levels have been changed after using imshow and imsave functions!
Why does it happen?
Is it because of the format (.jpg) that I'm using when employing imsave function?
Instead of using imsave, use imwrite
b=imread(a);
b=b+50;
b(b>170)=170;
imwrite(b,'50to170.png','png')
Notice that I am saving it as a png file instead of a jpg to prevent compression. Bitmap also saves it without compression.
This method is a more direct way to save raw image matrices than using imshow and imsave.
If you want the same functionality of imsave (selecting where the file goes) check out "uiputfile" to get file name and location.
I tested it.
1.First of all you can't use imsave(b) because the function imsave expects its first input argument to be a valid handle to a single graphics object.So first you should show the image in form c=imshow(b) and then use c as an input for function imsave.
2.If you want to save the image without showing it first use function "imwrite" and pass b as the first input argument to it.
Note that if you don't want gray levels be changed after saving, you should use 'bmp' as the save format not 'jpg'.
Because 'jpg' does not support indexed images and "imwrite" converts indexed images to RGB before writing data to JPEG files so the gray levels might be changed.It's not related to the contrast stretching when showing the image in matlab.It's related to the format used to save the image.

Resources