Convert Double Matrix To HSV in Matlab - image

I have a double matrix ​​in matlab and I want to convert it to an HSV image where supposedly similar double values ​​will appear with same color. I already converted to RGB with mat2gray but I want to to convert directly from double matrix to hsv image. Is it possible?
Thanks

You're asking if you can do what image and imagesc do. Yes, you can. You just need to interpolate into the 'hsv' colormap. The following code snippet demonstrates how to do this. It assumes your values are scaled between 0 and 1; you can easily change that though.
im = [0 .5 1; .7 .6 .2; .9 .3 .4];
cm = colormap('hsv');
cdata = interp1(linspace(0,1,length(cm)),cm,im);
figure;image(cdata)
cdata will be a (row x col x 3) matrix, you can use image directly to see it, or do whatever else you need to from there.

Related

Masked image displaying wrong intensity

I have a 3D image and a mask, both size [256x256x160]. The mask is a binary image of 0 and 255. After applying the mask on the image like so:
masked_image = image.*mask;
I get a masked_image that has totally wrong intensity values. In fact, its values are nowhere to be found on the original image. They are just too big. All I want to achieve is extract the original image's intensities at the location of the mask and calculate the mean value of that ROI. I can see from imshow that the mask is aligned properly on the image. However, applying the mask is unsuccessful and I don't understand why.
Your mask shouldn't be scaled from 0 to 255. It should contain only 0 or 1 to use it the way you want. Any one of these alternatives would work:
masked_image = image.*logical(mask);
% Or...
masked_image = image.*(mask > 0);
% Or...
masked_image = image.*(mask./255);
Converting the mask to 0 and 1 rather than 0 and 255 solved the problem.
The extracted ROIs now have the correct intensity.
Thanks for the clarification.

thresholding grayscale image without converting binary image

I would like to know how can ı achieve thresholding based on grayscale intensity by not converting it to binary image. As an example, below 50 intensity will be 0 while 50-255 intensity values remain the same(in Matlab).
Check the following...
Read sample image:
I = imread('cameraman.tif');
Set all values below 50 to zero.
I(I < 50) = 0;

Coloring an 8-bit grayscale image in MATLAB

I have an 8-bit grayscale image with different values (0,1,2,3,4,..., 255). What I want to do is color the grayscale image with colors like blue, red, etc. Until now, I have been doing this coloring but only in a greyscale. How can I do it with actual colors?
Here is the code I have written so far. This is where I am searching for all values that are white in an image and replacing them with a darkish gray:
for k = 1:length(tifFiles)
baseFileName = tifFiles(k).name;
fullFileName = fullfile(myFolder, baseFileName);
fprintf(1, 'Now reading %s\n', fullFileName);
imageArray = imread(fullFileName);
%// Logic to replace white grayscale values with darkish gray here
ind_plain = find(imageArray == 255);
imageArray(ind_plain) = 50;
imwrite(imageArray, fullFileName);
end
What you are asking is to perform a pseudo colouring of an image. Doing this in MATLAB is actually quite easy. You can use the grayscale intensities as an index into a colour map, and each intensity would generate a unique colour. First, what you need to do is create a colour map that is 256 elements long, then use ind2rgb to create your colour image given the grayscale intensities / indices of your image.
There are many different colour maps that are available to you in MATLAB. Here are the current available colour maps in MATLAB without the recently added Parula colour map that was introduced in R2014:
How the colour maps work is that lower indices / grayscale values have colours that move toward the left side of the spectrum and higher indices / grayscale values have colours that move toward the right side of the spectrum.
If you want to create a colour map with 256 elements, you simply use any one of those colour maps as a function and specify 256 as the input parameter to generate a 256 element colour map for you. For example, if you wanted to use the HSV colour map, you would do this in MATLAB:
cmap = hsv(256);
Now, given your grayscale image in your MATLAB workspace is stored in imageArray, simply use ind2rgb this way:
colourArray = ind2rgb(double(imageArray)+1, cmap);
The first argument is the grayscale image you want to pseudocolour, and the second input is the colour map produced by any one of MATLAB's colour mapping functions. colourArray will contain your pseudo coloured image. Take note that we offset the grayscale image by 1 and also cast to double. The reason for this is because MATLAB is a 1-indexed programming language, so we have to start indexing into arrays / matrices starting at 1. Because your intensities range from [0,255], and we want to use this to index into the colour map, we must make this go from [1,256] to allow the indexing. In addition, you are most likely using uint8 images, and so adding 1 to a uint8 will simply saturate any values that are already at 255 to 255. We won't be able to go to 256. Therefore, you need to cast the image temporarily to double so that we can increase the precision of the image and then add 1 to allow the image to go to 256 if merited.
Here's an example using the cameraman.tif image that's part of the image processing toolbox. This is what it looks like:
So we can load in that image in MATLAB like so:
imageArray = imread('cameraman.tif');
Next, we can use the above image, generate a HSV colour map then pseudocolour the image:
cmap = hsv(256);
colourArray = ind2rgb(imageArray+1, cmap);
We get:
Take note that you don't have to use any of the colour maps that MATLAB provides. In fact, you can create your own colour map. All you have to do is create a 256 x 3 matrix where each column denotes the proportion of red (first column), green (second column) and blue (third column) values per intensity. Therefore, the first row gives you the colour that is mapped to intensity 0, the second row gives you the colour that is mapped to intensity 1 and so on. Also, you need to make sure that the intensities are floating-point and range from [0,1]. For example, these are the first 10 rows of the HSV colour map generated above:
>> cmap(1:10,:)
ans =
1.0000 0 0
1.0000 0.0234 0
1.0000 0.0469 0
1.0000 0.0703 0
1.0000 0.0938 0
1.0000 0.1172 0
1.0000 0.1406 0
1.0000 0.1641 0
1.0000 0.1875 0
1.0000 0.2109 0
You can then use this custom colour map into ind2rgb to pseudocolour your image.
Good luck and have fun!

Extracting numerical data from an intensity printout

I'm looking to create an intensity matrix in Matlab from an intensity image plot (saved as a jpeg (RGB) file) that was made using what appears to be the jet colormap from Matlab. I am essentially trying to reverse engineer the numerical data from the plot. The original image along with the color bar is linked (I do not have enough reputation to insert images).
http://i.imgur.com/BmryO6W.png
I initially thought this could be done with the rgb2gray command, but it produces the following image with the jet colormap applied which does not match the original image.
http://i.imgur.com/RlBei2z.png
As far as I can tell, the only route available from here is to try matching the RGB value for each pixel to a value in the colormap lookup table. Any suggestions on if this the fastest approach?
It looks like your method using rgb2gray is almost working, apart from the scale. Because the colormap is scaled automatically to the contents of your plot, I think you will have to re-scale it manually (unless you can automatically detect the tick labels on the colorbar). You can do this using the following formula:
% Some random data like yours
x = rand(1000) * 256;
% Scale data to fit your range
xRange = [min(x(:)) max(x(:))];
scaleRange = [-10 10];
y = (x - xRange(1)) * diff(scaleRange) / diff(xRange) + scaleRange(1);
You can check the operation's success with
>> [min(y(:)) max(y(:))]
ans =
-10 10

Converting array into grayscale in matlab

I am trying to plot a set of data in grayscale. However, the image i get seems to be always blue.
I have a set of data, albedo that ranges from [0, 0.068], which is a 1X1 double.
My code is:
for all px,py
albedoMax = 0.0679; albedoMin = 0;
out_im(px,py) = 1/(albedoMax-albedoMin)*(albedo - albedoMin);
imshow(out_im);
drawnow;
end
Basically px,py are the image coordinates that i have to iterate over, and the formula is trying to map the input range of [0, 0.068] to [0 1]. However, by running this code, i notice that the output is always blueish. I was wondering what went wrong.
Thanks for the help.
Can't you make use of the rgb2gray function?
What you are making is one layer of the RGB image.
If you are creating a homogeneous blue image with constant color then the normalization is wrong. But if it is just the matter of being blue instead of being gray then just convert it using :
ImGray = rgb2gray(Im);
Do not forget to distribute the pixels like a grid/mesh, to fill all the image not just a part of it.

Resources