Hi sorry im new to image processing. Need some explanation , which 2 video attachment should I use for this MATLAB code and which reference image i should use? thank you

1 view (last 30 days)
clc;
close all;
inputvideo=vision.VideoFileReader('traffic.avi');%<<<<<<<<<<<<
vid1=vision.VideoPlayer;
while~isDone(inputvideo)
frame1=step(inputvideo);
step(vid1,frame1);
pause(0.005);
end
imwrite(frame1,'D:\referenceimage.jpg','jpg');%<<<<<<<<<<<<<<
release(inputvideo);
release(vid1);
referenceimage=imread('D:\referenceimage.jpg');<<<<<<<<<<<<
vid2=vision.VideoFileReader('Traffic.avi');%<<<<<<<<<<<<<
X=zeros(2,121);
Y=zeros(2,121);
Z=zeros;
for i=2:121
clc
frame=step(vid2);
frame2=((im2double(frame))-(im2double(referenceimage)));
frame1=im2bw(frame2,0.2);
[labelimage]=bwlabel(frame1);
stats=regionprops(labelimage,'basic');
BB=stats.BoundingBox;
X(i)=BB(1);
Y(i)=BB(2);
Dist=((X(i)-X(i-1))^2+(Y(i)-Y(i-1))^2)^(1/2);
Z(i)=Dist;
if(Dist>10&&Dist<20)
display('MEDIUM SPEED');
elseif(Dist<10)
display('SLOW SPEED');
else
display('FAST SPEED');
end
S=strel('disk',6);
frame3=imclose(frame1,S);
step(vid1,frame1);
pause(0.05);
end
M=median(Z);
Speed=(M)*(120/8);
release(vid1)

Answers (1)

Walter Roberson
Walter Roberson on 19 Jan 2023
traffic.avi is supplied with the Image Processing Toolbox.
The code reads and displays the content of the avi. The last frame is written to a file and then the file is read back in. It is not clear why the code bothers to write to a file and read back when it could have just used the content of frame1
  4 Comments
Amirullah Bin Abdul Razak
Amirullah Bin Abdul Razak on 22 Jan 2023
Hi ,
Should I just delete off these two lines of just leave as it is from the program code? ''
imwrite(frame1,'D:\referenceimage.jpg','jpg');%<<<<<<<<<<<<<<
release(inputvideo);
release(vid1);
referenceimage=imread('D:\referenceimage.jpg');<<<<<<<<<<<<
vid2=vision.VideoFileReader('Traffic.avi');%<<<<<<<<<<<<<
X=zeros(2,121);
Y=zeros(2,121);
Z=zeros;
for i=2:121
clc
frame=step(vid2);
frame2=((im2double(frame))-(im2double(referenceimage)));
Walter Roberson
Walter Roberson on 22 Jan 2023
FramesToProcess = 121; %don't ask me why
videoname = 'traffic.avi';
inputvideo = VideoReader(videoname);
vid1 = vision.VideoPlayer;
while hasFrame(inputvideo)
frame1 = readFrame(inputvideo);
step(vid1,frame1);
pause(0.005);
end
referenceimage = frame1;
inputvideo.CurrentTime = 0; %reset to beginning instead of release and reopen
vidlength = inputvideo.NumFrames;
if vidlength < FramesToProcess
clear inputvideo
error('video only has %d frames, need %d', vidlength, FramesToProcess);
end
X = zeros(2,FramesToProcess);
Y = zeros(2,FramesToProcess);
Z=zeros;
for i = 2:FramesToProcess
frame = read(inputvideo, i);
frame2=((im2double(frame))-(im2double(referenceimage)));
frame1=im2bw(frame2,0.2);
[labelimage]=bwlabel(frame1);
stats=regionprops(labelimage,'basic');
BB=stats.BoundingBox;
X(i)=BB(1);
Y(i)=BB(2);
Dist=((X(i)-X(i-1))^2+(Y(i)-Y(i-1))^2)^(1/2);
Z(i)=Dist;
if(Dist>10&&Dist<20)
display('MEDIUM SPEED');
elseif(Dist<10)
display('SLOW SPEED');
else
display('FAST SPEED');
end
S=strel('disk',6);
frame3=imclose(frame1,S);
step(vid1, frame3); %not frame1 !
pause(0.05);
end
M=median(Z);
Speed=(M)*(120/8);
release(vid1)
clear inputvideo

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!