I like the @Yuks solution. But there is another possibility that you might consider:
You can also calculate the average pixel value inside the rectangle and set the window color to the opposite. This way you will always have a good contrast.
Here is the code:
function PlotRect(im,x,y,w,h) m = double(im( round(y): round(y+h) , round(x): round(x+w),:)); if (mean(m(:)) < 255/2) col = [1 1 1]; else col = [0 0 0]; end rectangle('Position',[xywh],'EdgeColor', col); end
And the test:
function Inverse() im = imresize( uint8(0:5:255), [250, 400]) ; figure;imshow(im); hold on; PlotRect(im,5,8,50,75); PlotRect(im,100,30,25,42); PlotRect(im,200,10,40,40); PlotRect(im,300,10,40,40); end
source share