How can I make a 2D color map?
508 ビュー (過去 30 日間)
古いコメントを表示
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
採用された回答
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 件)
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
2020 年 9 月 4 日
編集済み: Image Analyst
2020 年 9 月 4 日
Your question asked 2 things
- How to create a 2-D colormap, and
- 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
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 Center および File Exchange で Red についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!