Divided colorbar automatically. Stop the blending of colors displayed

105 views (last 30 days)
Hi there,
When I use the colorbar function I get a scale of blended colors and I would like to demarcate each color without having to define each of those colors explicitly. Is there a way to turn off this blending on colors so that I get specific color segments in the colorbar.
I am confused as to why I am having so much trouble finding information on this. Seems fairly simple!

Accepted Answer

Kelly Kearney
Kelly Kearney on 10 Mar 2016
The default number of colors in a colormap is 64; if you decrease the number of colors, you get more discrete blocks:
colormap(parula(64));
colorbar;
vs
colormap(parula(10));
colorbar;
For more explicit control over color intervals, you might want to check out cptcmap.m

More Answers (1)

Image Analyst
Image Analyst on 10 Mar 2016
Edited: Image Analyst on 22 Jul 2021
No. I think to demarcate each intensity somehow (like a special color, like every 10th gray level is black or whatever), you're going to have to specify the colormap very precisely. Don't be afraid of doing that. I have confidence in you that it's well within your abilities.
Here are 4 different ways to "demarcate" the gray level ranges (depending on what you mean/want/need):
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 18;
% Read in input gray scale image.
grayImage = imread('cameraman.tif');
% Using parula with 256 colors.
subplot(2, 2, 1);
imshow(grayImage);
hold on;
cmap = parula(256);
imshow(grayImage, 'Colormap', cmap);
title('Using parula(256)', 'FontSize', fontSize);
colorbar
% Using parula with 256 colors but with every 16'th color black
% to better demarcate intensity ranges.
subplot(2, 2, 2);
imshow(grayImage);
hold on;
cmap2 = parula(256);
cmap2(1:16:end,:) = 0;
imshow(grayImage, 'Colormap', cmap2);
title('Using parula(256)', 'FontSize', fontSize);
colorbar
% Using parula with 256 colors but with only 8 ranges of 32 gray levels
% to better demarcate intensity ranges.
subplot(2, 2, 3);
imshow(grayImage);
hold on;
cmap3 = parula(8);
imshow(grayImage, 'Colormap', cmap3);
title('Using parula(8)', 'FontSize', fontSize);
colorbar
% Using parula with 256 colors but contours
% every 32 gray levels overlaid (this is 8 ranges).
subplot(2, 2, 4);
imshow(grayImage);
hold on;
cmap4 = parula(256);
imshow(grayImage, 'Colormap', cmap4);
title('Using contourf(grayImage, 32)', 'FontSize', fontSize);
colorbar
contourf(grayImage, 16)
g = gcf;
g.WindowState = 'maximized'
  1. The first one is just the full colormap applied to the image. There are no distinct intensity ranges since each range is just a single gray level and they all blend together smoothly.
  2. The upper right is where I set every 16th gray level to appear as black to better demarcate intensity ranges. Intensity ranges will not have black boundaries around them because the black just applies on a pixel-by-pixel basis.
  3. The lower left is where I just used 8 colors instead of 32. Gray levels in each of the 8 ranges will all appear as the same uniform color. Demarcation is just done by the color changing abruptly at boundaries of the different intensity regions in the image. Black pixels will not necessarily be connected to each other.
  4. The lower right is where I just used countourf() to draw contours. Gray levels in each of the 8 ranges will all appear as the same uniform color. Demarcation is just done by tthe contourf() function drawing a black boundary at boundaries of the different intensity regions in the image. Since it's a contour, you will have continuous black lines (unlike #2 above), just like you would with a topographic elevation map.

Community Treasure Hunt

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

Start Hunting!