Matlab:在背景灰色图像上绘制彩色线条,同时保留颜色条信息
Matlab: Plot colourful line over background gray image whilst retaining colorbar information
我正在使用 MATLAB 我想在灰度 png 图像上绘制一条彩色轨迹,同时保留轨迹的颜色信息。例如,对于下面的数据,我想在图像 A 上绘制数据 B。没有数据 B 变灰,也没有使颜色条代表灰度图像。任何帮助将不胜感激!
%Image A
RGB = imread('peppers.png');
I = rgb2gray(RGB);
figure
imshow(I)
hold on
%Data B
x = 1:1000;
y = x;
z = zeros(size(x));
lineColor = x;
surface([x;x], [y;y], [z;z], [lineColor;lineColor],...
'FaceColor', 'no',...
'EdgeColor', 'interp',...
'LineWidth', 8);
cc = colorbar();
非常感谢!
MATLAB 似乎不喜欢做不止一个颜色图宠物轴。通过使用 hold on
我们将图像(灰色颜色图)和表面(例如喷气颜色图)绘制到同一个图上。默认情况下,imshow 中的非 RGB 图像的颜色图设置为灰色,因此曲面图也是如此。尝试通过调用 colormap('jet')
更改颜色图会更改图像和表面的颜色图。
似乎还有其他人遇到同样的问题:
https://uk.mathworks.com/matlabcentral/answers/194554-how-can-i-use-and-display-two-different-colormaps-on-the-same-figure
最好的解决方案似乎是为同一个图形定义两个单独的轴并将它们链接起来,以便位置信息匹配。您需要指定要绘制到哪个轴,因此 imshow
已替换为 imagesc
,后者具有更多 flexibility.Then 您可以为每个轴定义不同的颜色映射。不幸的是,颜色条可能不会每次都发挥作用,因此您需要 fiddle 了解一下它的位置信息。
您的代码实践中:
figure
%Image A
ax1 = axes;
RGB = imread('peppers.png');
I = rgb2gray(RGB);
imagesc(ax1,I)
colormap(ax1,'gray')
%Data B
ax2 = axes;
ax2.Visible = 'off'; % make transparent
x = 1:1000;
y = x;
z = zeros(size(x));
lineColor = x;
h = surface(ax2, [x;x], [y;y], [z;z], [lineColor;lineColor],...
'FaceColor', 'no',...
'EdgeColor', 'interp',...
'LineWidth', 8);
colormap(ax2,'jet') % colourful colourmap
% Gotta reposition the colourbar, tedious bit!
% Position: [left bottom width height]
cb2 = colorbar(ax2,'Position',[.91 .11 .0375 .815]);
linkaxes([ax1,ax2]) % same positions in 1st and 2nd plot
结果:
我正在使用 MATLAB 我想在灰度 png 图像上绘制一条彩色轨迹,同时保留轨迹的颜色信息。例如,对于下面的数据,我想在图像 A 上绘制数据 B。没有数据 B 变灰,也没有使颜色条代表灰度图像。任何帮助将不胜感激!
%Image A
RGB = imread('peppers.png');
I = rgb2gray(RGB);
figure
imshow(I)
hold on
%Data B
x = 1:1000;
y = x;
z = zeros(size(x));
lineColor = x;
surface([x;x], [y;y], [z;z], [lineColor;lineColor],...
'FaceColor', 'no',...
'EdgeColor', 'interp',...
'LineWidth', 8);
cc = colorbar();
非常感谢!
MATLAB 似乎不喜欢做不止一个颜色图宠物轴。通过使用 hold on
我们将图像(灰色颜色图)和表面(例如喷气颜色图)绘制到同一个图上。默认情况下,imshow 中的非 RGB 图像的颜色图设置为灰色,因此曲面图也是如此。尝试通过调用 colormap('jet')
更改颜色图会更改图像和表面的颜色图。
似乎还有其他人遇到同样的问题: https://uk.mathworks.com/matlabcentral/answers/194554-how-can-i-use-and-display-two-different-colormaps-on-the-same-figure
最好的解决方案似乎是为同一个图形定义两个单独的轴并将它们链接起来,以便位置信息匹配。您需要指定要绘制到哪个轴,因此 imshow
已替换为 imagesc
,后者具有更多 flexibility.Then 您可以为每个轴定义不同的颜色映射。不幸的是,颜色条可能不会每次都发挥作用,因此您需要 fiddle 了解一下它的位置信息。
您的代码实践中:
figure
%Image A
ax1 = axes;
RGB = imread('peppers.png');
I = rgb2gray(RGB);
imagesc(ax1,I)
colormap(ax1,'gray')
%Data B
ax2 = axes;
ax2.Visible = 'off'; % make transparent
x = 1:1000;
y = x;
z = zeros(size(x));
lineColor = x;
h = surface(ax2, [x;x], [y;y], [z;z], [lineColor;lineColor],...
'FaceColor', 'no',...
'EdgeColor', 'interp',...
'LineWidth', 8);
colormap(ax2,'jet') % colourful colourmap
% Gotta reposition the colourbar, tedious bit!
% Position: [left bottom width height]
cb2 = colorbar(ax2,'Position',[.91 .11 .0375 .815]);
linkaxes([ax1,ax2]) % same positions in 1st and 2nd plot
结果: