Houghlines in MATLAB

After detecting lines in an image using Hough lines , how can I use it to calculate the change in the angle (rotation) of the reference image line?

+2
source share
2 answers

Note to readers: This is the next question, refer to them for background:


The process is similar to what I showed earlier. Below I use the images from your previous question (since you provided only one, I created the other by first turning it 10 degrees).

Let's start by defining lines for two images. We do this using the Hough transform function . This is what looks like both images:

Images with detected lines and points order

Next, we want to perform image registration using the endpoints of the line as control points. Firstly, we guarantee that the dots match each other in two images. I do this by calculating the convex hull using convhull , which automatically sorts them clockwise (or is it in the opposite direction!). The numbers shown above indicate the order.

Finally, we use the cp2tform function to get the transformation matrix that we use to align the image and extract translation, rotation and scaling.

Below is the full code:

 %% # Step 1: read and prepare images %# (since you provided only one, I created the other by rotating the first). I1 = imread('http://i.stack.imgur.com/Se6zX.jpg'); I1 = rgb2gray( imcrop(I1, [85 35 445 345]) ); %# Get rid of white border I2 = imrotate(I1, -10, 'bilinear', 'crop'); %# Create 2nd by rotating 10 degrees %% # Step 2: detect the cross sign endpoints (sorted in same order) p1 = getCross(I1); p2 = getCross(I2); %% # Step 3: perform Image Registration %# Find transformation that maps I2 to I1 using the 4 control points for each t = cp2tform(p2,p1,'affine'); %# Transform I2 to be aligned with I1 II2 = imtransform(I2, t, 'XData',[1 size(I1,2)], 'YData',[1 size(I1,1)]); %# Plot figure('menu','none') subplot(131), imshow(I1), title('I1') subplot(132), imshow(I2), title('I2') subplot(133), imshow(II2), title('I2 (aligned)') %# Recover affine transformation params (translation, rotation, scale) ss = t.tdata.Tinv(2,1); sc = t.tdata.Tinv(1,1); tx = t.tdata.Tinv(3,1); ty = t.tdata.Tinv(3,2); scale = sqrt(ss*ss + sc*sc) rotation = atan2(ss,sc)*180/pi translation = [tx ty] 

And here is a function that extracts the endpoints of lines:

 function points = getCross(I) %# Get edges (simply by thresholding) I = imfilter(I, fspecial('gaussian', [7 7], 1), 'symmetric'); BW = imclearborder(~im2bw(I, 0.5)); %# Hough transform [H,T,R] = hough(BW); %# Detect peaks P = houghpeaks(H, 2); %# Detect lines lines = houghlines(BW, T, R, P); %# Sort 2D points in counterclockwise order points = [vertcat(lines.point1); vertcat(lines.point2)]; idx = convhull(points(:,1), points(:,2)); points = points(idx(1:end-1),:); end 

with the result:

Resulting aligned image

 scale = 1.0025 rotation = -9.7041 translation = 32.5270 -38.5021 

The rotation is restored by almost 10 degrees (with some unavoidable error), and scaling is effective 1 (which means no scaling). Note that in the example above there was a translation component because the rotation was not performed around the center of the cross sign).

+3
source

I'm not sure that there is a MATLAB implementation of the Hough transform, but the line orientation will simply be in the right corner (90 degrees or pi / 2 radians) to the angle that you used to identify the line in the first place.

Hope this helps. There, decent coverage of Hough's transformations on the Internet and Wikipedia is a good place to start.

0
source

All Articles