matlab - 使用 matlab 和图像处理检测视频中的车道。
问题描述
我对 matlab 和图像处理有点陌生,我的学院给我一个任务来执行一个项目,该项目检测视频中移动汽车的车道。我尝试在 Mathworks 和其他网站上使用一些教程,这些教程真的很有帮助,我提出了一个检测图像中车道的代码,我只想知道如何在视频上应用我的代码,因为我看到它可以正常工作一个图像。
这是我的代码:
img = imread ('test_image.jpg');
I = rgb2gray (img);
%making a gaussian kernel
sigma = 1 ; %standard deviation of distribution
kernel = zeros (5,5); %for a 5x5 kernel
W = 0 ;
for i = 1:5
for j = 1:5
sq_dist = (i-3)^2 + (j-3)^2 ;
kernel (i,j) = exp (-1*exp(sq_dist)/(2*sigma));
W = W + kernel (i,j) ;
end
end
kernenl = kernel/W ;
%Now we apply the filter to the image
[m,n] = size (I) ;
output = zeros (m,n);
Im = padarray (I , [2 2]);
for i=1:m
for j=1:n
temp = Im (i:i+4 , j:j+4);
temp = double(temp);
conv = temp.*kernel;
output(i,j) = sum(conv(:));
end
end
output = uint8(output);
%--------------Binary image-------------
level = graythresh(output);
c= im2bw (output,level);
%---------------------------------------
output2 = edge (c , 'canny',level);
figure (1);
%Segment out the region of interest
ROI = maskedImage;
CannyROI = edge (ROI , 'canny',.45);
%----------------------------------
set (gcf, 'Position', get (0,'Screensize'));
%subplot (141), imshow (I), title ('original image');
%subplot (142), imshow (c), title ('Binary image');
%subplot (143), imshow (output2), title ('Canny image');
%subplot (144), imshow (CannyROI), title ('ROI image');
[H ,T ,R] = hough(CannyROI);
imshow (H,[],'XData',T,'YData',R,'initialMagnification','fit');
xlabel('\theta'), ylabel('\rho');
axis on , axis normal, hold on ;
P = houghpeaks(H,5,'threshold',ceil (0.3*max(H(:))));
x = T(P(:,2));
y = R(P(:,1));
plot (x,y,'s','color','white');
%Find lines and plot them
lines = houghlines (CannyROI,T,R,P,'FillGap',5,'MinLength',7);
figure, imshow (img), hold on
max_len = 0 ;
for k = 1:length(lines);
xy = [lines(k).point1; lines(k).point2];
plot (xy(:,1), xy(:,2), 'LineWidth', 5 , 'Color', 'blue');
%plot beginnings and ends of the lines
plot (xy(1,1), xy(1,2),'x', 'LineWidth', 2, 'Color', 'yellow');
plot (xy(2,1), xy(2,2),'x', 'LineWidth', 2, 'Color', 'red');
%determine the endpoints of the longest line segment
len = norm(lines(k).point1 - lines(k).point2);
if (len>max_len)
max_len = len;
xy_long = xy;
end
end
这是图片和视频的链接:
https://github.com/rslim087a/road-video
https://github.com/rslim087a/road-image
提前致谢。
解决方案
基本上视频处理的发生方式是将视频转换为视频帧(图像)。因此,如果需要,您可以将视频转换为视频帧并运行代码,循环遍历包含视频帧的文件夹。更改 imread 函数以从视频帧文件夹中获取图像...
img = imread(path_to_video_frames_folder/*)
推荐阅读
- c++11 - 我不明白为什么我会不断收到此错误:
- java - arrayList 中的第一项始终为空,并且不能在 Java 的 for 循环中被覆盖
- python - 如何将一串数字拆分为 Pandas 数据框中的编号列?
- c++ - 如何使用通过 USB 端口连接的外部设备与终端通信?
- mysql - 错误 1045 (28000):Linux 上用户 'rumba'@'localhost' 的访问被拒绝(使用密码:NO)
- excel - 如何使用 VBA 宏自动提取保存的 csv 文件并将其放入现有工作表位置
- python - 谷歌 API OAuth 2 和 Django
- android - 将布局重力更改为中心将 TextView 隐藏在选项卡后面
- python - HTML子页面更改而不更改url
- reactjs - 来自深度嵌套的孩子时获取空白页