Matlab: plot image given by 3 dimensional matrix - matlab

I want to plot an image. Therefor I have a matrix with the dimensions wxhx3, where w and h are the resolution (width, height respectively). The third dimension contains the vector of rgb-color.
So image(1,1,1) is the red component of Pixel(1,1), image(1,1,2) is the green and image(1,1,3) the blue one.
Now my question is, how can I plot an image with this given matrix?
If I want to use image(..), I have to define a colormap and recompute the indices because image(...) requires a wxhx1 matrix.
Can anyone help my?

Matlab images display functions (image, imshow, imagesc) take either data that is formatted into an image format (uint8 for 8-bit image and uint16 for 16-bit image) or data of double type for values in the [0,1] range.
So if you have value in the range [0,255] (or [0,65535] for 16-bit image) you can try :
imshow(uint8(matrix))
or
imshow(uint16(matrix))
If have you values in the range [0,1] you can try :
imshow(double(matrix))
Or, the least recommendable, if your values do not fit those cases you can try :
imshow(double(matrix/max(matrix(:))))
EDIT: from #rayryeng comment.

Related

Mutual Information, Kullback Leibler Divergence between two color images

I am working on a project on Image classification using Mutual Information. It requires me to use probability distribution of a color image, either I want to calculate the Mutual Information or the Kullback Leibler Divergence in Matlab. Can anyone help me out in this?
I have calculated the entropy of a colored image as:
I = imread('s1.png');
% rgb_columns = reshape(rgb, [], 3);
% %Change RGB matrices to a single matrix of color indices.
% %Removes the third dimension from the pixel intensity matrix.
Color_ind=double(I(:,:,1)).*256^2+double(I(:,:,2).*256)+double(I(:,:,3));
disp(size(Color_ind));
% Finding unique elements in the matrix and find their length
unique_ind=unique(Color_ind);
unique_len=length(unique_ind);
%Pre-allocate space for the vector that will hold the number of entries
%for each unique color
color_count_prob=zeros(unique_len,1);
%Count the number of each occurrence of each unique color index in the
%original matrix.
for i = 1:unique_len
color_count_prob(i)=(length(find(unique_ind(i)==Color_ind)))/(2073600);
end
en_sum=0;
for i = 1:unique_len
en_sum = en_sum + log2(color_count_prob(i));
end
en = -en_sum;
For PDF calculation of a colored image:
First, you need to convert the image to grayscale. If you insist on staying in RGB mode (or any other colored mode) you will have to generate 3 PDFs (one for each color channel) - I would not suggest doing that for the purposes of Kullback Liebler or Mutual Information, the grayscale image will do.
Second, you need to calculate the distribution of each image. For this purpose, you will need to flatten your image (convert from a 2D array to 1D array). Once you flatten the image, you should sort the values. Once sorted, you should normalize them (you can choose not to, but would recommend). After that, you can derive the histogram for the image.
And to measure the Kullback Liebler divergence, you need:
Measure the entropy on your image histograms. This will be a number.
Simply subtract the values from step one and it will give you the Kullback Liebler divergence value for those two images.

How to create a mask to segment a color image by activecontour?

I want to use the function activecontour in matlab to segment a color image, but I don't know how to create the mask.
The documentation says:
For color and multi-channel images, mask must be a 2-D logical array where the first two dimensions match the first two dimensions of the image A.
But I don't understand what has to be done. Any suggestions?
Let's consider that the size of your image is NxM pixels, N is the number of rows, M the number of columns.
If it is a color image, each pixel is probably composed of 3 values, one for intensity of red (R), one intensity of blue (B) and one for intensity of green (G). These are called the color channels. So the real shape of the matrix representing your image is NxMx3.
What the documentation says is that the mask should be 2-D, and the dimensions should match the first two dimension of your image. It means the mask should have the same number of rows and cols than your image, but each pixel of the mask is not composed of 3 values anymore. It is composed of 1 value (a logical value : 0 or 1).
So what you need to do is to give the function a matrix NxM with only 0 and 1 as possible values. The doc says that the mask is the :
Initial contour at which the evolution of the segmentation begins, specified as a binary image the same size as A.
So the mask needs to represent an initial guess of the contour. If you already know that what you want to see is in the upper left corner of the image, you can set the initial contour as a square located in the upper left corner for example.
Now to represent the contour by a matrix of logicals, you simply set all the elements of the matrix to 0 and just the elements representing the contour to 1 I guess.
Lets me know if there is something you don't understand, I'd be glad to answer you.

what is map and level in im2bw in matlab?

I want to convert RGB image to binary image for processing in matlab ,
its important to choose variables exactly,
so I need to know exacly what is map and level in im2bw(x,map,level) ?
Map is used only when the image is of map datatype. Essentially map is a format of storing images where each pixel is represented by a number. The numbers are stored in a lookup table called map. The map images are often more compressible than regular images. When the image has to be displayed, the value of pixel is displayed based on the lookup table.
In this case, im2bw just looks at the greyscale value of each pixel and then thresholds.
The level is the greythresh of the image [0-1]. It can be used to change the luminescence of the image.
The map is a colormap. Here is the information from mathworks:
"A colormap is an m-by-3 matrix of real numbers between 0.0 and 1.0. Each row is an RGB vector that defines one color. The kth row of the colormap defines the kth color, where map(k,:) = [r(k) g(k) b(k)]) specifies the intensity of red, green, and blue."
The colormap can be used to change the image's colors.
http://www.mathworks.com/help/images/ref/im2bw.html
http://www.mathworks.com/help/matlab/ref/colormap.html

How to represent points from PCA space in the RGB space

I'm trying to implement a morphological method for image colors from the article: "Probabilistic pseudo-morphology for grayscale and color images". At one point, we compute the PCA on the entire image, calculate a chebyschev inequality ( the equation 11 in the paper: http://perso.telecom-paristech.fr/~bloch/P6Image/Projets/pseudoMorphology/Caliman-PR2014.pdf) of each 3 components which gives us 3 pairs of vector. We next have to represent these vectors back in the RGB space. I don't understand how do we do that? Can someone help me?
Looking at the paper, I'm not sure which representation you're talking about. I'm guessing Fig. 16, but I'm not sure. There's a note in the caption of Fig. 16 that's helpful: "(For interpretation of the references to color in this figure caption, the reader is referred to the web version of this article.)"
Possible answer: if you have a matrix of size A = (y_pixels,x_pixels,3), then you can display this as an RGB image via:
A = rand(100,100,3);
figure()
imshow(A)
Note that your matrix must be scaled in the range [0..1].
It seems easy to map your your PCA scores for each pixel onto such a matrix, and simply display that as RGB via imshow. Does that solve your problem?

Matlab - Replace successive pixel with pixel from the left

So, I've quantized a grayscale image with four quantized values. I'm trying to maintain the first pixel of each row of the quantized image and replace each successive pixel with the difference from the pixel to its left.
How would you code this in matlab and can someone explain this to me conceptually?
Also, my concern is that because the image is relatively uniform because of the quantization of the dynamic range, most of the image would appear black, no? It seems to me that only the transition areas and the edges will have some difference in quantized values.
To create the difference to the pixel on the left, all you have to do is subtract the pixels in columns 1,2,3... from the columns 2,3,4...
%# create a random image with four values
randomImage = randi(4,[100,90]); %# use different numbers of rows and cols so we know which is which
%# catenate the first column of the image with the difference from the pixel to the left
%# for all pairs of columns in the image
differenceImage = [randomImage(:,1),randomImage(:,1:end-1)-randomImage(:,2:end)];
Yes, you'd expect quite a few uniform patches (which will be gray, since unless you plot the absolute value of the differences, there will be some that are negative).