Can I reduce Hough transform line results/combine results with similar theta
Afficher commentaires plus anciens
I am trying to write a code that detects a driving path from pictures of roads for a class. I used the image proccessing suite to gratscale the image, detect the edges and apply a Hough Transfrom, find the peaks of said transfrom and superimpose them over the target image, then finally plot the results.
What I am trying to do is to get the code to detect the lanes in the pictures , and project a path in the middle of said lanes throgh the image in the form of a straight line, my problem is everytime I try to plot the result of the Hough Transfrom, I am met with an abundance of lines rather than just two or three lines over the lane edges in the photos. I have been told after asking in a few places I can try to combine lines with similar theta results or to limit the lines by only projecting lines with a minimum length (trial and error until I only get the two lines I need) but I am unsure of how to go about doing this, the minLength command comes to mind but I cant seem to have much luck with it.
My code so far is as follows:
clear all
clc
a= imread('Figure1a.jpg'); % Image A read
imshow(a) % Show image
%% Processing
grey = rgb2gray (a)
bwa = im2bw (grey, 175/255);
se = strel('diamond',1)
%% Detect Edges
bwb = edge(bwa,'canny');
%% Hough Transform
[H,theta,rho] = hough(bwb);
figure
imshow(imadjust(rescale(H)),[],...
'XData',theta,...
'YData',rho,...
'InitialMagnification','fit');
xlabel('\theta (degrees)')
ylabel('\rho')
axis on
axis normal
hold on
colormap(gca,hot)
%% Find Peaks
P = houghpeaks(H,5,'threshold',ceil(0.3*max(H(:))));
%% Superimpose Peaks on image of transform to identify peaks
x = theta(P(:,2));
y = rho(P(:,1));
plot(x,y,'s','color','black');
%% Finde lines within Image
lines = houghlines(bwb,theta,rho,P,'FillGap',5,'MinLength',100);
%% Final plot
figure, imshow(bwb), hold on
max_len = 0;
midpt(1,:) = (lines(1).point1 + lines(3).point1)/2
midpt(2,:) = (lines(2).point1 + lines(4).point1)/2
for k = 1:length(lines)
xy = [lines(k).point1; lines(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
% Plot beginnings and ends of 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');
%plot (midpt(1,1),midpt(1,2),'o','linewidth',2,'colour','blue');
%plot (midpt(2,1),midpt(2,2),'o','linewidth',2,'colour','blue');
%plot (midpt(:,1),midpt(:,2),'o','linewidth',2,'colour','blue');
end
My results always end up in the form as such:

With the original photos:

What I am trying to achieve by applying the same code to all photos:

I feel like I am on the right track but I am missing the last link.
Could anyone weigh in with any advice moving forward?
Thanks in advanve for any advice/guidance/help.
Réponse acceptée
Plus de réponses (0)
Catégories
En savoir plus sur White dans Centre d'aide et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
