1 | function [A, vA, vB, bb_rel] = crop_borders(A, bcol, padding, crop_amounts)
|
---|
2 | %CROP_BORDERS Crop the borders of an image or stack of images
|
---|
3 | %
|
---|
4 | % [B, vA, vB, bb_rel] = crop_borders(A, bcol, [padding])
|
---|
5 | %
|
---|
6 | %IN:
|
---|
7 | % A - HxWxCxN stack of images.
|
---|
8 | % bcol - Cx1 background colour vector.
|
---|
9 | % padding - scalar indicating how much padding to have in relation to
|
---|
10 | % the cropped-image-size (0<=padding<=1). Default: 0
|
---|
11 | % crop_amounts - 4-element vector of crop amounts: [top,right,bottom,left]
|
---|
12 | % where NaN/Inf indicate auto-cropping, 0 means no cropping,
|
---|
13 | % and any other value mean cropping in pixel amounts.
|
---|
14 | %
|
---|
15 | %OUT:
|
---|
16 | % B - JxKxCxN cropped stack of images.
|
---|
17 | % vA - coordinates in A that contain the cropped image
|
---|
18 | % vB - coordinates in B where the cropped version of A is placed
|
---|
19 | % bb_rel - relative bounding box (used for eps-cropping)
|
---|
20 |
|
---|
21 | %{
|
---|
22 | % 06/03/15: Improved image cropping thanks to Oscar Hartogensis
|
---|
23 | % 08/06/15: Fixed issue #76: case of transparent figure bgcolor
|
---|
24 | % 21/02/16: Enabled specifying non-automated crop amounts
|
---|
25 | % 04/04/16: Fix per Luiz Carvalho for old Matlab releases
|
---|
26 | % 23/10/16: Fixed issue #175: there used to be a 1px minimal padding in case of crop, now removed
|
---|
27 | %}
|
---|
28 |
|
---|
29 | if nargin < 3
|
---|
30 | padding = 0;
|
---|
31 | end
|
---|
32 | if nargin < 4
|
---|
33 | crop_amounts = nan(1,4); % =auto-cropping
|
---|
34 | end
|
---|
35 | crop_amounts(end+1:4) = NaN; % fill missing values with NaN
|
---|
36 |
|
---|
37 | [h, w, c, n] = size(A);
|
---|
38 | if isempty(bcol) % case of transparent bgcolor
|
---|
39 | bcol = A(ceil(end/2),1,:,1);
|
---|
40 | end
|
---|
41 | if isscalar(bcol)
|
---|
42 | bcol = bcol(ones(c, 1));
|
---|
43 | end
|
---|
44 |
|
---|
45 | % Crop margin from left
|
---|
46 | if ~isfinite(crop_amounts(4))
|
---|
47 | bail = false;
|
---|
48 | for l = 1:w
|
---|
49 | for a = 1:c
|
---|
50 | if ~all(col(A(:,l,a,:)) == bcol(a))
|
---|
51 | bail = true;
|
---|
52 | break;
|
---|
53 | end
|
---|
54 | end
|
---|
55 | if bail
|
---|
56 | break;
|
---|
57 | end
|
---|
58 | end
|
---|
59 | else
|
---|
60 | l = 1 + abs(crop_amounts(4));
|
---|
61 | end
|
---|
62 |
|
---|
63 | % Crop margin from right
|
---|
64 | if ~isfinite(crop_amounts(2))
|
---|
65 | bcol = A(ceil(end/2),w,:,1);
|
---|
66 | bail = false;
|
---|
67 | for r = w:-1:l
|
---|
68 | for a = 1:c
|
---|
69 | if ~all(col(A(:,r,a,:)) == bcol(a))
|
---|
70 | bail = true;
|
---|
71 | break;
|
---|
72 | end
|
---|
73 | end
|
---|
74 | if bail
|
---|
75 | break;
|
---|
76 | end
|
---|
77 | end
|
---|
78 | else
|
---|
79 | r = w - abs(crop_amounts(2));
|
---|
80 | end
|
---|
81 |
|
---|
82 | % Crop margin from top
|
---|
83 | if ~isfinite(crop_amounts(1))
|
---|
84 | bcol = A(1,ceil(end/2),:,1);
|
---|
85 | bail = false;
|
---|
86 | for t = 1:h
|
---|
87 | for a = 1:c
|
---|
88 | if ~all(col(A(t,:,a,:)) == bcol(a))
|
---|
89 | bail = true;
|
---|
90 | break;
|
---|
91 | end
|
---|
92 | end
|
---|
93 | if bail
|
---|
94 | break;
|
---|
95 | end
|
---|
96 | end
|
---|
97 | else
|
---|
98 | t = 1 + abs(crop_amounts(1));
|
---|
99 | end
|
---|
100 |
|
---|
101 | % Crop margin from bottom
|
---|
102 | bcol = A(h,ceil(end/2),:,1);
|
---|
103 | if ~isfinite(crop_amounts(3))
|
---|
104 | bail = false;
|
---|
105 | for b = h:-1:t
|
---|
106 | for a = 1:c
|
---|
107 | if ~all(col(A(b,:,a,:)) == bcol(a))
|
---|
108 | bail = true;
|
---|
109 | break;
|
---|
110 | end
|
---|
111 | end
|
---|
112 | if bail
|
---|
113 | break;
|
---|
114 | end
|
---|
115 | end
|
---|
116 | else
|
---|
117 | b = h - abs(crop_amounts(3));
|
---|
118 | end
|
---|
119 |
|
---|
120 | if padding == 0 % no padding
|
---|
121 | % Issue #175: there used to be a 1px minimal padding in case of crop, now removed
|
---|
122 | %{
|
---|
123 | if ~isequal([t b l r], [1 h 1 w]) % Check if we're actually croppping
|
---|
124 | padding = 1; % Leave one boundary pixel to avoid bleeding on resize
|
---|
125 | bcol(:) = nan; % make the 1px padding transparent
|
---|
126 | end
|
---|
127 | %}
|
---|
128 | elseif abs(padding) < 1 % pad value is a relative fraction of image size
|
---|
129 | padding = sign(padding)*round(mean([b-t r-l])*abs(padding)); % ADJUST PADDING
|
---|
130 | else % pad value is in units of 1/72" points
|
---|
131 | padding = round(padding); % fix cases of non-integer pad value
|
---|
132 | end
|
---|
133 |
|
---|
134 | if padding > 0 % extra padding
|
---|
135 | % Create an empty image, containing the background color, that has the
|
---|
136 | % cropped image size plus the padded border
|
---|
137 | B = repmat(bcol,[(b-t)+1+padding*2,(r-l)+1+padding*2,1,n]); % Fix per Luiz Carvalho
|
---|
138 | % vA - coordinates in A that contain the cropped image
|
---|
139 | vA = [t b l r];
|
---|
140 | % vB - coordinates in B where the cropped version of A will be placed
|
---|
141 | vB = [padding+1, (b-t)+1+padding, padding+1, (r-l)+1+padding];
|
---|
142 | % Place the original image in the empty image
|
---|
143 | B(vB(1):vB(2), vB(3):vB(4), :, :) = A(vA(1):vA(2), vA(3):vA(4), :, :);
|
---|
144 | A = B;
|
---|
145 | else % extra cropping
|
---|
146 | vA = [t-padding b+padding l-padding r+padding];
|
---|
147 | A = A(vA(1):vA(2), vA(3):vA(4), :, :);
|
---|
148 | vB = [NaN NaN NaN NaN];
|
---|
149 | end
|
---|
150 |
|
---|
151 | % For EPS cropping, determine the relative BoundingBox - bb_rel
|
---|
152 | bb_rel = [l-1 h-b-1 r+1 h-t+1]./[w h w h];
|
---|
153 | end
|
---|
154 |
|
---|
155 | function A = col(A)
|
---|
156 | A = A(:);
|
---|
157 | end
|
---|