Plot a Colour Wheel scale

Hello! Please, can anyone tell how I can plot a Colour Wheel scale from specific data?
For example, if I have a [y,z] = meshgrid(-5: .35: 5, -5: .35: 5); Or [y,z] = meshgrid(-2: .1: 2, -2: .1: 2);
I also have attached file for example. Thanks in advance.

 Accepted Answer

Image Analyst
Image Analyst on 8 Apr 2014
See my attached demo. It does that.

3 Comments

Hi Image Analyst,
Thanks for answering the question but do you have for colored wheel only?
Thanks a lot.
It's one of the images it plots. If you want to mask it by a circle to make it round instead of square, you can use code from the FAQ to do that: http://matlab.wikia.com/wiki/FAQ#How_do_I_create_a_circle.3F
JMS
JMS on 10 Apr 2014
That is better and helps more.
Thanks.

Sign in to comment.

More Answers (1)

A simpler way:
r = linspace(0,1,10);
theta = linspace(0, 2*pi, 100);
[rg, thg] = meshgrid(r,theta);
[x,y] = pol2cart(thg,rg);
pcolor(x,y,thg);
colormap(hsv);
shading flat;
axis equal;

20 Comments

JMS
JMS on 10 Apr 2014
That was magic Kelly!
Many thanks
JMS
JMS on 10 Apr 2014
Edited: JMS on 10 Apr 2014
Can I use
rgb_image = hsv2rgb(hsv)
to convert it to RGB? Is it right? Or, how?
Thanks.
Yes, that will convert an hsv image into an rgb image.
JMS
JMS on 11 Apr 2014
If I have x components and y components, how I can get the colored wheel instead of the above? Such as
%
[x, y] = meshgrid(-2: .1: 2, -2: .1: 2);
Vel_x = y./(x.^2 + y.^2); % x-components
Vel_y = x./ (x.^2 + y.^2); % y-components
%
How do I find "r" in this case? Because I cannot make it like "r = Sqrt(x.^2 + y.^2)" because simply I will get all values become positive, while Vel_x and Ve_y should have negative values. Thus, all colored regions will be positive ”Same” I cannot distinguish between them.
r = linspace( ?, ?, 10);
theta = linspace(0, 2.*pi, 100);
[rg, thg] = meshgrid(r,theta);
[x,y] = pol2cart(thg,rg); pcolor(x,y,thg);
colormap(hsv);
rgb_image = hsv2rgb(hsv)
shading flat; axis equal; %
Thanks.
JMS, yes you can get r like that. That's exactly what I did in my demo. Look at it again below. See where I calculated radius inside the for loop. This time I also applied the circular mask for you.
fontSize = 20;
% Compute HSV image
rows = 300;
columns = 300;
midX = columns / 2;
midY = rows / 2;
% Construct v image as uniform.
v = 0.95 * ones(rows, columns);
s = zeros(size(v)); % Initialize.
h = zeros(size(v)); % Initialize.
% Initialize circle mask
circleMask = zeros(rows, columns);
% Construct the h image as going from 0 to 1 as the angle goes from 0 to 360.
% Construct the S image going from 0 at the center to 1 at the edge.
for c = 1 : columns
for r = 1 : rows
% Radius goes from 0 to 1 at edge, a little more in the corners.
radius = sqrt((r - midY)^2 + (c - midX)^2) / min([midX, midY]);
s(r, c) = min(1, radius); % Max out at 1
h(r, c) = atan2d((r - midY), (c - midX));
if radius <= 1
circleMask(r, c) = 1;
end
end
end
% Flip h right to left.
h = fliplr(mat2gray(h));
% Construct the hsv image.
hsv = cat(3, h, s, v);
% Display the hue image.
subplot(2, 2, 1);
imshow(h, []);
title('H (Hue) Image', 'FontSize', fontSize);
% Display the saturation image.
subplot(2, 2, 2);
imshow(s, []);
title('S (Saturation) Image', 'FontSize', fontSize);
% Display the value image.
subplot(2, 2, 3);
imshow(v, []);
title('V (Value) Image (with V = 0.95)', 'FontSize', fontSize);
% Construct the RGB image.
rgbImage = hsv2rgb(hsv);
% Mask with circle.
% Mask the image using bsxfun() function
maskedRgbImage = bsxfun(@times, rgbImage, cast(circleMask, class(rgbImage)));
% Display the RGB image.
subplot(2, 2, 4);
imshow(maskedRgbImage, []);
title('RGB Image, with V = 0.95', 'FontSize', fontSize);
drawnow;
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
JMS
JMS on 11 Apr 2014
Thanks a lot Image Analyst and just to comment, you forget to define fontsize.
Thanks, I edited it to put it in there. (It was in the original code in my Answer but I didn't transfer over that part in the snippet.)
JMS
JMS on 11 Apr 2014
The input data for the above function are scalars which are,
rows = 300;
columns = 300;
But instead, if I want to make the input data from the equations that I have posted them before
%
[x, y] = meshgrid(-2: .1: 2, -2: .1: 2);
Vel_x = y./(x.^2 + y.^2); % x-components
Vel_y = x./ (x.^2 + y.^2); % y-components
%
What should I do?
Because I want the plotted colors represent my calculations.
So, if I calculate the scalars like
Vscalar = Sqrt( Vel_x.^2 + Vel_y.^2);
and
%
rows = Vscalar;
columns = Vscalar;
%
All results will be positive and hence the resultant color will be wrong!
Thanks
I assume you're back to talking about Kelly's code. What you call Vscalar is a radius. The radius is the Saturation (also called "Chroma") component, and what Kelly calls r. It is always positive - not sure why you are expecting otherwise.
JMS
JMS on 11 Apr 2014
Ok. Let me explain like this, if I have two vectors Vx and Vy I can use
%
[x, y] = meshgrid(-2: .1: 2, -2: .1: 2);
Vel_x = y./(x.^2 + y.^2); % x-components
Vel_y = x./ (x.^2 + y.^2); % y-components
quiver(x, y, Vx, Vy)
%
to plot the vector field and then from those two vectors"the results" I want to view the colorbar to be Color wheel scale. How do I do that?
Thanks.
Okay, I'm pulling out my crystal ball, because you really haven't been clear about how this hypothetical color wheel corresponds to the data and quiver plot you keep referencing.
My guess is that perhaps, rather than a quiver plot, you want to show the directional field via colors, and rather than a traditional colorbar, you want a reference color wheel. If all of that is correct, then your steps will be:
1) Calculate the direction of your velocity field, and plot this via pcolor or similar
2) Plot the color wheel as I showed above in its own axis.
3) Make sure you choose an appropriate colormap that begins and ends with the same value, such as hsv.
Here's an example:
% Your data
[x, y] = meshgrid(-2: .1: 2, -2: .1: 2);
u = y./(x.^2 + y.^2); % x-components
v = x./ (x.^2 + y.^2); % y-components
% Calculate the angle
ang = atan2(v,u);
% Plot angle by color, overlaid with quiver
figure;
ax = axes('position', [0.1 0.1 0.7 0.7]);
pcolor(x,y,ang);
hold on;
quiver(x, y, u, v, 5, 'k');
shading flat;
% Plot color wheel for reference
cbax = axes('position', [0.8 0.8 0.15 0.15]);
r = linspace(0,1,10);
theta = linspace(-pi, pi, 100);
[rg, thg] = meshgrid(r,theta);
[x,y] = pol2cart(thg,rg);
pcolor(x,y,thg);
colormap(hsv);
shading flat;
axis equal;
set(cbax, 'visible', 'off');
set([ax cbax], 'clim', [-pi pi]);
JMS
JMS on 14 Apr 2014
When I am converting the HSV image to RGB image using the command
rgb_image = hsv2rgb(hsv)
it doesn't change anything? Any clue please?
Thanks
You lost me on quiver too. Why do you want a vector field, when you originally just wanted a rainbow-like picture? And who cares how you generate it, with meshgrid() or images, as long as you get the picture you want? Why does it matter?
JMS
JMS on 14 Apr 2014
Because by this way you can explain where the field is strong and weak. Then, I should be able instead of clicking the colorbar in the viewing image window in matlab to use color wheel using the vector field data as Kelly did. The modified code by you, starts with the below input data
%
rows = 300; columns = 300; midX = columns / 2; midY = rows / 2;
%
I tried to use the results from the vector field to be as input data into your code but it did give errors. Thank you. I think it is enough that I have lost and don't want anyone else to be.
I think your confusion is coming because the question you originally asked doesn't really correspond to what (I think!) you want to do. The is a big difference between plotting an image in HSV color space, and using an hsv-like colormap to pseudocolor data.
ImageAnalyst's code does the former; it takes image data (i.e. n x m x 3 arrays, where the last dimension is either RBG or HSV color space) and plots it in various ways. One of the examples shows the image of a color wheel, which is what you originally asked for.
However, I'm pretty sure you don't have image data, and you're not particularly interested in creating the color wheel itself. You have a directional field, and you just want to plot that directional data using a colormap that's appropriate to that type of cyclical data. To do that, you don't need to convert your data to an HSV image. Note that in my example, you can change the colormap to something else (try colormap(jet)); the hsv colormap just happens to make sense, since it begins and ends on the same color, just as a direction of +pi and -pi point in the same direction.
JMS
JMS on 15 Apr 2014
Thanks Kelly spot on. So, you mean I cannot get RGB using those data unless I have (n x m x 3)data?
By "those data", do you mean your quiver plot data? i.e....
[x, y] = meshgrid(-2: .1: 2, -2: .1: 2);
Vel_x = y./(x.^2 + y.^2); % x-components
Vel_y = x./ (x.^2 + y.^2); % y-components
If so, the real question is, RGB data of what? Nothing in that dataset indicates color. The data (velocity and position components), or quantities derived from it (speed, direction, etc.) can be plotted using a colormap, as I demonstrated above. But that doesn't automatically convert your data into an image. You could always export the figure, or just the colormapped data, to an RGB image if you wanted, but that will require some extra steps. I don't think I can help you any further unless you specify EXACTLY what you want your final product to be.
JMS
JMS on 15 Apr 2014
Yes exactly. I want to use quiver to produce the vector field and then use the same data "quiver plot data" to create the color wheel in RGB. So, instead of
%
colormap(hsv);
%
What should I do to have the color wheel in RGB from the quiver plot data?
Many thanks.
Aaaaaah!
You've used the phrase "have the color wheel in RGB from the quiver plot data" several times now, and it remains just as meaningless as before.
Your quiver plot has absolutely nothing to do with a color wheel. You can create a pretty picture of a color wheel several ways (such as the examples ImageAnalyst and I gave) using Matlab. Or you could pull out a piece of paper and some crayons and draw one there. But it's still just a pretty picture, and it has no connection to your quiver plot data.
I gave an example of how to plot directional data by color, overlaid with a quiver plot, and then I drew a small wheel next to it as a guide. Is that even remotely close to what you're looking for?
(Sound of hand slapping forehead) Yeah, I'm pretty much baffled too, and about to give up. I don't even know that he has vector field data. He just takes a vector spanning a certain range in x and certain range in Y, calls meshgrid and calls that his vector field. It's totally made up/synthesized. JMS do you have any actual real world data?
It sounds like JMS wants to create a color wheel but instead of using the normal values and ranges required to produce one (like I did), he wants to use some special specified range (-2 to +2) for some unknown reason. OK, well, if you insist on doing that, then you're going to have to transform those into the range that is required, and then create the wheel picture. Of course that could all be wrong since I don't know what he wants.
Maybe he wants to create an image of the velocity according to the equations, and just simply apply a colormap to it:
% First create velocityImage from equations. Then:
imshow(velocityImage);
colormap('hsv');
colorbar;
You don't get a pretty perfect color wheel but what you do get is an image where the color corresponds to the magnitude of the velocity.

Sign in to comment.

Asked:

JMS
on 8 Apr 2014

Commented:

on 15 Apr 2014

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!