How to display image with self-defined transparency from matrix in matlab? - matlab

I would like to display image directly from a matrix (not colormap). For example, I have a 100x100x3 matrix A, at position (i,j), it has color [0.5,1.0,0.8]. Using imshow(A) can directly display the image.
However, the alpha value cannot be specified. If I would like each pixel to have an additional alpha channel, for example [0.5,1.0,0.8,0.2], the imshow method complains.
Is there a way to accomplish this target?
Thank you very much.

You can use the AlphaData property of the Image returned by imshow
img = A(:,:,1:3);
alpha = A(:,:, 4);
i = imshow(img);
i.AlphaData = alpha;

Related

Matlab change a set of images to 2D grayscale but error occur

I was trying to turn a set of images to 2D grayscale image in order to do Surf feature extraction. In the set, some of the images are already in 2D scale so when I tried to run a loop.
This error message happens:
MAP must be a m x 3 array.
Here is my code:
for ii=1:numImages
img = readimage(imdsTrain,ii);
RGB = rgb2gray(img)
points = detectSURFFeatures(RGB);
[surf1, valid_points] = extractFeatures(RGB, points);
figure; imshow(RGB); hold on;
plot(valid_points.selectStrongest(10),'showOrientation',true);
title ('Ct-scan image with Surf feature')
end;
Is there any solution that can solve this problem?
Matlab's rgb2gray will fail if you give it a grayscale image (an image with only one color channel).
If you are sure that your images are either m x n X 3 or m x n x 1, you could check for the m x n x 3 case before attempting to do the rgb2gray. If your image is already a grayscale image no conversion is needed or can be done via rgb2gray.
Something like this:
img = readimage(imdsTrain,ii);
[rows, columns, colors] = size(img)
if colors > 1
RGB = rgb2gray(img)
end
Now getting the error MAP must be a m x 3 array suggests that whatever you supplied rgb2gray got interpreted as a colormap. So I would check if all your images are indeed three dimensions, as you are assumimg.

what is the right way to compute the measures for images with different color properties

I need a little help guys in Matlab in Matrix Dimensions,
I Have two images imported by imread function:
im1 = imread('1.jpg');
im2 = imread('2.jpg');
im1 is the reference image, while im2 is the Noisy image.
In the workspace window, Matlab shows the im2 Dimensions like this: 768x1024x3
while im2 displayed as: 768x1024
They are both RGB, there's no greyscale images,
In fact the second image is the a compressed image (performed compression algorithm on it ) while the first image is natural JPEG Image, untouched
and for calculating MSE/PNSR for both images, the matrix dimensions must be the same.
I Will need to transform im1 dimensions to be 3d like the first image (768x1024)
I tried this functions (squeeze, reshape) and with no success
You were on the right track with repmat. Here's the correct syntax:
im2 = repmat(im2, [1 1 3]);
This says you want 1 replicate along the first dimension, 1 replicate along the second dimension, and 3 replicates along the third dimension.
Are you sure that both are RGB images because im2 has only one channel and it looks grayscale but it can also be a colormap image in that case try
[im2, map] = imread('im2.jpg');
and see if anything is appearing in map variable. If the image is indeed colormap image, the map variable should be of size 256 X 3.
What donda has suggested is repeating the grayscale channel 3 times to make it of size 768x1024x3. Another possibility is that noisy image was created by converting RGB image to grayscale or by taking green channel of RGB image. Verify the source of the image in that case.
About PSNR computation I have a feeling that there is some problem with your code. I have given my code below use this and see if it works. Get back to me if you face any problem.
function [Psnr_DB] = psnr(I,I_out)
I = double(I);
I_out = double(I_out);
total_error = 0;
for iterz = 1:size(I,3)
for iterx = 1:size(I,1)
for itery = 1:size(I,2)
total_error = total_error + (I(iterx,itery,iterz)-I_out(iterx,itery,iterz))^2;
end
end
end
MSE = total_error/numel(I);
Psnr = (255^2)/MSE;
Psnr_DB = 10*log10(Psnr) %#ok<NOPRT>

bwlabel on enhanced image results in black image

I tried to enhance an image and perform connected component analysis but it returns a black image.
My code is
I = imread('Sub.png');
I=rgb2gray(I);
imshow(I)
J = adapthisteq(I);
imshow(J)
figure, imhist(J,64)
% I = contrast(I);
L = bwlabel(J);
figure,imshow(label2rgb(L,'jet','k','shuffle'));
Also how to number each blob after bwlabel
I think that is only a matter of scaling the intensity of J when you call bwlabel, since the image is of type uint8. Its maximal possible value is thus 255.
Using this line instead:
L = bwlabel(J/255);
Outputs the following:
Yay!

heatmap to color image matlab

I have a grayscle image. I can create a heatmap in matlab using:
I = imagesc(I);
it displays the intensity values in color. I want to get a color image using this heatmap. How can I do it?
This is the output of imagesc:
Presumably by "color image" you mean an RGB or truecolor image (an image array with three color channels). And what you're calling a "heatmap" is the colormap that Matlab applies by default to grayscale images (image arrays with only one color channel). A grayscale image plus a color map is referred to as an indexed color image (read more about that here). You can use the ind2rgb function to convert from indexed color to RGB:
IMG_gray = rand(100);
map = colormap; % Get the current colormap
IMG_rgb = ind2rgb(IMG_gray,map);
Note that by default the colormaps in Matlab use only 64 colors rather 256. To get smoother color gradations you can set the colormap manually via map = colormap(jet(256)); or map = colormap(hsv(256));.
If you then want an image file, you can use imwrite, which can take either RGB images or indexed color images with colormaps as input.
Scale the intensity values in your image as imagesc does, and use a colormap (jet is default):
N = 256;
IN = round(N * (I-min(I(:)))/(max(I(:))-min(I(:))));
cmap = jet(N); % see also hot, etc.
IRGB = ind2rgb(IN,cmap);

How to get image matrix from axes content

Is there a way to get the content of a contourf plot as an image matrix? I want rasterize only the content, not the axes, labels and the empty space of the entire figure.
My goal is to overlay a transparent, colored contour plot over a grayscale image and I don't see another way, since MATLAB has only one colormap per figure.
Try getframe and frame2im
Example from the frame2im documentation:
Create and capture an image using getframe and frame2im:
peaks %Make figure
f = getframe; %Capture screen shot
[im,map] = frame2im(f); %Return associated image data
if isempty(map) %Truecolor system
rgb = im;
else %Indexed system
rgb = ind2rgb(im,map); %Convert image data
end
Not a direct answer to the question, but this is how I think you could achieve your goal:
%# load in grayscale image
gray_im = rgb2gray(imread('peppers.png'));
%# converting n x m grey image to n x m x 3 rgb gray image
rgb_gray_im = cat( 3, gray_im, gray_im, gray_im );
%# displaying this image
imshow( rgb_gray_im );
%# plotting contourf on top with arbitrary colourmap
hold on
h = axes('position', [0.5, 0.5, 0.2, 0.2]);
z = peaks;
contourf(h, z, [min(z(:)), -6 : 8]);
Which gives the result:
The figure's colourmap is being used for the contourf plot. The background image is not relying on a colourmap, and is instead being displayed in truecolour - i.e. each pixel is being displayed as an RGB value defined in rgb_gray_im.
There are also other ways of getting around the MATLAB colourmap restrictions: see for example this blog post or these answers.