Plotting colourful line over gray image whilst retaining colourbar information

15 views (last 30 days)
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();

Accepted Answer

Prachi Kulkarni
Prachi Kulkarni on 22 Sep 2021
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 Comments
Manny Kins
Manny Kins on 30 Sep 2021
Many thanks for your help and explanation and sorry for the late response! Take care

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!

Translated by