MATLAB Image Processing: Getting HSV components from a colored image - 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);

Related

Matlab imread return 4 channel matrix

I do imread image in Matlab, and it returns a 4-channel image:
im: 1012x972x4 uint8.
Which format is this image? How to check its color format(RGB, CMYK, etc)? I opened it in Gimp and the color profile is simply sRGB built-in
From imread() documentation:
The return value A is an array containing the image data. If the file
contains a grayscale image, A is an M-by-N array. If the file contains
a truecolor image, A is an M-by-N-by-3 array. For TIFF files
containing color images that use the CMYK color space, A is an
M-by-N-by-4 array. See TIFF in the Format-Specific Information section
for more information.
So the answer is apparently that this image's color space is CMYK.
If you want to check for a general input, then, from the same page:
To determine which color space is used, use imfinfo to get information
about the graphics file and look at the value of the
PhotometricInterpretation field.

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.

How do I make a png with transparency appear transparent in MatLab?

I have an image with a transparent background, but when I open it in MATLAB, I get a black background. I'm overlaying it on top of a background image. How can I get this to display? I've tried using the alpha function alpha(image,0) but it sets my entire image to 0. Is it possible for me to set the alpha of individual pixels to be equal to 0? That way I can run each pixel through a loop.
I'm not sure if this helps, but when I run a imfinfo('ryu1.png'), I get :
...
Transparency = 'alpha'
SimpleTransparencyData = []
...
You can read in your image using imread. However, you need to specify additional output parameters if you want to grab the alpha channel. You need to call it like this:
[im, map, alpha] = imread('ryu1.png');
im is your image read in, map is the colour map which we will ignore, but alpha contains the transparency information that you want. First call imshow and record the handle to the image, then set your transparency with the alpha channel using the set command. In other words:
[im, map, alpha] = imread('ryu1.png');
f = imshow(im);
set(f, 'AlphaData', alpha);
This should make the figure with the transparency intact.
Addition (Thanks to Yvon)
Supposing you already have a background image loaded into MATLAB. If you want to blend these two together, you need to do some alpha matting. You use the alpha channel and mix the two together. In other words, supposing your background image is stored in img_background and img_overlay is the image you want to go on top of the background, do this:
alphaMask = im2double(alpha); %// To make between 0 and 1
img_composite = im2uint8(double(img_background).*(1-alphaMask) + double(img_overlay).*alphaMask);
The first step is necessary as the alpha map that is loaded in will be the same type as the input image, which is usually uint8. We need to convert this to a double image such that it goes in between 0 and 1, and im2double is perfect to do this. The second line converts each of the images to double precision so that we can compute this sum and in order to make the data types between the alpha mask and both images are compatible. We then convert back to uint8. You can then show this final image using imshow.

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.

how to view an image saved in variable in MATLAB?

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

Resources