How to find line using Hough Transform

Hi I have a matrix of coordinates such
BW = [380 .2134;
      380.95 .1784;
      381.05 .1374]
and I would like to identify lines using Hough transform. Unfortunately, I do not know how. Examples do not work for my data.

 Accepted Answer

Either convert your list of coordinates to a binary image
for k = 1 : size(BW, 1);
binaryImage(BW(k, 2), BW(k, 1)) = true;
end
or else use RANSAC: https://en.wikipedia.org/wiki/Ransac to identify lines in a collection of arbitrary points.

11 Comments

Thanks a lot ! I will check this other method But how to do binaryImage when I have data in BW like 0.2184 I need scalar to do this.
You need to convert those numbers to an integer that is the row and column. So pick a resolution, say the resolution of your original image or make up something like 1024x1280 or whatever. Then get the row and column like
row = int32(y * 1280;
col = int32(x * 1024);
i want to apply line hough transform to the image in the previous comment to give me two straight lines as shown in the next comment and then superimposing the lines on the original gray scale image , could you help me ,please? Image Analyst
Just call polyfit
% Find the top and bottom lines.
x = 1 : columns;
topCurve = zeros(1, columns);
bottomCurve = zeros(1, columns);
for col = 1 : columns
topCurve(col) = find(binaryImage(:, col), 1, 'first');
bottomCurve(col) = find(binaryImage(:, col), 1, 'last');
end
% Fit top curve to line
topCoefficients = polyfit(x, topCurve, 1);
yFitTop = polyval(topCoefficients, x);
hold on
plot(x, yFitTop, 'r-', 'LineWidth', 3);
% Fit bottom curve to line
bottomCoefficients = polyfit(x, bottomCurve, 1);
yFitBottom = polyval(bottomCoefficients, x);
hold on
plot(x, yFitBottom, 'r-', 'LineWidth', 3);
See attached m-file for a full demo.
could you please help me to connect dashed lines in the top and bottom to the end of the image ? @Image Analyst
Since you have the endpoints, you can just use polyfit() to get the equation of the line connecting the two endpoints, then use polyval() to get the y values between the two end points. See my code for examples of how to use polyfit() and polyval().
could you please tell me how to save the figure that hough transform apply on it to be saved image ? Image Analyst
Did you try what I said, about using polyfit() and polyval()? If not, why not? Did you put your two endpoints into polyfit
coefficients = polyfit([x1, x2], [y1, y2], 1);
and then get the line y values between x1 and x2?
xLine = x1:x2;
yLine = polyval(coefficients, xLine);
So now you have the x and y locations between endpoint (x1, y1) and (x2, y2). Just 3 lines of code so I'm sure you tried it, so what happened?
Or, on re-reading your question I guess you did use them and your actual question is "how to save the figure", so for that I'd use imwrite() to save the image alone, and export_fig() if you want to save the whole window (with axes labels, tick marks, etc.)
Thanks alot for your help, i did it but by this it is manual not automatic and i want it to be automatic . last thing , the code of polyfit you mentioned to give two red lines on the binary image , could you tell me how to apply these two lines on the original gray scale image after applying them on it's binary image ? @Image Analyst .sorry for annoying you
You can burn the lines into the image like this:
for k = 1 : length(xLine)
col = round(xLine(k))
row = round(yLine(k))
grayImage(row, col) = 255;
end

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!