How to determine color in MATLAB - matlab

I would like to map HSV values to color names in MATLAB. I have converted RGB to HSV and thresholded the values using a series of if statements in order to determine the color . However I would instead like to map the values to defined color names. Is this possible in MATLAB?

If you're working with an mxnx3 (i.e. 2D with 3 channels) RGB image im, then typically the red channel is im(:,:,1), green is im(:,:,2), and blue is im(:,:,3). So if you want the RGB values at some point (x, y), then you can get the vector by im(x,y,:).
If you just want to convert HSV values to RGB, then you can use the function hsv2rgb.

Related

Grouping hex color values

I am trying to write my own 3d topography-type program in matlab that will take a 2d thermal image and create a 3d model based on the intensity of colors in the 2d image.(white is the highest peak, red is lower, orange is lower...) I have extrapolated the hex and RGB color values of each of the pixels and sorted them into an array that indexes the place of the pixel in the image. There are thousands of unique colors. I need to design an algorithm that will evaluate which color grouping each pixel falls into. (white, red, orange...) without having to physically view each unique color.
I have extracted the hex values from matlab into excel. I alpa-numerically ordered the values and saw that some adjacent values do not lie in the same color family. (ex. #00024C is blue and #000300 is black, yet they are adjacent) Is there some kind of threshold built into matlab that I can use to sort the colors?

Converting a RGB image to grayscale based on the intensity of one color channel [duplicate]

This question already has an answer here:
Access RGB channels in an image in MATLAB
(1 answer)
Closed 6 years ago.
I'm working with images similar to this: a cell image, and I want to extract only the red-pink sections. As of now I'm using img(:,:,1) to pull out the red values but this produces a binary image. I wanted to know if there was a way to extract the "red" values and produce a grayscale image based on their degree of "redness" or intensity. Any help would be awesome.
You are likely visualizing the result using imshow which will automatically set the color limits of the axes to be between 0 and 1. Your image is RGB and the values of the red channel are going to range from 0 to 255. Because of this, if you only specify one input to imshow, you will get an image where all values > 1 will appear as white and all zero-values will be black. So your image isn't really binary, it just appears that way.
You want to either display your image with imagesc which will automatically scale the color limits to match your data:
imagesc(img(:,:,1));
colormap gray
Or you can specify the second input to imshow to cause it to also scale to fit your data range:
imshow(img(:,:,1), [])
The reason that this isn't an issue when you are visualizing all channels is that if you specify red, green, and blue channels, this is considered a true color image and all axes color limits are ignored.
The data you capture will be correct (and is grayscale), but the visualization may be incorrect. When trying to visualize a 2D matrix (same as your result img(:,:,1)), matlab applies the default colormap and the result is:
[x,y]=meshgrid(1:200, 1:200);
z=x.^2.*sin(y/max(y(:))*pi);
figure;imagesc(z);
If you want to avoid the applied jet colormap, either change the colormap:
colormap('gray')
or change your 2D matrix into a 3D one, explicitely specifying the colors to display (must be values between 0 and 1):
z3d = z(:,:,[1 1 1]); % more efficient than repmat
z3d = (z3d - min(z(:)))./range(z(:)); % make sure values in range [0; 1]
You see banding in the colormap version, because by default a colormap contains 64 different colors; the 3d matrix version doesn't have this problem as it directly displays the colors.
If I may add to your question, it seems to me you're simply trying to isolate and visualise the red, green, and blue fluorofores separately (or in combination). I specifically think this because you mention 'pink'.
It may be nicer to just isolate the channels:
>> F_red = F; F_red(:,:,[2,3]) = 0;
>> F_green = F; F_green(:,:,[1,3]) = 0;
>> F_blue = F; F_blue(:,:,[1,2]) = 0;
>> F_pink = F; F_pink(:,:,2) = 0;
Here's a subplot of the result:
Furthermore, you should know that the 'naive' way of producing a grayscale image does not preserve the 'luminosity' of colours as perceived by the human eye, since 'green' at the same intensity as 'red' and 'blue' will actually be perceived as brighter by the human eye, and similarly 'red' is brighter than 'blue'. Matlab provides an rgb2gray function which converts an rgb image to a grayscale image that correctly preserves luminance. This is irrelevant for your pure red, green, and blue conversions, but it may be something to think about with respect to a 'pink-to-grayscale' image. For instance, compare the two images below, you will see subtle contrast differences.
>> F_pinktogray_naive = mean(F(:,:,[1,3]), 3);
>> F_pinktogray_luminance = rgb2gray(F_pink);
A subplot of the two:
In a sense, you probably care more about the left (naive) one, because you don't care about converting the pink one to a gray one "visually", but you care more about the red and blue fluorofores being "comparable" in terms of their intensity on the grayscale image instead (since they represent measurements rather than a visual scene). But it's an important distinction to keep in mind when converting rgb images to grayscale.

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

Generate image in particular colormap in Matlab

I have a grayscale image in Matlab that I want to convert to an RGB image. To plot it I can simply do
imagesc(img)
colormap jet
or replace colormap jet with any other colormap. How would I get the actual array of RGB values that would give the equivalent image if I were to do image(img)?

An explanation for this MATLAB code snippet

Consider:
%# load a grayscale image
img = imread('coins.png');
%# display the image
figure
imshow(img,[]);
%# false-color
colormap('hot')
The above code is from here:
Infrared image processing in Matlab
But I don't understand how figure (what's the difference with/without it?) and colormap (how does it affect the already shown image?) work?
figure is not required, imshow just displays img on it. If a figure hadn't been opened, imshow would've created a new one.
The colormap colors the intensities of the image. The hot map colors values in increasing intensity with black, red, yellow, and white-hot. Another popular colormap is jet which has a number of interesting colors.
False colors
So the matrix you want to see has intensities which can have any range of values. For better visualization, the intensities are displayed in a range of colors or a set of false colors. Normally, a grayscale image will display an image is shades of grey, where white is maximum and black is minimum. False color is an extension of that concept with several colors in between (like jet) and an effect of metal being heated in hot.
Colormap at the pixel level
Suppose you have a matrix with pixel values ranging from [cmin xmax]. Now, normalize the values so that the range is [0,1]. Also, suppose you have a color map, such that a range of colors are mapped to some values from 0 to 1 (e.g. 0.5 is mapped to RGB(100,200,100))- then you get the false color mapping by finding the closest intensity in the map and display the corresponding color.
More on colormap in the MATLAB documentation. I've included some picture from that link here:
Jet
(source: mathworks.com)
Bone
alt text http://www.mathworks.com/access/helpdesk/help/techdoc/ref/bone_spine.gif