btw: using circshift(shape,[delta_x,delta_y]) does work properly...anyway i would be interested in a answer/solution, because i find imwarp somehow easier to use if the image has to be enlarged because the translation would the object cause to be outside of the image..
Why does translation with imwarp + affine2d-object enlarges the object?
11 views (last 30 days)
Show older comments
Frederik Kratzert
on 17 Aug 2015
Commented: Frederik Kratzert
on 17 Aug 2015
Hey everybody,
i searched the forum for an answer but couldnt find one, so i hope oyu can help me here.
I have some troubles with image translation which i need to center objects in a binary image with its centroid to the image center. After i thought i am already done i found some bug for which i dont find a solution.
When ever i try to translate an object in a binary image by any given delta_x/y the object after translation has increased its area.
Here is some example i wrote for the questions to point out the problem:
%create binary T shape
shape = zeros(40,40);
shape(10,10:30) = 1;
shape(11:30,20) = 1;
shape = im2bw(shape);
disp(['Area before: ',num2str(sum(sum(shape)))]);
%create Reference2D object
Rout = imref2d(size(shape));
%create affine2d object for translation
delta_x = -5;
delta_y = 8;
tform = affine2d([1 0 0; 0 1 0; delta_x delta_y 1]);
%execute translation
output = imwarp(shape,tform,'outputView',Rout);
disp(['Area after: ',num2str(sum(sum(output)))]);
The Output then is:
Area before: 41
Area after: 84
The syntax of the affine2d-object should be right, regarding to this page: http://de.mathworks.com/help/images/performing-general-2-d-spatial-transformations.html
I tried it with a couple of objects and i am facing always the same problem. Even with:
shape = zeros(40,40);
shape(10:30,10:30) = 1;
shape = im2bw(shape);
i got the following output:
Area before: 441
Area after: 484
So its not a problem of "slim" objects with parts of width = 1;
I would be very happy if anybody could help me out, as it is essential for me to maintain objects size...
Thanks in advance, Frederik
Accepted Answer
David Young
on 17 Aug 2015
The reason for the enlargement is the interpolation rule that imwarp uses - 'linear' by default. Because the image is binary, some pixels close to the boundary of the translated shape are given interpolated values that are rounded to be 1 rather than 0. If you use 'nearest' interpolation, you will not get the increase.
output = imwarp(shape,tform,'nearest','outputView',Rout);
More Answers (1)
Image Analyst
on 17 Aug 2015
That transform enlarges the object. Use imshow() to show it:
%create binary T shape
shape = zeros(40,40);
shape(10,10:30) = 1;
shape(11:30,20) = 1;
shape = im2bw(shape);
subplot(1,2,1);
imshow(shape);
axis on;
disp(['Area before: ',num2str(sum(sum(shape)))]);
%create Reference2D object
Rout = imref2d(size(shape));
%create affine2d object for translation
delta_x = -5;
delta_y = 8;
tform = affine2d([1 0 0; 0 1 0; delta_x delta_y 1]);
%execute translation
output = imwarp(shape,tform,'outputView',Rout);
subplot(1,2,2);
imshow(output);
axis on;
disp(['Area after: ',num2str(sum(sum(output)))]);
See Also
Categories
Find more on 3-D Volumetric Image Processing in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!