- Read in a cropped image of the colorbar and extract the RGB values
- Inpterpolate the RGB values to create a colormap with the specified number of colors.
Exact replication of a colorbar
4 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
Richard Wood
am 24 Mär. 2023
Bearbeitet: DGM
am 24 Mär. 2023
Dear all
I have been asked to replicate the exact colorbar scheme that I put in the attached figure. How could I do this?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1335179/image.png)
0 Kommentare
Akzeptierte Antwort
Adam Danz
am 24 Mär. 2023
Bearbeitet: Adam Danz
am 24 Mär. 2023
This solution has 2 steps:
Image attached. The cropped colorbar image will be read down the vertical midline. It's important that no extra colors from the border or background appear at the top or bottom.
% Read in colorbar image
file = 'colorbarimage.png';
A = imread(file);
% get color from the center vertical line
centerIdx = floor(size(A,2)/2);
rgbMidline = flipud(double(squeeze(A(:,centerIdx,:)))./255);
Specify the number of interpolated colors you'd like
% How many color segments should there be?
nColors = 300;
Produce the colormap cmap
x = linspace(1, nColors, size(rgbMidline,1));
cmap = interp1(x, rgbMidline, 1:nColors);
Take a look
figure()
imageSize = [5000,4800];
blocksize = round(imageSize/32);
data = randi(2,fliplr(blocksize))-1;
B = imresize(data, fliplr(imageSize));
imagesc(B)
colormap([0 0 0; 1 1 1])
axis equal
axis tight
colormap(cmap)
colorbar
0 Kommentare
Weitere Antworten (2)
DGM
am 24 Mär. 2023
Bearbeitet: DGM
am 24 Mär. 2023
I'm going to do this differently and attempt to reconstruct the original map from its approximate breakpoints, thus removing much of the image noise. This is merely a demonstration.
I'm going to crop the relevant image region manually in GIMP because I'm lazy and hate dealing with MATLAB view controls. I'm starting with this image.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1335319/image.png)
Then I do the following. This relies on MIMT tools and manual point selection and some discretion.
inpict = imread('image.png');
% use MIMT tools to view the CT in RGB
CTx = ctflop(mean(im2double(inpict),2));
ctpath(CTx,'rgb','invert','2D');
% manually reconstruct the CT by finding its breakpoints
x0 = [1 92 140 186 234 284 327 374];
CT0 = CTx(x0,:);
% try to fix the values near noisy breakpoints
CT0(6,:) = CTx(301,:);
CT0(7,1) = CTx(301,1);
CT0(end,:) = [1 0.52 0.32];
Once I've manually worked through those points to build the short color table, I can interpolate and compare the results.
% interpolate
N = 374;
xf = linspace(1,max(x0),N);
CT = interp1(x0,CT0,xf,'linear');
% compare
comp = [ctflop(CTx) ctflop(CT)];
comp = imresize(comp,[374 150],'nearest');
clf; imshow(comp)
imwrite(comp,'comp.png')
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1335324/image.png)
The left half is the original sweep taken from the image, with all its periodic artifacts. The right half is the reconstructed equivalent.
I can then write a rudimentary helper function to generate said map whenever I want.
function cset = mymap(steps)
% CMAP = MYMAP({STEPS})
% Custom colormap generator
%
% STEPS optionally specifies the CT length (default 64)
if nargin == 1
steps = 64;
end
CT0 = [0.00392 0.00784 0.0157; 0.0451 0.38 0.384; 0.129 0.78 0.78; 0.973 0.993 0.994;
0.545 0.879 0.872; 0.996 0.796 0.619; 0.996 0.792 0.619; 1 0.52 0.32];
x0 = [1 92 140 186 234 284 327 374];
xf = linspace(1,max(x0),steps);
cset = interp1(x0,CT0,xf,'linear');
end
There you have it.
It's worth noting that this colormap is self-intersecting. People complain about rainbow colormaps being misleading, but actually reversing the trajectory in the middle of the map and using a set of colors twice for different values is another entire new level of visual confusion. I'll add that for a large region following that reversal, the map is actually constant. The peach-colored region has no color variation, so data variation in that region will be invisible. I'll trust you to know what you're doing with that.
0 Kommentare
Image Analyst
am 24 Mär. 2023
See my attached demo. It lets you drag a box over the colorbar in the image and it converts that to a N-by-3 colormap which you can then apply to other gray scale images.
0 Kommentare
Siehe auch
Kategorien
Mehr zu Red finden Sie in Help Center und File Exchange
Produkte
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!