全套系统MATLAB智能驾驶深度学习之hough变换实现案例应用
函数比较基础,应用起来难度不高,主要倾向于图像的识别和简单的处理。
主函数如下
main.m
clc; clear all; close all;
warning off all;
I = imread('images\\1.jpg');
I1 = Image_Normalize(I, 1);
hsize = [3 3];
sigma = 0.5;
I2 = Image_Smooth(I1, hsize, sigma, 1);
I3 = Gray_Convert(I2, 1);
bw2 = Image_Binary(I3, 1);
figure; subplot(1, 2, 1); imshow(I, []); title('原图像');
subplot(1, 2, 2); imshow(bw2, []); title('二值化图像');
[~, ~, xy_long] = Hough_Process(bw2, I1, 1);
angle = Compute_Angle(xy_long);
[I4, bw3] = Image_Rotate(I1, bw2, angle*1.8, 1);
[bw4, Loc1] = Morph_Process(bw3, 1);
[Len, XYn, xy_long] = Hough_Process(bw4, I4, 1);
[bw5, bw6] = Region_Segmation(XYn, bw4, I4, 1);
[stats1, stats2, Line] = Location_Label(bw5, bw6, I4, XYn, Loc1, 1);
[Dom, Aom, Answer, Bn] = Analysis(stats1, stats2, Line, I4);
Hough函数变换
即 Hough_Process.m
function [Len, XYn, xy_long] = Hough_Process(bw, Img, flag)
if nargin < 3
flag = 1;
end
[H, T, R] = hough(bw);
P = houghpeaks(H, 4, 'threshold', ceil(0.3*max(H(:))));
lines = houghlines(bw, T, R, P, 'FillGap', 50, 'MinLength', 7);
max_len = 0;
for k = 1 : length(lines)
xy = [lines(k).point1; lines(k).point2];
len = norm(lines(k).point1-lines(k).point2);
Len(k) = len;
if len > max_len
max_len = len;
xy_long = xy;
end
XY{k} = xy; % 存储信息
end
[Len, ind] = sort(Len(:), 'descend'); % 按长度排序
% 直线信息排序
for i = 1 : length(ind)
XYn{i} = XY{ind(i)};
end
xy_long = XYn{1};
x = xy_long(:, 1);
y = xy_long(:, 2);
if abs(diff(x)) < abs(diff(y))
x = [mean(x); mean(x)];
else
y = [0.7*y(1)+0.3*y(2); 0.3*y(1)+0.7*y(2)];
end
xy_long = [x y];
if flag
figure('units', 'normalized', 'position', [0 0 1 1]);
subplot(2, 2, 1); imshow(bw); title('二值图像', 'FontWeight', 'Bold');
subplot(2, 2, 2); imshow(H, [], 'XData', T, 'YData', R, 'InitialMagnification', 'fit');
xlabel('\theta'); ylabel('\rho');
axis on; axis normal; title('霍夫变换域', 'FontWeight', 'Bold')
subplot(2, 2, 3); imshow(Img); title('原图像', 'FontWeight', 'Bold');
subplot(2, 2, 4); imshow(Img); title('区域标识图像', 'FontWeight', 'Bold');
hold on;
plot(xy_long(:,1), xy_long(:,2), 'LineWidth', 2, 'Color', 'r');
end
区域分割子函数
Region_Segmation.m
function [bw1, bw2] = Region_Segmation(XY, bw, Img, flag)
if nargin < 4
flag = 1;
end
for i = 1 : 2
xy = XY{i};
XY{i} = [1 xy(1, 2); size(bw, 2) xy(2, 2)];
ri(i) = round(mean([xy(1,2) xy(2,2)]));
end
minr = min(ri);
maxr = max(ri);
bw1 = bw; bw2 = bw;
bw1(1:minr+5, :) = 0;
bw1(maxr-5:end, :) = 0;
bw2(minr-5:end, :) = 0;
bw2(1:round(minr*0.5), :) = 0;
if flag
figure('units', 'normalized', 'position', [0 0 1 1]);
subplot(2, 2, 1); imshow(Img, []); title('原图像', 'FontWeight', 'Bold');
subplot(2, 2, 2); imshow(bw, []); title('原二值图像', 'FontWeight', 'Bold');
hold on;
for i = 1 : 2
xy = XY{i};
plot(xy(:, 1), xy(:, 2), 'r-', 'LineWidth', 2);
end
hold off;
subplot(2, 2, 3); imshow(bw1, []); title('下区域图像', 'FontWeight', 'Bold');
subplot(2, 2, 4); imshow(bw2, []); title('上区域图像', 'FontWeight', 'Bold');
end
其他具体的函数列表文件
全套文件源码–>传送门