1function [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
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.
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)
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% 15/05/22: Fixed EPS bounding box (issue #356)
34 crop_amounts = nan(1,4); % =
auto-cropping
36 crop_amounts(end+1:4) = NaN; % fill missing values with NaN
38 [h, w, c, n] = size(A);
39 if isempty(bcol) %
case of transparent bgcolor
40 bcol = A(ceil(end/2),1,:,1);
43 bcol = bcol(ones(c, 1));
46 % Crop margin from left
47 if ~isfinite(crop_amounts(4))
51 if ~all(
col(A(:,l,a,:)) == bcol(a))
61 l = 1 +
abs(crop_amounts(4));
64 % Crop margin from right
65 if ~isfinite(crop_amounts(2))
66 bcol = A(ceil(end/2),w,:,1);
70 if ~all(
col(A(:,r,a,:)) == bcol(a))
80 r = w -
abs(crop_amounts(2));
83 % Crop margin from top
84 if ~isfinite(crop_amounts(1))
85 bcol = A(1,ceil(end/2),:,1);
89 if ~all(
col(A(t,:,a,:)) == bcol(a))
99 t = 1 +
abs(crop_amounts(1));
102 % Crop margin from bottom
103 bcol = A(h,ceil(end/2),:,1);
104 if ~isfinite(crop_amounts(3))
108 if ~all(
col(A(b,:,a,:)) == bcol(a))
118 b = h -
abs(crop_amounts(3));
121 if padding == 0 % no padding
122 % Issue
#175: there used to be a 1px minimal padding in case of crop, now removed
124 if ~isequal([t b l r], [1 h 1 w]) % Check
if we
're actually croppping
125 padding = 1; % Leave one boundary pixel to avoid bleeding on resize
126 bcol(:) = nan; % make the 1px padding transparent
129 elseif abs(padding) < 1 % pad value is a relative fraction of image size
130 padding = sign(padding)*round(mean([b-t r-l])*abs(padding)); % ADJUST PADDING
131 else % pad value is in units of 1/72" points
132 padding = round(padding); % fix cases of non-integer pad value
135 if padding > 0 % extra padding
136 % Create an empty image, containing the background color, that has the
137 % cropped image size plus the padded border
138 B = repmat(bcol,[(b-t)+1+padding*2,(r-l)+1+padding*2,1,n]); % Fix per Luiz Carvalho
139 % vA - coordinates in A that contain the cropped image
141 % vB - coordinates in B where the cropped version of A will be placed
142 vB = [padding+1, (b-t)+1+padding, padding+1, (r-l)+1+padding];
143 % Place the original image in the empty image
144 B(vB(1):vB(2), vB(3):vB(4), :, :) = A(vA(1):vA(2), vA(3):vA(4), :, :);
146 else % extra cropping
147 vA = [t-padding b+padding l-padding r+padding];
148 A = A(vA(1):vA(2), vA(3):vA(4), :, :);
149 vB = [NaN NaN NaN NaN];
152 % For EPS cropping, determine the relative BoundingBox - bb_rel
153 bb_pixels = [l-1 h-b-1 r+1 h-t+1]; %[LowerLeftXY, UpperRightXY]
154 bb_pixels(bb_pixels<0) = 0;
155 bb_pixels = min(bb_pixels, [w h w h]);
156 bb_rel = bb_pixels ./ [w h w h];
function version(in silent)
Return a scalar MATLAB string containing the latest available ParaMonte MATLAB version newer than the...
function abs(in path, in style)
Return the Get absolute canonical path of a file or folder.
function crop_borders(in A, in bcol, in padding, in crop_amounts)