-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathget_window.m
executable file
·57 lines (52 loc) · 1.74 KB
/
get_window.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
50
51
52
53
54
55
56
57
function image_window = get_window(I,W,H, method)
% GET_WINDOW Gets a WxH window from the input image (I) with method
% 'method'. Possible method are 'center','random' or coordiantes.
%
%
% INPUT:
% I: input image
% W,H: width and height of the desired window
% method: {center, random, coordinates}
% center picks a centered window from the image
% random picks any random window where possible
% coordinates specify the upper left corner coords.
%
% OUTPUT: the sampled window
%
%$ Author: Jose Marcos Rodriguez $
%$ Date: N/D $
%$ Revision : 1.00 $
%% FILENAME : get_window.m
if ischar(method)
if strcmp(method,'random')
[height,width,~] = size(I);
max_x_pixel = width-W+1;
min_y_pixel = height-H+1;
rand_h = mod(round(rand()*100),min_y_pixel)+1;
rand_w = mod(round(rand()*100),max_x_pixel)+1;
image_window = I(rand_h:rand_h+H-1,rand_w:rand_w+W-1,:);
elseif strcmp(method,'center')
[height,width,~] = size(I);
x_margin = width-W+1;
y_margin = height-H+1;
x0 = max(1,floor(x_margin/2));
y0 = max(1,floor(y_margin/2));
image_window = I(y0:y0+H-1,x0:x0+W-1,:);
else
cprintf('Errors','GET_WINDOW: windows selction method not recognized\n')
image_window = [];
end
elseif isnumeric(method)
x0 = method(1);
y0 = method(2);
[height,width,~] = size(I);
if y0+H-1 <= height && x0+W-1 <= width
image_window = I(y0:y0+H-1,x0:x0+W-1,:);
else
cprintf('Errors','Window size o\n')
image_window = [];
end
else
cprintf('Errors','Method not recognized\n')
image_window = [];
end