|
| 1 | +function res = cropOrientedBox(obj, obox, varargin) |
| 2 | +% Crop the content of an image within an oriented box. |
| 3 | +% |
| 4 | +% RES = cropOrientedBox(IMG, OBOX) |
| 5 | +% Crops the content of the image IMG that is contained within the |
| 6 | +% oriented box OBOX. The size of the resulting image is approximately |
| 7 | +% (due to rounding) the size of the oriented box. |
| 8 | +% |
| 9 | +% Example |
| 10 | +% % open and display input image |
| 11 | +% img = Image.read('circles.png') ; |
| 12 | +% figure; show(img); hold on; |
| 13 | +% % identifies oriented box around the main region |
| 14 | +% obox = regionOrientedBox(img > 0); |
| 15 | +% drawOrientedBox(obox, 'g'); |
| 16 | +% % crop the content of the oriented box |
| 17 | +% res = cropOrientedBox(img, obox); |
| 18 | +% figure; show(res) |
| 19 | +% |
| 20 | +% See also |
| 21 | +% regionOrientedBox, crop |
| 22 | +% |
| 23 | + |
| 24 | +% ------ |
| 25 | +% Author: David Legland |
| 26 | +% e-mail: david.legland@inrae.fr |
| 27 | +% INRAE - BIA Research Unit - BIBS Platform (Nantes) |
| 28 | +% Created: 2022-06-24, using Matlab 9.12.0.1884302 (R2022a) |
| 29 | +% Copyright 2022 INRAE. |
| 30 | + |
| 31 | +% retrieve oriented box parameters |
| 32 | +boxCenter = obox(1:2); |
| 33 | +boxSize = obox(3:4); |
| 34 | +boxAngle = obox(5); |
| 35 | + |
| 36 | +% create the transform matrix that maps from box coords to global coords |
| 37 | +transfo = createTranslation(boxCenter) * createRotation(deg2rad(boxAngle)); |
| 38 | + |
| 39 | +% sample points within the box (use single pixel spacing) |
| 40 | +lx = -floor(boxSize(1)/2):ceil(boxSize(1)/2); |
| 41 | +ly = -floor(boxSize(2)/2):ceil(boxSize(2)/2); |
| 42 | + |
| 43 | +% map into global coordinate space |
| 44 | +[y, x] = meshgrid(ly, lx); |
| 45 | +[x, y] = transformPoint(x, y, transfo); |
| 46 | + |
| 47 | +% evaluate within image, keeping type of original image |
| 48 | +resData = zeros(size(x), class(obj.Data)); |
| 49 | +resData(:) = interp(obj, x, y); |
| 50 | + |
| 51 | +% create new image |
| 52 | +res = Image('data', resData, 'parent', obj); |
0 commit comments