i made a motion detection code :
clear all
threshold= 35;
video= mmreader('SampleVideo.avi'); first_frame = video.read(1);
first_frame_gray=rgb2gray(first_frame);
total_frames= video.NumberOfFrames;
[row,column,page]=size(first_frame);
dummy_matrix = zeros(row,column);
for i=2:total_frames
current_frame=video.read(i);
current_frame_gray=rgb2gray(current_frame);
frame_difference=imabsdiff(current_frame_gray,first_frame_gray);
for nr=1:row
for nc=1:column
if ((frame_difference(nr,nc) > threshold))
dummy_matrix(nr,nc) = frame_difference(nr,nc);
else
dummy_matrix(nr,nc) = 0;
end
end
end
first_frame_gray= current_frame_gray;
figure(1)
subplot(2,1,1)
imshow(current_frame)
subplot(2,1,2)
imshow(uint8(dummy_matrix))
end
...
now i convert it to real time here is my code for real time:
clear all;
threshold=35; i=1;
video = videoinput('winvideo',1,'YUY2_1280x1024'); first_frame=getsnapshot(video); first_frame_gray=rgb2gray(first_frame);
[row,column,page]=size(first_frame); dummy_matrix = zeros(row,column);
current_frame=getsnapshot(video);
current_frame_gray = rgb2gray(current_frame);
frame_difference=imabsdiff(current_frame_gray,first_frame_gray);
for nr=1:row
for nc=1:column
if (frame_difference(nr,nc) > threshold)
dummy_matrix(nr,nc) = frame_difference(nr,nc);
else
dummy_matrix(nr,nc) = 0;
end
end
end
first_frame_gray= current_frame_gray;
preview(video);
figure(1)
subplot(2,1,1)
imshow(current_frame)
subplot(2,1,2)
imshow(uint8(dummy_matrix))
Movie(i) = im2frame(uint8(dummy_matrix),gray);
i=i+1;
the problem is my code run once and capture only first frame . if i use while loop then the code rune forever and to stop it i have to close matlab plus its really slow.