How can I create a rectangle with an outlined border? - matlab

I want to draw a rectangle to outline an area of an image that I've plotted in one axes of a figure. I have multiple axes in this figure, so I am using the rectangle() function. What I want is to draw a white rectangle a thin black border just inside and just outside the rectangle. The part of the image inside the rectangle should be visible, so all 'facecolor' should be 'none'. I have tried drawing 3 rectangles, two black ones with thin linewidths and one thicker white one, but the problem is that 'Position' is defined in axes units and 'LineWidth' is defined in point units, so that the scaling doesn't work too well, especially when the figure is resized.
FYI, the outline is so that the white rectangle is more visible against a light background. The images plotted vary widely, so a single color won't be universally visible for my data.
Any suggestions on how I can do this?

How about just using different line widths for black and white rectangle?
imshow('cameraman.tif')
rectangle('position',[80 30 100 100],'edgecolor','k','LineWidth',4)
rectangle('position',[80 30 100 100],'edgecolor','w','LineWidth',1)
Hmm, the corners look much better on MATLAB figure than as PNG file.
Better with getframe:

I like #Yuks solution. But there is another possibility that you can consider:
You could also calculate the mean value of the pixels inside the rectangle, and set the box color to the inverse. In 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',[x y w h],'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

Yuk's solution works quite well for adding a rectangle to a normal MATLAB plot, too. The 'position' values are not interpretet as pixels but are adjusted to the plot values (see code example below):
figure;
plot(0:10,0:10); grid on;
hold on;
rectangle('position',[1 1 8.5 8.5],'LineWidth',2);
hold off;
This code results in the following plot:

Related

Border of a scatter plot in Matlab: constructing it and colouring it

I would like your help to plot the border of a scatter in Matlab and fill it with a specified colour.
Let me firstly show you how the scatter looks like.
scatter(x,y)
Now, let me show you what I have tried to design the border.
k = boundary(x,y);
plot(x(k),y(k));
which produces this
This is not what I want. Firstly, the scatter is nicely convex and I don't understand why boundary produces that weird shape. Secondly, I want to colour with blue inside the border.
Could you kindly advise on how to proceed? Also, when building the border I would prefer not to rely on the convexity of the scatter, because in some of my real examples the scatter is not convex.
Boundary
There is no easy way to get the boundary of a non-convex shape without any information on what is expected. When should the boundary follow the convex hull and when should deviate from it?
Matlab function boundary is generic, made to work with arbitrary input coordinates. It has a 'shrink factor' parameter s that can be tuned between 0 (convex hull) and 1 (highly non-convex), depending on the expected result.
% Make some x,y data with a missing corner
[x,y]=meshgrid(1:20);
sel=~(x>15 & y<=5);
x=x(sel);y=y(sel);
% Compute boundary with several values for s
k=boundary([x,y]); %Default shrink factor : 0.5
k_convhull=boundary([x,y],0);
k_closest=boundary([x,y],1);
% Plot the boundary:
plot(x,y,'ok') %black circles
hold on
plot(x(k),y(k),'-r') %red line
plot(x(k_convhull),y(k_convhull),'-g') %green line
plot(x(k_closest),y(k_closest),'-b') %blue line
The re-entrant corner is still somewhat cut, even with the 'shrink factor' set to 1. If this does not suit you, you will need to make your own function to compute the coordinates of the border.
Plot a surface
You need to use a patch object there.
p=patch(x(k3),y(k3),[1 .8 .8]);
uistack(p,'bottom')
[1 .8 .8] is the RGB color of the patch surface (light pink)
uistack is there to move the patch below the other graphic objects, because it is not transparent. It will still cover the axes grid, if any. Another option is :
p=patch(x(k3),y(k3),[1 0 0],'FaceAlpha',.2);
Here, the patch will be plain red and drawn on top of other objects, but has 80% transparency set through the alpha channel (0 is fully transparent, 1 is solid color).
Beware, the patch object class has so many options that is somewhat complex to handle beyond basic use cases like this one.

How to fix contourf plot interpolating across NaN region?

I'm trying to create a countourf plot with the region in the bottom left hand corner whited out. Values for the contour are provided at the corner of each rectangle in the below image and all other points on the mesh have a value of NaN.
I want to know how to stop the countourf plot from drawing the triangular section at the top right of the white square, i.e. how do I stop it from interpolating across these two values.
End game: I would like a complete white rectangle on the bottom left, not a chamfered rectangle.
You are getting that triangle because on that specific square your data looks something like:
[1 2 ;...
NaN 3]
And that is a completely valid upper right triangle to contour.
So you can interpolate your data to get more resolution and make that triangle smaller. Or you could just use patch to add a white square at the desired position.
data=rand(8);
data(1:4,1:4)=NaN;
contourf(data)
hold on;
patch([1 1 5 5],[1 5 5 1],'w')
It looks like seven squares a side, so we can set
x = 1:7;
[XX,YY] = meshgrid(x); % create x,y grid for the square
ZZ = nan(7); % create number grid for the square
ZZ(1:3,1:3)=ones(3); % set the lower 3x3 to 1
figure;
hold on % hold your plot
plot()% your contour
imagesc(XX,YY,ZZ); % Or similar
This is the outline, I can't determine what exact plotting function you need since you didn't show your code. In general, the idea is to create a grid as large as that of your contour plot, and set the lower left square to 1, thus white, leaving the rest NaN, thus not plotted.

How to plot contours with selected colors and formatted labels

I'm trying to plot contour using my computed data with limited contour labels and and colors as given in the top panel of this image:
But I ended up with a slightly different plot (see the plot in the bottom of the above image).
I want to modify my plot with the following three specifications
Restrict contour labels in 2 or 3 decimal places
Remove plot labels in the area where the contours are too close to each other.
Plot with two colors as in the first image
Here is my code:
f=load('fort.15');
ngridx=180;
ngridy=180;
x=f(:,3);
y=f(:,4);
z=f(:,5);
xlin=linspace(min(x),max(x),ngridx);
ylin=linspace(min(y),max(y),ngridy);
[X,Y]=meshgrid(xlin,ylin);
Z=griddata(x,y,z,X,Y,'linear');
[c,h] = contour(X,Y,Z,20);
set(h,'LineWidth',2,'LineColor',rgb('SteelBlue'),'ShowText','on',...
'LabelSpacing',800 )
axis([0 6 -5 7])
I'm not an expert in Matlab. Please help me get the right plot.
I'm attaching my data file here.
Well, I got only 2 of 3. Deine the level in which the color has to change (here scl) and you good to go:
scl = 6.5; % switch color level;
[c1,h1] = contour(X,Y,Z,scl:max(Z(:)),'Color','r');
hold on
[c2,h2] = contour(X,Y,Z,min(Z(:)):scl,'Color','b');
clabel(c2,h2);
axis([0 6 -5 7])
The idea here is to builed your plot from two contour objects, using hold on command. the vector scl:max(Z(:)) define the levvels to show in the first contour, and the get the red color and no lables. And a similar logic works for the secound contour.
If you want to lable some red contours, or remove lables from the blue ones, you need to replace h2 in the clabel function with a vector of the levels you want to lable. If you will be mo specific in the comments I'll update my answer.
Changing the formatting of the lables, is probably possible somehow, but it's really not trivial, so I left it by now.

Matlab: (1) Plotting multiple canvases, (2) holding them separately, (3) montage all

I've got 10 grayscale images. I'd like to plot a simple YELLOW line over each image separately, then show them all over one plot (montage style).
I tried to draw all images first, but that made plotting lines very tricky (X,Y axes weren't standard for plotting over each separate image).
I thought about burning the line over the image, but I don't have the computer vision toolkit (easy functions to do this), otherwise it seemed complicated to both convert the grayscale to color and get it to burn the image.
I thought I might be able to use the function newplot to create a temporary plot space for each image, draw the line with a simple plot(...) call, then save it and just montage(...) all the individual plots at the end.
Is this possible? I've never played with the function newplot or tried to loop through individual plots, saving them up for a call to montage(...) this way, but it seems like a logical/simple approach.
I finally worked it out with subplot, subimage, and plot, using subplot with the position arguments does what I want easily enough. Using subplot kept the axis relative to the subplot I was on so I could plot the line with a standard fplot/plot call. The trick was normalizing the position to percentages vs. thinking of it in terms of pixels.
here's some code demoing it:
% Loop through this code, each time moving the subplot by position
LOOP {
% calculate left & bottom position as percentages (0..1)
subplot( 'Position', [ left bottom (1/cols) (1/rows) ] );
hold on
% (1) Draw the image
subimage(tmpImg, [0 255]);
axis off;
% (2) Plot the line over the original image
F = #(x) polyval(p, x);
fplot(F, [1 dimX 1 dimY], '-y');
}

Matlab image legend with color blocks

I'm trying to make a legend for a colormapped image that's been made from a label image and custom color map using label2rgb. Basically I want a legend that shows small rectangular samples of the colormap colors along with custom labels.
I would like to be able to set the color of the labels and the background. The image is displayed using imshow, and apparently legend has no effect on such figures.
I've tried a little hack around this using colorbar as shown below, but I'm pretty unsatisfied with it since if I resize the plot window everything gets thrown off.
Can anyone tell me how this type of legend should be made, preferably in a way so it floats and moves itself as necessary when the figure window is resized. If this is not possible, then can someone tell me how to either color the label text of a colorbar or give the colorbar's bounding box (encompassing its labels) a background color? As you can see, if I hadn't manually added a background white box, the labels would be invisible.
Thanks.
label_image = zeros(768, 1024);
label_image(100:400, 500:600)=1;
label_image(500:600, 100:600)=2;
label_image(25:300, 100:400)=3;
custom_colormap = [ 0 1 0; 1 0 0; 0 0 1;];
label_image = label2rgb(label_image, custom_colormap, [0 0 0]);
% Make a white backround for the colorbar.
% Don't want to have to do this line.
label_image(25:140, 775:1010,:)=255;
% Show the image
imshow(label_image, 'InitialMagnification', 50);
% Here, I would like to be able to set colorbar label text color and/ or
% colorbar bounding box background color, or better yet, use some variant
% on the legend function to do this automatically
colormap(custom_colormap);
labels={'Both', 'Always Moving', 'Moved'};
hcb=colorbar('peer',gca,...
[0.7 0.8 0.03 0.1],...
'YTickLabel',labels,...
'XAxisLocation','bottom',...
'TickLength',[0 0],...
'Color',[1 0 1]);
You want to set the color of the x and y axes of the colorbar, respectively.
set(hcb,'ycolor','w','xcolor','w')
Thus, you wont have to put the white rectangle. Check get(hcb) to see a list of all the properties you can modify.