How do I measure the angle of a line in my image using the Image Processing Toolbox in MATLAB?

18 views (last 30 days)
I have an image with a few line objects. I would like to know if the Image Processing Toolbox has the capability to recognize these line objects from the figure and give me an estimate of their orientation.

Accepted Answer

MathWorks Support Team
MathWorks Support Team on 7 Jun 2016
To recognize line objects in your figure window, you may use the HOUGH Transform. Information about the HOUGH transform may be found at the following location:
 
<http://www.mathworks.com/help/toolbox/images/ref/hough.html>
As a simple example, run the following code on the attached file "test.jpg":
 
% read the image into MATLAB and convert it to grayscale
I = imread('test.jpg');
Igray = rgb2gray(I);
figure, imshow(I);
% We can see that the image is noisy. We will clean it up with a few
% morphological operations
Ibw = im2bw(Igray,graythresh(Igray));
se = strel('line',3,90);
cleanI = imdilate(~Ibw,se);
figure, imshow(cleanI);
% Perform a Hough Transform on the image
% The Hough Transform identifies lines in an image
[H,theta,rho] = hough(cleanI);
peaks = houghpeaks(H,10);
lines = houghlines(Ibw,theta,rho,peaks);
figure, imshow(cleanI)
% Highlight (by changing color) the lines found by MATLAB
hold on
for k = 1:numel(lines)
x1 = lines(k).point1(1);
y1 = lines(k).point1(2);
x2 = lines(k).point2(1);
y2 = lines(k).point2(2);
plot([x1 x2],[y1 y2],'Color','g','LineWidth', 2)
end
hold off
% Identify the angles of the lines.
% The following command shows the angle of the 2nd line found by the Hough
% Transform
lines(1).theta
lines(2).theta

More Answers (0)

Tags

No tags entered yet.

Community Treasure Hunt

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

Start Hunting!