forked from LanceOptican/lmoMatlab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
crop_borders.m
81 lines (79 loc) · 1.63 KB
/
crop_borders.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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
%CROP_BORDERS Crop the borders of an image or stack of images
%
% [B, v] = crop_borders(A, bcol, [padding])
%
%IN:
% A - HxWxCxN stack of images.
% bcol - Cx1 background colour vector.
% padding - scalar indicating how many pixels padding to have. Default: 0.
%
%OUT:
% B - JxKxCxN cropped stack of images.
% v - 1x4 vector of start and end indices for first two dimensions, s.t.
% B = A(v(1):v(2),v(3):v(4),:,:).
function [A, v] = crop_borders(A, bcol, padding)
if nargin < 3
padding = 0;
end
[h, w, c, n] = size(A);
if isscalar(bcol)
bcol = bcol(ones(c, 1));
end
bail = false;
for l = 1:w
for a = 1:c
if ~all(col(A(:,l,a,:)) == bcol(a))
bail = true;
break;
end
end
if bail
break;
end
end
bcol = A(ceil(end/2),w,:,1);
bail = false;
for r = w:-1:l
for a = 1:c
if ~all(col(A(:,r,a,:)) == bcol(a))
bail = true;
break;
end
end
if bail
break;
end
end
bcol = A(1,ceil(end/2),:,1);
bail = false;
for t = 1:h
for a = 1:c
if ~all(col(A(t,:,a,:)) == bcol(a))
bail = true;
break;
end
end
if bail
break;
end
end
bcol = A(h,ceil(end/2),:,1);
bail = false;
for b = h:-1:t
for a = 1:c
if ~all(col(A(b,:,a,:)) == bcol(a))
bail = true;
break;
end
end
if bail
break;
end
end
% Crop the background, leaving one boundary pixel to avoid bleeding on resize
v = [max(t-padding, 1) min(b+padding, h) max(l-padding, 1) min(r+padding, w)];
A = A(v(1):v(2),v(3):v(4),:,:);
end
function A = col(A)
A = A(:);
end