to draw rectangle containing motion region

조회 수: 2 (최근 30일)
Karan Ratnaparkhi
Karan Ratnaparkhi 2011년 3월 22일
I have an image having black background and motion region that is detected is in white color. I want a matlab code to draw the rectangle over image containing all the motion regions. I have tried following code but it is giving wrong output. Plz help me with the code.. {
%for loop to get d first pixel of image having value 255
d=1;
for x=1:1:r
for y=1:1:c
if fd1(x,y)==255 %fd1 is image whose pixels we are checking
as1=x;
bs1=y;
d=0;
break;
end
if (d==0)
break;
end
end
end
}
%reverse for loop to get d last pixel of image having value 255
d=1;
for x=r:-1:1
for y=c:-1:1
if fd1(x,y)==255
ae1=x;
be1=y;
d=0;
break;
end
if(d==0)
break;
end
end
end
w=as1-ae1;
h=be1-bs1;
rectangle('Position',[as1,bs1,w,h],'EdgeColor','g','LineWidth',2)
}
  댓글 수: 3
Ashish Uthama
Ashish Uthama 2011년 3월 22일
+1 on the code format. From your comments, I would suggest looking at the FIND function to replace your loops.
What is 'wrong' about the output?
Karan Ratnaparkhi
Karan Ratnaparkhi 2011년 3월 23일
using above the rectangle which i am getting is nt right

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

채택된 답변

David Young
David Young 2011년 3월 23일
I think your problem may be the classic row/column vs x/y confusion.
In an image, the first index (row index) corresponds to the y coordinate in functions like PLOT and RECTANGLE. The second index (column index) corresponds to the x coordinate.
There are various possible ways to change your code, but I'd suggest this one. Change the first two lines of the loops to this:
for y=1:1:r
for x=1:1:c
and this:
for y=r:-1:1
for x=c:-1:1
Don't change anything else! See if that helps. (I'm assuming that you are drawing the rectangle in the same axes as the image - for example by displaying the image then using HOLD ON.)
  댓글 수: 1
Karan Ratnaparkhi
Karan Ratnaparkhi 2011년 3월 23일
hey David my prblm ws dat nly bt finally i gt it. Thnx for ur help.

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Images에 대해 자세히 알아보기

제품

Community Treasure Hunt

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

Start Hunting!

Translated by