Forward mapping- problem with Rotation

So, i got the input image and I want to apply rotation by 60 degrees. Earlier I created the matrix of output image (all of its pixels are the same colour):
matrix=[0.5 0.86603 0; -0.86603 0.5 0; 0 0 1];
u=1:size(input_image,2);
v=(1:size(input_image,1))';
[U,V]=meshgrid(u,v);
t=1:size(input_image,2)*size(input_image,1);
[x, y] = transformPointsForward(affine2d(matrix),U(t),V(t));
%making indices positive integers
min_x=min(min(x));
min_y=min(min((y));
x=ceil(x-min_x+1);
y=ceil(y-min_y+1);
%copying pixels from input to output image
output_image(y(t),x(t),:)=input_image(V(t),U(t),:);
When it comes to scaling, the result is what it should be. But when I use ratation matrix, the result is like below. Is it the error in the way I'm indexing in my last line of code?

Respuestas (1)

Matt J
Matt J el 19 de En. de 2018
You should probably just do
output_image = imwarp(input_image,affine2d(matrix));

10 comentarios

Kamil Galazka
Kamil Galazka el 19 de En. de 2018
If I'm right, imwarp is applying inverse mapping. I'm using it, but in my programme I also need forward mapping (https://blogs.mathworks.com/steve/2006/04/28/spatial-transforms-forward-mapping/). Below you got the difference between the results.
<<
<<
>>
>> The first one is forward mapping, the second one inverse mapping (imwarp function). I want my code to result in first image- I want to see the "gaps", for the comparison and educational purposes.
Matt J
Matt J el 19 de En. de 2018
Editada: Matt J el 19 de En. de 2018
I see. Then I think you really want
matrix=[0.5 0.86603 0; -0.86603 0.5 0; 0 0 1];
input_image=permute(input_image,[2,1,3]);
[mm,nn,pp]=size(input_image);
u=1:mm; v=1:nn;
[U,V]=ndgrid(u-mean(u),v-mean(v));
[x, y] = transformPointsForward(affine2d(matrix), U(:),V(:));
%making indices positive integers
min_x=min(x);
min_y=min(y);
x=ceil(x-min_x+1);
y=ceil(y-min_y+1);
max_x=max(x);
max_y=max(y);
idx=sub2ind([max_x,max_y], x,y);
output_image=zeros(max_x,max_y,pp);
for i=1:pp
tmp=zeros(max_x,max_y);
tmp(idx)=input_image(:,:,i);
output_image(:,:,i)=tmp;
end
output_image=ipermute(output_image,[2,1,3]);
I got the error:
Error using sub2ind (line 43)
Out of range subscript.
I also noticed there should be min(min(x) and the same with y, cause we need the one, smallest element from the whole matrix.
Matt J
Matt J el 19 de En. de 2018
Editada: Matt J el 19 de En. de 2018
This should fix all that.
[x, y] = transformPointsForward(affine2d(matrix), U(:),V(:));
Kamil Galazka
Kamil Galazka el 19 de En. de 2018
Still the same error :/
Matt J
Matt J el 19 de En. de 2018
Could you attach input_image in a .mat file
Kamil Galazka
Kamil Galazka el 19 de En. de 2018
Could it be jpg?
Matt J
Matt J el 19 de En. de 2018
For me it runs with no problems, and produces the desired result.
Kamil Galazka
Kamil Galazka el 19 de En. de 2018
Maybe that's a problem with my Matlab version (2015b)... Cause this code is working, but I'm getting this from input_image as jpg:
I think I'll give up, thanks for your help, man :)
I think you're getting the right image, but just displaying it incorrectly,
imshow(output_image/max(output_image(:)))

Iniciar sesión para comentar.

Categorías

Más información sobre Image Processing Toolbox en Centro de ayuda y File Exchange.

Preguntada:

el 19 de En. de 2018

Comentada:

el 20 de En. de 2018

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by