Can I reduce Hough transform line results/combine results with similar theta

18 views (last 30 days)
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.

Accepted Answer

Aghamarsh Varanasi
Aghamarsh Varanasi on 25 Jan 2021
Hi,
  1. The ‘MinLength’ Argument to the ‘houghlines function determines the number of lines that will be given as output and stored in ‘lines’ Variable. This may vary from image to image.
  2. You could do a basic comparison on the ‘x’ and ‘y’ values of the end points of the lines to select which two lines to plot in the final output.
Here is the output that I got for this code.
clear all;
clc
a= imread('Figure.jpeg'); % 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',75);
%% Final plot
figure, imshow(bwb), hold on
% here lines(1) and lines(2) are the desired lines
max_len = 0;
midpt(1,:) = (lines(1).point1 + lines(2).point1)/2;
midpt(2,:) = (lines(1).point2 + lines(2).point2)/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,'color','blue');
plot (midpt(2,1),midpt(2,2),'o','linewidth',2,'color','blue');
plot (midpt(:,1),midpt(:,2),'linewidth',2,'color','blue');
hold on
end
You could eliminate the third line by choosing the two lines that mark the road from the 'lines' variable. In this case they are the first two elements of the 'lines' variable
  3 Comments
Aghamarsh Varanasi
Aghamarsh Varanasi on 27 Jan 2021
I think you are looking for something like 'polyfit' function
You can try
pos = get(gcf, 'Position');
x = linspace(0,pos(3));
p = polyfit([midpt(1,1),midpt(2,1)], [midpt(1,2), midpt(2,2)],1);
plot(x,polyval(p,x),'b');
Where midpt(1,:) and midpt(2,:) are the end points of the 'blue line' in the figure.
Muamin Sulaiman
Muamin Sulaiman on 27 Jan 2021
Yes polyfit actually worked, I tried to use extend factors to no avail, I didn't know that polyfit existed hahaha.
Thank you very much for the help :)

Sign in to comment.

More Answers (0)

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!