[21341] | 1 | function [A, vA, vB, bb_rel] = crop_borders(A, bcol, padding, crop_amounts)
|
---|
[18904] | 2 | %CROP_BORDERS Crop the borders of an image or stack of images
|
---|
| 3 | %
|
---|
[21341] | 4 | % [B, vA, vB, bb_rel] = crop_borders(A, bcol, [padding])
|
---|
[18904] | 5 | %
|
---|
| 6 | %IN:
|
---|
| 7 | % A - HxWxCxN stack of images.
|
---|
| 8 | % bcol - Cx1 background colour vector.
|
---|
[21341] | 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.
|
---|
[18904] | 14 | %
|
---|
| 15 | %OUT:
|
---|
| 16 | % B - JxKxCxN cropped stack of images.
|
---|
[21341] | 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)
|
---|
[18904] | 20 |
|
---|
[21341] | 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;
|
---|
[18904] | 31 | end
|
---|
[21341] | 32 | if nargin < 4
|
---|
| 33 | crop_amounts = nan(1,4); % =auto-cropping
|
---|
[18904] | 34 | end
|
---|
[21341] | 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
|
---|
[18904] | 58 | end
|
---|
[21341] | 59 | else
|
---|
| 60 | l = 1 + abs(crop_amounts(4));
|
---|
[18904] | 61 | end
|
---|
[21341] | 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));
|
---|
[18904] | 80 | end
|
---|
[21341] | 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
|
---|
[18904] | 96 | end
|
---|
[21341] | 97 | else
|
---|
| 98 | t = 1 + abs(crop_amounts(1));
|
---|
[18904] | 99 | end
|
---|
[21341] | 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));
|
---|
[18904] | 118 | end
|
---|
[21341] | 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
|
---|
[18904] | 126 | end
|
---|
[21341] | 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
|
---|
[18904] | 132 | end
|
---|
[21341] | 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];
|
---|
[18904] | 149 | end
|
---|
[21341] | 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];
|
---|
[18904] | 153 | end
|
---|
| 154 |
|
---|
| 155 | function A = col(A)
|
---|
[21341] | 156 | A = A(:);
|
---|
[18904] | 157 | end
|
---|