Why does translation with imwarp + affine2d-object enlarges the object?
Mostra commenti meno recenti
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
1 Commento
Frederik Kratzert
il 17 Ago 2015
Risposta accettata
Più risposte (1)
Image Analyst
il 17 Ago 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)))]);
1 Commento
Frederik Kratzert
il 17 Ago 2015
Categorie
Scopri di più su Images in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!