How to make a color scale like this?

21 vues (au cours des 30 derniers jours)
pink flower
pink flower le 9 Déc 2020
Modifié(e) : Image Analyst le 9 Déc 2020
Hello. How can I make a color scale with several different colors? I would like a scale exactly like this, with 100 colors. Thanks!

Réponses (2)

David Goodmanson
David Goodmanson le 9 Déc 2020
Hi pf,
I copied in the image in your posting (a jpeg) and called it image1. That results in a 3d array that is 180x882x3 uint8, with values from 0 to 255. Then
figure(1)
imagesc(image1)
% on the resulting image, use zoom and datatips to find array coordinates at each end
% of the image colorbar, and in the middle of the colorbar heightwise.
% good choices are (25,110) and (868,110)
% determine the colormap. Divide by 255 since colormap values run from 0 to 1.
rgbX = double(squeeze(image1(110,25:868,:)))/255; % 844x3
% interpolate down to 100 colors, put colorbar on the original image
n = 100;
rgbXn = interp1(1:844,rgbX,linspace(1,844,n)); % 100x3
colormap(rgbXn)
colorbar

Image Analyst
Image Analyst le 9 Déc 2020
Modifié(e) : Image Analyst le 9 Déc 2020
You can determine it from your image. Essentially you can use the improfile() function:
colorMap = improfile(rgbImage, xi, yi, 100)
colorMap = squeeze(colorMap) / 255;
Here's a full demo I created for you (also attached with reference image):
% Demo to find colormap from an image of a colorbar.
% By Image Analyst, December 8, 2020.
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clearvars;
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
fprintf('Beginning to run %s.m ...\n', mfilename);
%-----------------------------------------------------------------------------------------------------------------------------------
% Read in image of colorbar.
folder = [];
baseFileName = 'colorbar.jpeg';
fullFileName = fullfile(folder, baseFileName);
% Check if file exists.
if ~exist(fullFileName, 'file')
% The file doesn't exist -- didn't find it there in that folder.
% Check the entire search path (other folders) for the file by stripping off the folder.
fullFileNameOnSearchPath = baseFileName; % No path this time.
if ~exist(fullFileNameOnSearchPath, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist in the search path folders.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
rgbImage = imread(fullFileName);
[rows, columns, numberOfColorChannels] = size(rgbImage)
% Display the test image full size.
subplot(3, 1, 1);
imshow(rgbImage, []);
axis('on', 'image');
caption = sprintf('Reference Image : "%s"', baseFileName);
title(caption, 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
hp = impixelinfo(); % Set up status line to see values when you mouse over the image.
% Set up figure properties:
% Enlarge figure to full screen.
hFig1 = gcf;
hFig1.Units = 'Normalized';
hFig1.WindowState = 'maximized';
% Get rid of tool bar and pulldown menus that are along top of figure.
% set(gcf, 'Toolbar', 'none', 'Menu', 'none');
% Give a name to the title bar.
hFig1.Name = 'Demo by Image Analyst';
% Get the profile of line y=112 and x=25 and x=869
x1 = 25;
x2 = 869;
xi = [x1, x2]; % Take 100 samples
yi = [112, 112];
%--------------------------------------------------------------------------------------
% HERE IS THE MAIN PART OF THE ROUTINE:
% Create the colormap from the RGB profile through the relevant part of the image.
colorMap = improfile(rgbImage, xi, yi, 100)
% Colormap needs to be between 0 and 1, so divide by 255.
colorMap = squeeze(colorMap) / 255;
%--------------------------------------------------------------------------------------
% Plot r, g, and b components.
subplot(3, 1, 2);
plot(colorMap(:, 1), 'r-', 'LineWidth', 2); % Plot red component.
hold on;
plot(colorMap(:, 2), 'g-', 'LineWidth', 2); % Plot green component.
plot(colorMap(:, 3), 'b-', 'LineWidth', 2); % Plot blue component.
grid on;
title('Color Map Profiles', 'FontSize', fontSize, 'Interpreter', 'None');
drawnow;
% Create a indexed (gray scale) image and apply our new colormap with the imshow() function.
indexedImage = uint8(repmat(1:100, [10, 1])); % Image is 100 pixels wide and 10 pixels tall.
subplot(3, 1, 3);
imshow(indexedImage, 'ColorMap', colorMap);
caxis([0, 99]);
impixelinfo; % Let user mouse around and see the indexed value (not the mapped RGB value).
colorbar; % Put up a colorbar on the right hand side.
axis('on', 'image');
title('Indexed Image with Color Map Applied', 'FontSize', fontSize, 'Interpreter', 'None');
fprintf('Done running %s.m ...\n', mfilename);

Catégories

En savoir plus sur Blue dans Help Center et File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by