Reduce processing time on Matlab motion detection script.

3 ビュー (過去 30 日間)
Gee
Gee 2016 年 6 月 5 日
回答済み: Image Analyst 2016 年 6 月 6 日
We currently have a matlab script that detects motion in some long videos of ours (hours) and outputs those detections as a list with the start, end and total time of the periods of motion detected. The only problem is that the script we have is very slow.
I tried reducing the number of frames that were read, because the motions we're looking for are quite significant (they can easily be seen with the human eye), however it seems I can only skip 2-3 frames before no motion is detected. Is there anyway I could make this script process faster? I don't see why only looking at every 25th frame in the video shouldn't be adequate for motion detection when the movements occur over minutes and can easily be seen with the human eye?
The code we're currently running is:
for k = 2 : 4 : nframes
waitbar(k/nframes,h);
img_o=read(obj, k);
img_o=imresize(img_o,[new_r new_c]);
img_oo=img_o;
a=size(img_o);
if(length(a)>2)
img_o=rgb2gray(img_o);
end
col_1=img_o(:);
col_m=mean(col_1(:));
if(col_m<5)
motion=2;
mov_mat=[];
if(fig_flag==1)
close(fig)
end
close(h1);
return
end
d=abs(double(img_l) - double(img_o));
d=d>th;
d=bwareaopen(d,10);
d=imfill(d,'holes');
se=strel('disk',3);
d=imdilate(d,se);
s=round((1-(sensitivity/100))*200);
d=bwareaopen(d,s);
[r,c]=find(d==1);
rec_flag=0;
if ~(isempty(r))
[lab,num]=bwlabel(d);
for i=1:num
[r,c]=find(lab==i);
pt_1(i,:)=[min(c) min(r)];
len(i,1)=max(r)-min(r);
br(i,1)=max(c)-min(c);
rec_flag=1;
end
mov_frames=mov_frames + 1;
non_mov_flag=0;
if(mov_frames==1)
last_mov_frame=k/4;
end
m_flag=1;
else
if(m_flag==1)
non_mov_flag=non_mov_flag + 1;
if(non_mov_flag>200 || k==nframes)
m_flag=0;
final_mov_frame=(k-(non_mov_flag))/4;
mov_frames=0;
mov_time=final_mov_frame - last_mov_frame;
video_time_1=round(last_mov_frame/freq);
video_time_2=round(final_mov_frame/freq);
video_time_3=round(mov_time/freq);
total_mov_time=total_mov_time + video_time_3 + 1;
vid_time_ch_1=compute_time_1(video_time_1);
vid_time_ch_2=compute_time_1(video_time_2);
vid_time_ch_3=compute_time_1(video_time_3);
%display(strcat('from:',vid_time_ch_1,'to:',vid_time_ch_2,'duration:',vid_time_ch_3));
mov_mat{qq,1}=last_mov_frame;
mov_mat{qq,2}=final_mov_frame;
mov_mat{qq,3}=vid_time_ch_1;
mov_mat{qq,4}=vid_time_ch_2;
mov_mat{qq,5}=vid_time_ch_3;
qq=qq+1;
end
end
end
if(strcmp(display_status,'on'))
dis_count=dis_count+1;
if(rec_flag==1 && dis_count>50)
dis_count=0;
figure(2)
fig = get(groot,'CurrentFigure');
subplot(1,2,1)
imshow(img_oo);
hold on
for i=1:size(pt_1,1)
rectangle('Position',[pt_1(i,:) br(i,1) len(i,1)],'EdgeColor','g');
end
hold off
title(strcat('frame:',num2str(k/4)));
subplot(1,2,2)
imshow(d);
title('ROI binary')
drawnow
fig_flag=1;
end
end
img_l=img_o;
if(rec_flag==1)
motion=1;
end
clear pt_1 len br
end
if(total_mov_time/qq<=2)
motion=0;
end
total_mov_time=compute_time_1(total_mov_time-1);
mov_mat{1,6}=total_mov_time;
mov_mat{1,7}=nframes;
mov_mat{1,8}=freq;
if(fig_flag==1)
close(fig)
end
close(h1);
toc
close(h);
end
Any help would be great!

回答 (1 件)

Image Analyst
Image Analyst 2016 年 6 月 6 日
When you detect the motion at some frame, then just say it started at some frame earlier. Like if you're always 3 frame lagging from what people see, and you detection motion at frame 435, just say it happened at frame 432.

Community Treasure Hunt

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

Start Hunting!

Translated by