diff --git a/zUtil_Imaging/gtCrop.m b/zUtil_Imaging/gtCrop.m index 31dfdfffcd889ea8e2621e627493915ab930808d..c126f375c69a833ca39b78d3a392ad7a35600999 100644 --- a/zUtil_Imaging/gtCrop.m +++ b/zUtil_Imaging/gtCrop.m @@ -29,15 +29,15 @@ function img = gtCrop(img, gtbbox) % Greg Johnson, September 2006 % Modified to work also with 3D volumes W. Ludwig, March 2012 - [size_img(1), size_img(2), size_img(3)] = size(img); + [size_img(1), size_img(2), size_img(3), size_img(4)] = size(img); switch (length(gtbbox)) case 4 % 2D case - minLims = gtbbox(1:2) - maxLims = minLims + gtbbox(3:4) - 1 + 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(1:2), maxLims) + minLims = max(minLims, 1); + maxLims = min(size_img(1:2), maxLims); img = img(minLims(2):maxLims(2), minLims(1):maxLims(1)); case 6 @@ -47,9 +47,19 @@ function img = gtCrop(img, gtbbox) maxLims = minLims + gtbbox(4:6) - 1; % if bbox is too large, crop to image extent minLims = max(minLims, 1); - maxLims = min(size_img, maxLims); + maxLims = min(size_img(1:3), maxLims); img = img(minLims(1):maxLims(1), minLims(2):maxLims(2), minLims(3):maxLims(3)); + case 8 + % 4D case, uses different conventions from 2D case: x and y are not + % inverted + minLims = gtbbox(1:4); + maxLims = minLims + gtbbox(5:8) - 1; + % if bbox is too large, crop to image extent + minLims = max(minLims, 1); + maxLims = min(size_img(1:4), maxLims); + + img = img(minLims(1):maxLims(1), minLims(2):maxLims(2), minLims(3):maxLims(3), minLims(4):maxLims(4)); otherwise error('gtCrop:wrongInput', ... 'Argument bbox needs either to be a 4 (2D) or 6 (3D) element vector');