How can I make a 2D color map?

조회 수: 173 (최근 30일)
Richard Ott
Richard Ott 2020년 9월 4일
댓글: Bruno Luong 2020년 9월 4일
Hi everyone,
I want to color data in a plot by using 2 color coordinates to achieve basically a 2D colormap.
Here's a link to the kind of coloramps I'm talking about https://dominikjaeckle.com/projects/color2d/
I couldn't find anything for Matlab on how to create such a coloarmap. Any ideas?
Thanks so much in advance.
Cheers
Richard
  댓글 수: 2
Richard Ott
Richard Ott 2020년 9월 4일
No, the link you send is for a normal 1D color map. You specify one value and that's what it's colored by.
What I mean is a colormap with gradients in two direction as specified by a value pair (as shown in the link that I posted).

댓글을 달려면 로그인하십시오.

채택된 답변

Bruno Luong
Bruno Luong 2020년 9월 4일
편집: Bruno Luong 2020년 9월 4일
Something like this
R=[1 0;
1 0];
G=[1 1
0 0];
B=[0 0
0 1];
R = interp2(R,8);
G = interp2(G,8);
B = interp2(B,8);
I = uint8(255*cat(3,R,G,B));
image(I)
  댓글 수: 1
Richard Ott
Richard Ott 2020년 9월 4일
YES. Perfect! Thanks!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Image Analyst
Image Analyst 2020년 9월 4일
Try this. The code creates an RGB image to use as a colormap. Then it creates a scatterplot and uses the RGB image to decide the colors of the markers:
% Initialization steps.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
%==================================================================================================
% Show lab color space for values of L = 50.
numColors = 256;
ramp = linspace(-100,100, numColors);
figure;
cform = makecform('lab2srgb');
a = repmat(ramp, [numColors 1]); % -a on left
b = repmat(flipud(ramp'), [1 numColors]); % -b on bottom
L = 50 * ones(numColors, numColors); % A single L value.
Lab = cat(3, L, a, b); % A 2D image.
colormap2D = applycform(Lab, cform);
% Display it.
subplot(2, 1, 1);
imshow(colormap2D);
axis on;
caption = sprintf('2D Colormap');
title(caption, 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
drawnow;
% Now that RGB image is made up (to be a 2-D colormap), make scatterplot.
numPoints = 1000;
amplitude = 3000;
x = amplitude * rand(1, numPoints);
y = amplitude * rand(1, numPoints);
subplot(2, 1, 2);
grid on;
thisColor = zeros(numPoints, 3);
for k = 1 : numPoints
col = ceil(x(k) * numColors / amplitude);
row = ceil(y(k) * numColors / amplitude);
thisColor(k, :) = [colormap2D(row, col, 1), colormap2D(row, col, 2), colormap2D(row, col, 3)];
fprintf('(x,y) = (%6.1f, %6.1f), row = %3d, col = %3d, thisColor = (%.4f, %.4f, %.4f)\n', x(k), y(k), row, col, thisColor(k, :));
% plot(x(k), y(k), '.', 'Color', thisColor, 'MarkerSize', 30);
% hold on;
end
scatter(x, y, 30 * ones(1, numPoints), thisColor, 'filled');
axis('square');
grid on;
xlim([0, amplitude]);
Of course you could adapt it to use any RGB image as a colormap.
  댓글 수: 3
Image Analyst
Image Analyst 2020년 9월 4일
편집: Image Analyst 2020년 9월 4일
Your question asked 2 things
  1. How to create a 2-D colormap, and
  2. How to plot markers using colors from that colormap.
I thought I'd better add the part where I gave a scatterplot since you asked for that but the answer you accepted did not have that.
I don't know what you mean by the main colors. If you want to rotate the image, you can do
colormap2D = imrotate(colormap2D, 45);
but like I said, the scatterplot code where I make up the set of colored markers can use any image whatsoever. Just make it up however you want. To drive home that point, here is a demo where I use the peppers demo image as the colormap:
% Initialization steps.
clc; % Clear the command window.
fprintf('Beginning to run %s.m ...\n', mfilename);
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 20;
%==================================================================================================
colormap2D = imread('peppers.png');
[numColorsY, numColorsX, numberOfColorChannels] = size(colormap2D)
% Display it.
subplot(2, 1, 1);
imshow(colormap2D);
axis on;
caption = sprintf('2D Colormap');
title(caption, 'FontSize', fontSize);
% Enlarge figure to full screen.
set(gcf, 'units','normalized','outerposition',[0 0 1 1]);
drawnow;
% Divide by image colormap 255 to get into the range 0-1. It's originally 0-255 which is not the right range for a colormap.
colormap2D = double(colormap2D) / 255;
% Now that RGB image is made up (to be a 2-D colormap), make scatterplot.
numPoints = 1000;
amplitude = 3000;
x = amplitude * rand(1, numPoints);
y = amplitude * rand(1, numPoints);
subplot(2, 1, 2);
grid on;
thisColor = zeros(numPoints, 3);
for k = 1 : numPoints
col = ceil(x(k) * numColorsX / amplitude);
row = ceil(y(k) * numColorsY / amplitude);
thisColor(k, :) = double([colormap2D(row, col, 1), colormap2D(row, col, 2), colormap2D(row, col, 3)]);
fprintf('(x,y) = (%6.1f, %6.1f), row = %3d, col = %3d, thisColor = [%.4f, %.4f, %.4f]\n', x(k), y(k), row, col, thisColor(k, :));
% plot(x(k), y(k), '.', 'Color', thisColor, 'MarkerSize', 30);
% hold on;
end
scatter(x, y, 30 * ones(1, numPoints), thisColor, 'filled');
axis('square');
grid on;
xlim([0, amplitude]);
title('scatterplot', 'FontSize', fontSize);
If this is helpful, can you Vote for my Answer? Thanks in advance.
Bruno Luong
Bruno Luong 2020년 9월 4일
"In both answer the gradients start in the corner. I'm trying to think if there's a way to have the main colors on the middle of the side-edgdes. Basically, a 45° rotation of this colormap."
There is a reason for that.
If you define the colors at 4 corners, color inside is an interpolation, because all points inside the rectangle is the barycentric combination of the four corners.
If you define the colors at 4 midlles points of the edges, this is no longer true, and you need to extrapolate to get to the corner. The extrapolation results can be unexpected (strange colors, overflow RGB range, etc...)

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Blue에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by