Skip to content
Snippets Groups Projects
Commit 930079bf authored by Nicola Vigano's avatar Nicola Vigano
Browse files

gtCrop: vectorized it a little bit, to make it more easy to change and mantain


Signed-off-by: default avatarNicola Vigano <nicola.vigano@esrf.fr>

git-svn-id: https://svn.code.sf.net/p/dct/code/trunk@316 4c865b51-4357-4376-afb4-474e03ccb993
parent 077a33a4
No related branches found
No related tags found
No related merge requests found
function im = gtCrop(im, gtbbox) function img = gtCrop(img, gtbbox)
% GTCROP.M Crops an image given a bounding box % GTCROP.M Crops an image given a bounding box
% Usage: % Usage:
% im=gtCrop(imorig,bbox) % im=gtCrop(imorig,bbox)
...@@ -13,7 +13,7 @@ function im = gtCrop(im, gtbbox) ...@@ -13,7 +13,7 @@ function im = gtCrop(im, gtbbox)
% Example: % Example:
% imorig=reshape(1:25,5,5) % produce a 10x10 matrix % imorig=reshape(1:25,5,5) % produce a 10x10 matrix
% bbox=[3 2 2 3]; % bbox=[3 2 2 3];
% im=gtCrop(imorig,bbox) % im=gtCrop(imorig, bbox)
% %
% imorig = % imorig =
% 1 6 11 16 21 % 1 6 11 16 21
...@@ -26,49 +26,30 @@ function im = gtCrop(im, gtbbox) ...@@ -26,49 +26,30 @@ function im = gtCrop(im, gtbbox)
% 13 18 % 13 18
% 14 19 % 14 19
% Greg Johnson, September 2006 % Greg Johnson, September 2006
% Modified to work also with 3D volumes W. Ludwig, March 2012 % Modified to work also with 3D volumes W. Ludwig, March 2012
switch (length(gtbbox))
case 4 % 2D case
minLims = gtbbox(1:2);
maxLims = minLims + gtbbox(3:4) - 1;
% if bbox is too large, crop to image extent
minLims = max(minLims, 1);
maxLims = min(size(img), maxLims);
img = img(minLims(2):maxLims(2), minLims(1):maxLims(1));
case 6
% 3D case, uses different conventions from 2D case: x and y are not
% inverted
minLims = gtbbox(1:3);
maxLims = minLims + gtbbox(4:6) - 1;
% if bbox is too large, crop to image extent
minLims = max(minLims, 1);
maxLims = min(size(img), maxLims);
if length(gtbbox) == 4 % 2D case img = img(minLims(1):maxLims(1), minLims(2):maxLims(2), minLims(3):maxLims(3));
x1 = gtbbox(1); otherwise
y1 = gtbbox(2); error('gtCrop:wrongInput', ...
x2 = x1 + gtbbox(3) - 1; 'Argument bbox needs either to be a 4 (2D) or 6 (3D) element vector');
y2 = y1 + gtbbox(4) - 1; end
end
% if bbox is too large, crop to image extent
if x1 < 1, x1 = 1; end
if y1 < 1, y1 = 1; end
if x2 > size(im, 2), x2 = size(im, 2); end
if y2 > size(im, 1), y2 = size(im, 1); end
im=im(y1:y2, x1:x2);
elseif length(gtbbox) == 6
x1 = gtbbox(1);
y1 = gtbbox(2);
z1 = gtbbox(3);
x2 = x1 + gtbbox(4) - 1;
y2 = y1 + gtbbox(5) - 1;
z2 = z1 + gtbbox(6) - 1;
% if bbox is too large, crop to image extent
if x1 < 1, x1 = 1; end
if y1 < 1, y1 = 1; end
if z1 < 1 ,z1 = 1; end
if x2 > size(im, 2), x2 = size(im, 2); end
if y2 > size(im, 1), y2 = size(im, 1); end
if z2 > size(im, 3), z2 = size(im, 3); end
im=im(y1:y2, x1:x2, z1:z2);
else
error('gtCrop:wrongInput', 'Argument bbox needs either to be a 4 (2D) or 6 (3D) element vector');
end
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment