Plotting colourful line over gray image whilst retaining colourbar information

5 次查看(过去 30 天)
Hi, I would like to plot a colourful trajectory on top of a grayscale image whilst retaining the colour information of the trajectory. For example with the data below, I would like to plot Data B over Image A. Without Data B turning gray and without making the colourbar represent the grayscaled image. Any help would be greatly appreciated!
%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();

采纳的回答

Prachi Kulkarni
Prachi Kulkarni 2021-9-22
Hi,
To plot a color line over the grayscale image A, the image should be represented in RGB format even though the colors are in grayscale. For this you will need to add one line to your code.
Following is the modified code with one extra line.
%Image A
RGB = imread('peppers.png');
I = rgb2gray(RGB);
% -----------------------------------------------------
% Add this line to your code
I = cat(3,I,I,I);
% -----------------------------------------------------
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();
  3 个评论
Prachi Kulkarni
Prachi Kulkarni 2021-9-23
Hi,
The color line is not showing the full range of colors, because the color is ranging from 1 to 1000, but the line is stopped at 384 due to the size of the grayscale image.
To get the full range of colors, x will have to range from 1 to the smallest dimension of the grayscale image, which is 384 in this case.
Following is the modified code.
%Image A
RGB = imread('peppers.png');
I = rgb2gray(RGB);
% -----------------------------------------------------
% Add these lines to your code
lineLength = min(size(I));
I = cat(3,I,I,I);
% -----------------------------------------------------
figure
imshow(I)
hold on
%Data B
% -----------------------------------------------------
% Modify this line in your code
x = 1:lineLength;
% -----------------------------------------------------
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();

请先登录,再进行评论。

更多回答(0 个)

类别

Help CenterFile Exchange 中查找有关 Colormaps 的更多信息

产品


版本

R2021a

Community Treasure Hunt

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

Start Hunting!

Translated by