how can I show color of RGB triplet format in matlab? - 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

Related

Why are the scatterplot colors in MATLAB R2021a different?

I have installed Matlab R2021a, and when I run the command scatterplot for a vector, I get a figure as below:
I mean black and yellow. However the default colors in older versions is as follows:
I mean the colors are white and blue.
My concern, I need my MATLAB shows the colors of the figure as shown in older version, I mean with white and blue colors.
The behavior indeed changed in R2021a as mentioned in the release notes:
Visual appearance updates to plots generated with eyediagram and scatterplot functions.
The eyediagram and scatterplot functions now provide black plot backgrounds by default.
You can change the colors as desired by you by modifying the properties of axis/figure as shown below:
%Taking the example from the documentation
d = (0:63)';
s = qammod(d,64);
scatterplot(s);
%Modifying the colors
h=gca; %Axis handle
h.Title.Color='k' %Color of title
h.Children.Color='b'; %Color of points
h.YColor='k'; %Y-axis color including ylabel
h.XColor='k'; %X-axis color including xlabel
h.Color ='w'; %inside-axis color
h.Parent.Color='w' %outside-axis color
Without modification, we get this:
After modification, as desired, we get this:

What's the most efficient way of identifying items of a specific shade of colour in an image with matlab?

im trying to identify a specific shade of green leaves (e.g. navy green) from the attached image. how do i do that in the most efficient way? So far, i'm converting the RGB to HSV and then thresholding the image based on some specific range of saturation and value that will isolate my desired shade. it's working on some images and it's just all over the place on others. i want something that can isolate a specific shade of green in any different image that has slightly different saturation and value (e.g. if the picture was taken with too much light)
Image link
pic=imread('image.jpg');
q=rgb2hsv(pic);
H=q(:,:,1);
S=q(:,:,2);
V=q(:,:,3);
thresh=S>0.6111 & S<0.6666 & V>0.3888 & V<0.4583;
st=strel('diamond',20);
w=imdilate(thresh,st);
comps=bwconncomp(w,8);
num=comps.NumObjects;
fprintf('The number of leaves is %i',num)
% then i try to have some pointers on the image to show me where matlab has identified the the shade.
m = regionprops(w,'centroid');
boxes = cat(1, m.Centroid);
imshow(pic)
hold on
plot(boxes(:,1),boxes(:,2), 'b*')
hold off
Your help will be highly appreciated.
Either the HSV color space (hey, S is saturation and V value), where H will give you the hue,or CIE-Lab color space, where euclidean distance will give you how close 2 specific pixel are to each other in color.
This answer explains how to do it for HSV: Segment pixels in an image based on colour (Matlab)
Using combined with CIE-LAB may help if the colors are very close together (like the greens in each leaf), but you should give HSV a shot

Graduate matlab plot from one specific color to another

I just started learning Matlab and I want to know is there an easy way to vary the color of the scatter plot, for example, from yellow to red. I was thinking about using the command
scatter(x,y,100,c)
where c would be defined as an RGB triplet
t=linspace(0,1,100);
c='y' .* (1-t) + 'r' .* t;
Now this does something, but it doesn't really gives changes the color gradually from yellow to red. If there is an easier way or even some custom function that does this automatically, that would be great. Thanks.
Colors are harder than that! There are infinite possible paths to go from a color to another, as color spaces are 3D volumes. Of all possible color spaces, the worst possible one to use to deal with new colors or color creation is RGB!
The reason behind this is because there is no way of knowing what color [123, 213, 42] is, and generally it wont be a color between [123, 213, 0] and [123, 213, 255].
The best option for your case is the HSV color space. The first value, Hue, contains all the color information, while the other two are the saturation and Value or intensity of the color. Just reading the first value you know wich color you are talking about.
in HSV, red is H=0 and yellow is H=60degrees, or H=60*255/360 in uint8 or H=60/255 in double. Choose a S and V value that are good for you, e.g. S=255, V=255 to have intense colors. Then:
H=linspace(0,60/255,100);
HSV=[H;ones(size(H));ones(size(H))];
rgb=hsv2rgb(HSV);
This will give you a colormap of the colors. Then you need to assign them properly in scatter

Matlab How to receive color name after inputting RGB values

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,:)))

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