MATLAB: Does the color or intensity of the image severely degrade when I place multiple multi-colored line objects on top of the image

changecolormapdegradationMATLABpainter

When many multi-colored line objects (points) are overlaid on a (color or grayscale) image, severe (color or intensity) degradation occurs progressively. If all line objects have the same color this degradation does not occur. If ~100 line objects are placed on the image then the degradation does not occur.
This can be reproduced with the following code:
imshow('cameraman.tif');
%imshow('trees.tif'); % uncomment for color image
set(gcf, 'renderer', 'painters');
hold on;
CC = [ones(500,1), linspace(0,1,500)', zeros(500,1)]; % colors for line objects
X = (rand(500,1))*255; % random x-data for plot
Y = (rand(500,1))*255; % random y-data for plot
for i = 1: 500
plot(X(i,:),Y(i,:), '.', 'Color', CC(i,:)); % multi-colored
%plot(X(i,:),Y(i,:), '.'); % uncomment to see single-colored
drawnow;
end

Best Answer

This is not a bug but rather an attribute of the painters renderer. Currently this renderer by design is not intended to show more than 256 colors. When lines use up single RGB colors, then those are taken from the image.
To work around this issue change the figure renderer to either ZBuffer or OpenGL using the SET function as follows:
set(gcf, 'Renderer', 'zbuffer')
% or
set(gcf, 'Renderer', 'opengl')