Matlab How to receive color name after inputting RGB values - matlab

Matlab: How to receive color name after inputting RGB values
So I have this image and I used impixelregion to find the RGB values of each pixel. However, I want the name of the color to show up on Matlab. For example, if I input RGB values [9,9,11], I want Matlab to tell me that the color is black. Do I have to create my own function or code for this or is there something out there that can let me input whatever RGB values I want and have Matlab tell me what color those RGB values stand for?
thank you!

Here is a file with the name and corresponding color value, following wikipedia:
Colors_name_val.mat
And here is the corresponding code to give you the name of a color.
function name = name_rgb(my_val)
load('Colors_name_val.mat')
delta=10000000;
for k=1:430
curDelta=sum(abs(my_val-Val(k,:)));
if(curDelta<delta)
name=Name(k);
delta=curDelta;
end
end
end
It just find the less different color of the list, by minimising sum(abs(my_val-Val(k,:)))

Related

Reading grayscale image in matlab [duplicate]

This question already has an answer here:
What does the index refer to when selecting a pixel on an image in Matlab?
(1 answer)
Closed 6 years ago.
I have gray scale image "lena.bmp". I want read this image in matlab using imread() function.
When i use code below to read and show image my image is dark (black).
img = imread('lena.bmp');
imshow(img);
But when i use code below, I have no problem to view.
[img map]= imread('lena.bmp');
imshow(img,map);
It seems that my first code doses not reading image in grayscale mode (like what rgb2gray function generate).
My image is as follows:
What can i do to solve this problem?
Your image is an "indexed" image. That means it contains integer values which act as "labels" more than anything, and each of those labels is mapped to a colour (i.e. an rgb triplet). Your map variable represents that mapping; at row 5 you have the rgb triplet that corresponds to 'label' "5", for instance.
To see what I mean, do unique(img) and you'll see that the values of your img array are in fact quite regular. The command rgbplot can demonstrate the actual colourmap graphically. Run rgbplot(map) on your map variable to see the mapping for each of the red green and blue colours.
Now, save and read the image below on your computer as img2 and compare the array values.
This image was generated by converting from the "indexed" image you linked to, to a "grayscale" one using photoediting software (the GIMP). The difference is that
in a grayscale image, the pixel values represent actual intensities, rather than integer 'labels'. Imread reads grayscale images as uint8 images by default, meaning it assigns intensity values to pixels ranging from 0 (black) to 255 (white). Since these values happen to be integers you could still cheat and treat them as 'labels' and force a colour-mapping on them. But if you assign a 'linear map' (i.e. value 1 = intensity 1, value 2 = intensity 2, etc) then your image will look as you would expect.
You'll see that the values from unique(img2) are quite different. If you imshow(img2) you'll see this displays as you'd expect. If you don't specify a colormap for imshow, it will assume that the map is a linear mapping from the lowest to the highest value in the image array, which explains why your indexed image looked weird, since its values were never supposed to correspond to intensities.
Also try imagesc(img2) which will show this but using the "current" colormap. imagesc causes the colormap to be "scaled", so that the lowest colour goes to the lowest value in the image, and similarly for the highest.
The default colormap is jet so you should see a psychedelic looking image but you should be able to make out lena clearly. If you try colormap gray you should see the gray version again. Also try colormap hot. Now to make sense of the colormaps, try the rgbplot command on them (e.g. rgbplot(gray), rgbplot(hot) etc).
So, going back to imshow, imshow basically allows you to display an indexed image, and specify what colormap you want to use to display it. If you don't specify the colormap, it will just use a linear interpolation from the lowest value to the highest as your map. Therefore imshow(img) will show the image pretty much in the same way as imagesc(img) with a gray colormap. And since the values in your first img represent evenly spaced 'labels' rather than actual intensities, you'll get a rubbish picture out.
EDIT: If you want to convert your indexed image to a grayscale image, matlab provides the ind2gray function, e.g.:
[img, map] = imread('lena.bmp');
img_gray = ind2gray(img, map);
This is probably what you need if you mean to process pixel values as intensities.

how can I show color of RGB triplet format in matlab?

I have a question in matlab
I need a simple code in matlab to get (R,G,B) and show the color of that.
For example get (1,0,0) and show red color. How can I do ?
Here is Example
To change the background color of a figure to green, specify the color with a short name, a long name, or an RGB triplet. These statements generate equivalent results:
whitebg('g')
whitebg('green')
whitebg([0 1 0]);
You can use ColorSpec anywhere you need to define a color. For example, this statement changes the figure background color to pink:
set(gcf,'Color',[1,0.4,0.6])
for more detail visit this http://in.mathworks.com/help/matlab/ref/colorspec.html?s_tid=gn_loc_drop

I have a 2D plot in Matlab where I want to specify the RGB value for each point to vary to color

I am trying to plot a 2D line in Matlab with a color that varies based on an RGB code that I assign to each point. The code below works well for a given colormap ('col' values defining the color), but I am trying to keep tighter control over the color assignment, so that values always render the same color across multiple charts.
surface([x;x],[y;y],[z;z],[col;col],...
'facecol','no',...
'edgecol','interp',...
'linew',2);
The question is how do you decide which colour does each point have? defining the colormap is easy (shown below).
col=colormap(hot(128)) %% or other colour style like hsv or jet
if you have a variable (lets called it V) that for each point has a certain value and you want the colours to change based on that:
first define the values for the extremes in the colormap:
min_col=0; %%%can be the minimum of V
max_col=1; %%%can be the maximum of V
Then interpolate to your data
new_col=interp1(linspace(min_col,max_col,length(col)),col, V(:))

Access RGB channels in an image in MATLAB

I want to access the red channel of each pixel in my image. I don't want to change it. I just want to identify the pixels with a range of red. I'm looking for pixels that will have the colors like RGB(15,0,0), RGB(120,0,0), RGB(200,0,0) and so on. My image is mostly gray, I want to identify the red boxes on that.
I tried:
image = imread('myimage.jpg');
figure; imshow(image);
redPlane = image(:,:,1);
figure; imshow(redPlane);
The second figure displayed is all gray. It took off the red.
You are visualizing the red channel as a grayscale image. Think about it. The image is essentially a 3D matrix. By doing image(:,:,1);, you are accessing the first slice of that image, which is a 2D matrix and this corresponds to the red components of each pixel. imshow functions such that if the input is a 2D matrix, then the output is automatically visualized as grayscale. If imshow is a 3D matrix, then the output is automatically visualized in colour, where the first, second and third slices of the matrix correspond to the red, green and blue components respectively.
Therefore, by doing imshow on this 2D matrix, it would obviously be grayscale. You're just interpreting the results incorrectly. Here, the whiter the pixel the more red the pixel is in that location of the image. For example, assuming your image is uint8 (unsigned 8-bit integer) if a value has 255 at a particular location, this means that the pixel has a fully red component whereas if you had a value of 0 at a particular location, this means that there is no red component. This would be visualized in black and white.
If you want to display how red a pixel is, then put this into a 3D matrix where the second (green) and third (blue) channels are all zero, while you set the red channel to be from the first slice of your original image. In other words, try this:
imageRed = uint8(zeros(size(image))); %// Create blank image
imageRed(:,:,1) = redPlane; %// Set red channel accordingly
imshow(imageRed); %// Show this image
However, if you just want to process the red channel, then there's no need to visualize it. Just use it straight out of the matrix itself. You said you wanted to look for specific red channel values in your image. Ignoring the green and blue components, you can do something like this. Let's say we want to create an output Boolean map locationMap such that any location that is true / 1 will mean that this is a location has a red value you're looking for, and false / 0 means that it isn't. As such, do something like:
redPlane = image(:,:,1);
% // Place values of red you want to check here
redValuesToCheck = [15 20 100];
%// Initialize a boolean map where true
%// means this is a red value we're looking for and
%// false otherwise
locationMap = false(size(redPlane));
%// For each red value we want to check...
for val = redValuesToCheck
%// Find those locations that share this
%// value, and set to true on the boolean map
locationMap(redPlane == val) = true;
end
%// Show the map
imshow(locationMap);
One small subtlety here that you may or may not notice, but I'll bring it up anyway. locationMap is a Boolean variable, and when you use imshow on this, true gets visualized to white while false gets visualized to black.
Minor note
Using image as a variable name is a very bad idea. image is a pre-defined function already included in MATLAB that takes in a matrix of numbers and visualizes it in a figure. You should use something else instead, as you may have other functions that rely on this function but you won't be able to run them as the functions are expecting the function image, but you have shadowed it over with a variable instead.

Identify color in matlab

I want to identify the colors of this color checker chart in a little robust way. In the first step not every color has to be detected correctly.
I ectracted already the subimages, so that I have squares with only one color.
The idea for detecting the color (and print them out) was to transform the colors from RGB to the HSV color model.
Does anybody has a better solution or can help?
Best regards!
I think you want an algorithm like the following:
Compute the mean RGB value for a given subimg.
Compute the Euclidean distance from your mean RGB value to the RGB value in each of the
squares.
Return the square name that is closest to your RGB value.