Problem with matrix indexing in for loop

HI guys
In this code, I am getting problem when I am trying to find index of object . In for loop if I comment out img=img+obj(x_f,y_f) then Index x_f and y_f shows from 1 to 512 but when I remove comment then indexes changed. I don't know how it is happening in for loop. Can anyone please help to sort out problem. I will appreciate your help
obj=zeros(512);
obj(230:250,230:250)=5;
%theta = pi/2*180;
figure;
subplot(2,2,1);
imshow(obj);
drawnow;
N=512;
x=[-N/2:N/2-1];
y=[-N/2:N/2-1];
theta=pi/180;
img=zeros(512);
for i=1:180
x_f=round( x .* cos(theta*i) + y.* sin(theta * i)+256);
y_f=round( y .* cos(theta*i)- x .* sin(theta * i)+256);
img=img+obj(x_f,y_f);
% subplot(2,2,2);
% imshow(img);
% drawnow;
end

답변 (3개)

per isakson
per isakson 2014년 6월 29일
편집: per isakson 2014년 6월 29일

1 개 추천

Here are some links on debugging in Matlab
BTW:
>> x_f(1:5)
ans =
-4 -3 -2 -1 0
Try
>> dbstop if error
Image Analyst
Image Analyst 2014년 6월 29일

1 개 추천

x_f and y_f will not change depending on whether the img line is in there or commented out. However the values do go negative and you can't have negative indexes into img. What are you trying to do anyway? Draw sine waves into an image???

댓글 수: 5

Parag
Parag 2014년 6월 29일
thanks for your reply. I am trying to rotate this simulated image using rotation matrix.
Image Analyst
Image Analyst 2014년 6월 29일
Use imrotate() - that's by far the easiest. If you want to do it manually, use the rotation matrix [cos, -sin, sin, cos] like you learned in math class. Don't do it like you tried. Don't.
Parag
Parag 2014년 6월 29일
Thanks ..I did it with imrotate. for making forward projection I am trying this. I will try now with rotation matrix.
Thanks
Parag
Parag 2014년 6월 29일
I did it using same logic in labview but here I am getting problems.Thanks for suggestion
Image Analyst
Image Analyst 2014년 6월 30일
I would be surprised if that produced a rotated image in labview.

댓글을 달려면 로그인하십시오.

Parag
Parag 2014년 6월 29일

0 개 추천

Thank you for your reply.
I could not able to figure out the behavior of matlab in for loop. When I do that similar indexing without using for loop it work for me.

질문:

2014년 6월 29일

댓글:

2014년 6월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by