Skip to content

Commit 6e62bd1

Browse files
committed
add cropOrientedBox.m
1 parent 01802d1 commit 6e62bd1

File tree

2 files changed

+55
-3
lines changed

2 files changed

+55
-3
lines changed

src/@Image/cropOrientedBox.m

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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);

src/@Image/interp.m

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515

1616
% ------
1717
% Author: David Legland
18-
% e-mail: david.legland@inra.fr
18+
% e-mail: david.legland@inrae.fr
1919
% Created: 2011-12-14, using Matlab 7.9.0.529 (R2009b)
2020
% Copyright 2011 INRA - Cepia Software Platform.
2121

@@ -54,7 +54,7 @@
5454
y = yData(obj);
5555

5656
if nc == 1
57-
val = interp2(y, x, double(obj.Data), ...
57+
val(:) = interp2(y, x, double(obj.Data), ...
5858
point(:, 2), point(:, 1), method, fillValue);
5959
else
6060
for i = 1:nc
@@ -69,7 +69,7 @@
6969
y = yData(obj);
7070
z = zData(obj);
7171
if nc == 1
72-
val = interp3(y, x, z, double(obj.Data), ...
72+
val(:) = interp3(y, x, z, double(obj.Data), ...
7373
point(:, 2), point(:, 1), point(:, 3), method, fillValue);
7474
else
7575
for i = 1:nc

0 commit comments

Comments
 (0)