Custom ColorMap Question: Does it have to be written as a script??

2 views (last 30 days)
Hello everyone. I have a question about colormaps. I am trying to generate my own custom colormap. I have a 2D grid with random objects scattered about, and I have fluid flowing from left to right. I want it so that everything that has zero velocity is white, and then everything with positive velocity, the fluid, corresponds to the jet colormap. I have tried to customize the colormap using matlab "color editor" but some things that should not be white are white as the code iterates. I think this is because some of the very low velocities are considered as white in the colormap. Any suggestions? And if I need to be more specific, please do let me know.
Thank you kindly!
  1 Comment
Stephen23
Stephen23 on 26 Oct 2016
Edited: Stephen23 on 26 Oct 2016
Open any colormap function, e.g
edit jet
and you will find that they are not scripts (as your title asks), but each is in fact a function with one output argument. So that answers the question in your title. The body of your question is unclear.

Sign in to comment.

Answers (2)

Image Analyst
Image Analyst on 26 Oct 2016
You can make up your own custom colormap in an m-file that is a function, (i.e., starts with the "function" keyword) rather than a script. Is that what you're asking?
I don't know what's iterating. Your fluid flow simulation? Are you computing a new custom colormap at each iteration? If so, why?
How many colors are in your colormap. If zero velocity is the lowest it goes, and white is the first row of the colormap, then it's possible some other small velocities would also display as white if you don't have enough colors (rows) in your custom colormap.
  3 Comments
Image Analyst
Image Analyst on 27 Oct 2016
Edited: Image Analyst on 27 Oct 2016
Colormaps assign a range to each color. Because you have some velocities very close to zero, then are included in the white range, even though you don't want them to be.
I suggest you use 256 colors in your colormap. Then convert to RGB using rgb2ind. Then extract the individual color channels and use the mask (your second image) to set them to white and recombine. Something like
rgbImage = ind2rgb(grayImage, jet(256));
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
% Create a mask of where pixels are exactly 0
mask = grayImage == 0;
% Make mask pixels white:
redChannel(mask) = 255;
greenChannel(mask) = 255;
blueChannel(mask) = 255;
% Recombine separate color channels into a single, true color RGB image.
rgbImage = cat(3, redChannel, greenChannel, blueChannel);
imshow(rgbImage);
In this way, only the pixels that are EXACTLY zero will show up as white.
Sean Farrell
Sean Farrell on 28 Oct 2016
Okay, thanks for the answer! I'll give this a whirl in a bit!
-Sean

Sign in to comment.


Kelly Kearney
Kelly Kearney on 28 Oct 2016
An alternative solution would be to use pcolor instead of image, imagesc, etc, and set your zero-values to NaN. The pcolor function displays NaNs with no color, allowing the axis color to filter through.
x = max(peaks(100), 0); % some fake data
x(x == 0) = NaN;
pcolor(x);
shading flat;
colormap(jet);
  2 Comments
Image Analyst
Image Analyst on 28 Oct 2016
But pcolor doesn't show the last row or column. Just try this:
m = randi(255, 3, 3); % Make 3-by-3 image.
pcolor(m); % Shows a 2-by-2 image, not a 3-by-3 image.
Kelly Kearney
Kelly Kearney on 28 Oct 2016
I feel like I've had this discussion with you at least 20 times, haha.
Yes, one of the differences between image and pcolor is that the latter chooses to define colors at the grid vertices rather than at the center of grid cells. Another is that the input x and y coordinates in pcolor correspond to grid vertices rather than grid centers. Both approaches have their advantages.
If you use interpolated shading, the far-edge colors are used. If you prefer flat or faceted shading, and the dropped-edge is an issue (in hi-res data like the OP's example, it's often not), you can easily get around it by padding an array with NaNs:
m = randi(255, 3, 3);
m2 = padarray(m, [1 1], NaN, 'post');
pcolor(m2); % shows the 3x3

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!