Display transparent image on axes (MATLAB) - matlab

So I have this transparent image (http://imgur.com/fyqslAx) that I want to display on an axes in MATLAB with its transperancy. To do this, I used the code below, which works with other transparent png images that I have:
[A, map, alpha] = imread('fyqslAx.png');
h = imshow(A, map)
set(h, 'AlphaData', alpha);
This code however, does not seem to work with the image above. Im guessing this is because it has something to do with the image being grayscale and having a bitdepth of 1, resulting in the map and alpha having nothing in it (whereas the other png transparent images I have, have something in map and alpha). If I just use this:
A = imread('fyqslAx.png');
h = imshow(A)
A black background appears where the image should be transparent.
How do I display this http://imgur.com/fyqslAx with its transperancy on an axes?
EDIT: horchler's method worked; Thanks!!

If you run imfinfo('fyqslAx.png'), you'll see that the 'Transparency' is specified as 'simple' and the 'SimpleTransparencyData' is set to 0 (false). I can't find documentation on this, but I think that this may indicate that the alpha channel has been compressed into the image data because the the image is binary and grayscale. Effectively the image is a binary mask, either transparent or not. You can display your image like this:
A = imread('fyqslAx.png');
h = imshow(A);
set(h, 'AlphaData', A);
If you try to return a colormap and/or alpha channel from imread using extra output arguments, you'll see that both are empty.

Related

Creating a binary image

clear all;
clc;
imag = imread('286502.png');
image_binary = im2bw(imag,0.85); %converte image to binary
image_binary = not(image_binary);
figure(1);clf
imagesc(image_binary);colormap(gray)
I am using this code to genarate a binary image of an ellipse having its inside as white while outside as black.But , the problem i am facing is that whenever the foreground and background of my input ellipse is either light or dark then the binary image becomes all black or all white.
if you want to keep the gray-scale nature of our image, you may remove the line im2bw command because that flattens the gray-scale values into binary (0-1 or white/black).
if you just want to show a binary image in gray color, you can try customizing the colormap
colormap([0 0 0;0.5 0.5 0.5]);
im2bw(imag,0.85) thresholds at a fixed value, which will work for some images but not for others. I recommend that you binarize with a method such as Otsu, which determines an optimal threshold for each image individually.
image_binary = imbinarize(imag);

Convert grayscale uint8 image to RGB uint8 image

I have a code to run, which uses an original image and a mask image. The code assumes that the original image is RGB, but my original image is gray scale. This must be the result of the MATLAB whos command when I run the code:
Name Size Bytes Class Attributes
mask 308x206 63448 logical
origImg 308x206x3 190344 uint8
The mask is produced by making part of the image white and the rest is black (in a simple software like windows paint).
I want to use a gray-scale image as the origImg and produce the mask from the origImg in windows paint, but the result of the MATLAB whos command is as follows when I want to use custom photos with attributes as I said:
Name Size Bytes Class Attributes
mask 490x640x3 940800 uint8
origImg 490x640 313600 uint8
I have to convert the origImage dimension to x3 and remove the x3 from the mask, and also convert its class from unit8 to logical. In that case, I think that the code should work properly.
What should I do here in order to prepare the origImg and mask for that goal?
origImg=imread('G:\the_path\to\my_custom\image.png');
mask=imread('G:\the_path\to\my_custom\image_mask.png');
% I have to do something here to make it work.
whos;
% Rest of the code...
I am not sure if I understand you correctly.
To make a RGB image out of a gray-scale image, which still shows up as a gray-scale image, you can use
origImg = repmat(origImg,1,1,3);
which just repeats your gray-scale image for every channel of the RGB image.
For the mask, you have to do the opposite. since I don't know your image_mask.png file, I assume that it is a RGB image that uses only black and white. In this case, all three channels are the same and you could simply use one of them for the mask, doesn't matter which one:
mask = mask(:,:,1);
To convert it to logical, use
mask=logical(mask);

How to create an inverse gray scale?

I have an image with dark blue spots on a black background. I want to convert this to inverse gray scale. By inverse, I mean, I want the black ground to be white.
When I convert it to gray scale, it makes everything look black and it makes it very hard to differentiate.
Is there a way to do an inverse gray scale where the black background takes the lighter shades?
Or, another preferable option is to represent the blue as white and the black as black.
I am using img = rgb2gray(img); in MATLAB for now.
From mathworks site:
IM2 = imcomplement(IM)
Is there a way to do an inverse gray scale where the black
background takes the lighter shades?
Based on your image description I created an image sample.png:
img1 = imread('sample.png'); % Read rgb image from graphics file.
imshow(img1); % Display image.
Then, I used the imcomplement function to obtain the complement of the original image (as suggested in this answer).
img2 = imcomplement(img1); % Complement image.
imshow(img2); % Display image.
This is the result:
Or, another preferable option is to represent the blue as white and
the black as black.
In this case, the simplest option is to work with the blue channel. Now, depending on your needs, there are two approaches you can use:
Approach 1: Convert the blue channel to a binary image (B&W)
This comment suggests using the logical operation img(:,:,3) > 0, which will return a binary array of the blue channel, where every non-zero valued pixel will be mapped to 1 (white), and the rest of pixels will have a value of 0 (black).
While this approach is simple and valid, binary images have the big disadvantage of loosing intensity information. This can alter the perceptual properties of your image. Have a look at the code:
img3 = img1(:, :, 3) > 0; % Convert blue channel to binary image.
imshow(img3); % Display image.
This is the result:
Notice that the round shaped spots in the original image have become octagon shaped in the binary image, due to the loss of intensity information.
Approach 2: Convert the blue channel to grayscale image
A better approach is to use a grayscale image, because the intensity information is preserved.
The imshow function offers the imshow(I,[low high]) overload, which adjusts the color axis scaling of the grayscale image through the DisplayRange parameter.
One very cool feature of this overload, is that we can let imshow do the work for us.
From the documentation:
If you specify an empty matrix ([]), imshow uses [min(I(:)) max(I(:))]. In other words, use the minimum value in I as black, and the maximum value as white.
Have a look at the code:
img4 = img1(:, :, 3); % Extract blue channel.
imshow(img4, []); % Display image.
This is the result:
Notice that the round shape of the spots is preserved exactly as in the original image.

Converting colour image to grey scale (Matlab)

I'm having some trouble converting a tif image into grey scale so I can have a look at the pixel values. I first used the code
im = imread('etc.');
im = rgb2gray(im);
imshow(im)
but this doesn't display grey scale values - it shows an RGB colour map. I've also tried loading the colour map to then convert the map
[im,map] = imread('etc.');
but the map is empty. Any pointers appreciated - I couldn't see anything useful in the Matlab help.
Edit: imshow results
There will not be a colour map in the file if it's a rgb image - only if it's an indexed image.
imshow uses a default colour map (jet). If you want a greyscale map, you have to deliberately set colormap gray.
im = rgb2gray(imread('filename'));
imshow(im)
colormap gray
I think with the .tif format you have to use the command
[X,map] = imread('imagename.tif')
newmap = rgb2gray(map)
imshow(X,newmap)
Link to online Matlab help where's this exact example: RGB to gray

imshow() displays a white image for a grey image

I have computed an image with values between 0 and 255. When I use imageview(), the image is correctly displayed, in grey levels, but when I want to save this image or display it with imshow, I have a white image, or sometimes some black pixels here and there:
Whereas with imageview():
Can some one help me?
I think that you should use imshow(uint8(image)); on the image before displaying it.
Matlab expects images of type double to be in the 0..1 range and images that are uint8 in the 0..255 range. You can convert the range yourself (but change values in the process), do an explicit cast (and potentially loose precision) or instruct Matlab to use the minimum and maximum value found in the image matrix as the white and black value to scale to when visualising.
See the following example with an uint8 image present in Matlab:
im = imread('moon.tif');
figure; imshow(im);
figure; imshow(double(im));
figure; imshow(double(im), []);
figure; imshow(im2double(im));