Plot a Colour Wheel scale
Ältere Kommentare anzeigen
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.
Akzeptierte Antwort
Weitere Antworten (1)
Kelly Kearney
am 9 Apr. 2014
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 Kommentare
JMS
am 10 Apr. 2014
Image Analyst
am 10 Apr. 2014
Yes, that will convert an hsv image into an rgb image.
JMS
am 11 Apr. 2014
Image Analyst
am 11 Apr. 2014
Bearbeitet: Image Analyst
am 11 Apr. 2014
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
am 11 Apr. 2014
Image Analyst
am 11 Apr. 2014
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
am 11 Apr. 2014
Image Analyst
am 11 Apr. 2014
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
am 11 Apr. 2014
Kelly Kearney
am 11 Apr. 2014
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
am 14 Apr. 2014
Image Analyst
am 14 Apr. 2014
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
am 14 Apr. 2014
Kelly Kearney
am 14 Apr. 2014
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
am 15 Apr. 2014
Kelly Kearney
am 15 Apr. 2014
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
am 15 Apr. 2014
Kelly Kearney
am 15 Apr. 2014
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?
Image Analyst
am 15 Apr. 2014
(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.
Kategorien
Mehr zu Red finden Sie in Hilfe-Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!