Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot inverse colors in Matlab?

I'm plotting on top of an image in Matlab. Sometimes, I can't see what's being plotted because the color of the image underneath is too close to the color of the image at the same location. I could just always change the color of the plot (e.g. from 'rx' to 'bx'), but that's cumbersome.

Is it possible to plot the inverse color of what's underneath so that the overlay is always visible?

like image 758
Benjamin Oakes Avatar asked Mar 03 '10 16:03

Benjamin Oakes


2 Answers

I believe it's not possible to automatically invert color of the plot based on background image. You probably can rasterize the plot and somehow combine it with the image (xor?).

Here is another solution. If you can use closed markers, like circle, square, triangle, you can set different MarkerEdgeColor and MarkerFaceColor, so the marker will be visible against different colors.

h = plot(1:5,'o');
set(h,'MarkerEdgeColor','b')
set(h,'MarkerFaceColor','r')
like image 115
yuk Avatar answered Oct 07 '22 19:10

yuk


This is possible.

Assuming you know what your image is like, you can do the following:

  1. Read the color at the coordinates over which you plot

  2. Invert the color

  3. Use scatter

    %# load rgb color image - this is maybe not the best example, since it's all so dark. X = double(imread('ngc6543a.jpg'))/255; %# since it's quite a dark image, invert half of it X(:,1:floor(size(X,2)/2),:) = 1-X(:,1:floor(size(X,2)/2),:);

    %# create some plot data plotX = rand(50,1) * size(X,1); plotY = rand(50,1) * size(X,2);

    %# read RGB components (it must be possible to do this more efficently, but I don't see %# it right now plotColors = zeros(length(plotX),3); for c = 1:3 plotColors(:,c) = interp2(X(:,:,c),plotY,plotX); end

    %# invert plotColors = 1-plotColors; %# If you want highly different colors, and avoid the problem that grey is the inverse %# to grey, you could use %# plotColors = round(1-plotColors); %# This gives you the choice of wrgbcmyk, whichever is farthest from the image color

    %# plot figure,imshow(X) hold on scatter(plotY,plotX,[],plotColors)

Edit: this has now been tested and it should work.

Edit2: inverting half the original image makes it clearer how this works

Edit3: incorporated a modified form of gnovice's suggestion

Edit4: fixed the bug as pointed out by AB

like image 44
Jonas Avatar answered Oct 07 '22 19:10

Jonas