-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdrawPatchRect.m
49 lines (44 loc) · 1.74 KB
/
drawPatchRect.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
function rect = drawPatchRect(patchloc, patchSize, color)
% DRAWPATCHRECT draw rectangles for a patch in the current axis
% rect = drawPatchRect(patchloc, patchSize) draw rectangles for a patch in the current axis.
% patchloc is a Nx2 vec: [y, x] (note that this is opposite of matlab usual 2D notation. We do
% this to keep with usual n-d notation) specifies the top left corner of the patch. patchSize is
% 1x2 or Nx2 vector [y_i, x_i]. Returns the rectangle object.
%
% rect = drawPatchRect(patchloc, patchSize, color) allows for the specification of color, a Nx3 or
% Nx1 cell of strings
%
% Warning: currently implemented only for 2D
%
% Example:
% patchview.figure();
% imagesc(peaks(100));
% colormap gray;
% patchview.drawPatchRect([10, 50; 10, 55], [12, 7], {'r', 'b'});
%
% Contact: [email protected]
nPatches = size(patchloc, 1);
if nargin == 2
color = repmat({'b'}, [nPatches, 1]);
end
assert(size(patchloc, 2) == 2, 'Currently, drawPatchRect is only implemented for 2D');
if ~iscell(color)
if ischar(color);
color = {color};
else
color = mat2cell(color, ones(size(color, 1)), size(color, 2));
end
end
if size(patchSize, 1) == 1
patchSize = repmat(patchSize, [nPatches, 1]);
end
% set linewidth to be about 0.1 * size of the pixel
set(gca,'Units', 'pixels');
pos = get(gca, 'Position');
volSize = get(gca, 'xlim');
lineWidth = 0.1 .* pos(3) ./ diff(volSize);
% draw rectangle around patch in image
for i = 1:size(patchloc, 1)
pos = fliplr([patchSize(i, :), patchloc(i, :) - 0.5]);
rect = rectangle( 'Position', pos, 'LineWidth', lineWidth, 'EdgeColor', color{i});
end