problem with real time detection

1 回表示 (過去 30 日間)
Ayesha ayub
Ayesha ayub 2014 年 10 月 9 日
回答済み: Image Analyst 2014 年 10 月 9 日
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.

回答 (1 件)

Image Analyst
Image Analyst 2014 年 10 月 9 日
First of all, binarize that pair of for loops like this
binaryImage = frame_difference(nr,nc) <= threshold;
dummy_matrix = frame_difference;
dummy_matrix(binaryImage) = 0;
anyway, I see no other looping in your code. Why do you think it should loop? You have no for or while construction to make it loop. You need to put in code to make it loop if you want it to loop.

Community Treasure Hunt

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

Start Hunting!

Translated by