我已经编写了一个代码来检测两张图片之间的车辆。第一张图片是参考图片。第二张图片是拍摄图像。 问题是,如果我想使用实时视频来检测车辆,我应该在代码中添加什么代码? 我想用现场网络摄像头来检测车辆。
这是我的代码:
display('Initiating matching...');
display('Calibrating procedures...');
display('... ... ...');
k=imread ('DSC00018.jpg');
p=imread ('DSC00019.jpg');
[x,y,z] = size(k);
if(z==1)
;
else
k = rgb2gray(k);
figure
imshow(k)
end
[x,y,z] = size(p);
if(z==1)
;
else
p = rgb2gray(p);
figure
imshow(p)
end
%applying edge detection on first picture
%in the picture.
k1=imresize(k,[300 300]);
edge_det_k = edge(k1,'sobel');
%%applying edge detection on second picture
%in the picture.
p1=imresize(p,[300 300]);
edge_det_p = edge(p1,'sobel');
%definition of different variables to be used in the code below
%output variable if pictures have been matched.
OUTPUT_MESSAGE = ' RED for 90 seconds ';
OUTPUT_MESSAGE2 = ' GREEN for 20 seconds ';
OUTPUT_MESSAGE3 = ' GREEN for 30 seconds ';
OUTPUT_MESSAGE4 = ' GREEN for 60 seconds ';
OUTPUT_MESSAGE5 = ' GREEN for 90 seconds ';
%initialization of different variables used
matched_data = 0;
white_points = 0;
black_points = 0;
x=0;
y=0;
l=0;
m=0;
%for loop used for detecting black and white points in the picture.
for a = 1:1:300
for b = 1:1:300
if(edge_det_k(a,b)==1)
white_points = white_points+1;
else
black_points = black_points+1;
end
end
end
display('matching...');
%for loop comparing the white (edge points) in the two pictures
for i = 1:1:300
for j = 1:1:300
if(edge_det_k(i,j)==1)&(edge_det_p(i,j)==1)
matched_data = matched_data+1;
else
;
end
end
end
%calculating percentage matching.
total_data = white_points;
total_matched_percentage = (matched_data/total_data)*100;
%handles.pp=total_matched_percentage;
%guidata(hObject,handles);
%outputting the result of the system.
if total_matched_percentage > 90 %can add flexability at this point by reducing the amount of matching.
total_matched_percentage
OUTPUT_MESSAGE
elseif total_matched_percentage <= 90 & total_matched_percentage > 70
total_matched_percentage
OUTPUT_MESSAGE2
display(OUTPUT_MESSAGE2);
elseif total_matched_percentage <= 70 & total_matched_percentage > 50
total_matched_percentage
OUTPUT_MESSAGE3
display(OUTPUT_MESSAGE3);
elseif total_matched_percentage <= 50 & total_matched_percentage > 10
total_matched_percentage
OUTPUT_MESSAGE4
display(OUTPUT_MESSAGE4);
else
total_matched_percentage
OUTPUT_MESSAGE5
display(OUTPUT_MESSAGE5);
end