OpenCV using the Hough probability transform to get a straight line, the coordinate of Pixel point on the line is obtained by DDA algorithm.

Source: Internet
Author: User
Tags abs

How do I get the pixel coordinates on a straight line after the picture Hough transform is fitted?

This is the problem that I encountered in the image processing study today, the Hough transform adopts the probabilistic Hough transform, so the straight line information is actually the coordinates of the two endpoints of the straight line, so a more direct idea is to use the DDA algorithm to obtain.

I. Introduction to Algorithms

DDA algorithm is the simplest algorithm for drawing straight line in computer graphics. The main idea is derived from the linear formula Y = kx + B.
We know that the line segment is two endpoints P0 (x0,y0) and P1 (x1,y1), and we can find K and B.

In the case of k,b, we can calculate a Y value as long as we know an X value. If the step of X is 1 (x is added at 1, i.e. x = x + 1), then the step of Y is k+b, as well as knowing that a Y value can also calculate the x value, when the stepping of Y is 1,x (1-b)/k. Based on the computed x and Y values, rounding down, get coordinates (x ', Y '), and draw a point at (X ', y ') at the line segment.

To further simplify the calculation, B is usually taken 0 and the starting point is considered (0,0). With the current point (xi, Yi), the computational formula of the DDA algorithm (XI+1,YI+1) can be summed up as follows:

Xi+1 = xi + xStep (1)
yi+1 = Yi + ystep (2)
We generally determine xstep and ystep by calculating Δx and Δy:

If Δx >δy, the maximum difference of the x-axis is greater than the maximum difference of the y-axis, the x-axis direction is the main direction of the step, XStep = 1,ystep = k;
If Δy >δx, the maximum difference of the y-axis is greater than the maximum difference of the x-axis, the y-axis direction is the main direction of the step, Ystep = 1,xstep = 1/k.
Based on this formula, it is possible to calculate (Xi+1, yi+1) by Iteration (Xi,yi), and then draw the computed (x, y) coordinate points in the coordinate system.

Two. Code display

Here is the code, assuming (x1, y1), (x2, y2) is the two endpoint coordinates of the line:

1Xdis = x2-x1#Increment of x2Ydis = Y2-y1#Increment of y3 if(ABS (Xdis) >ABS (Ydis)):4Maxstep =ABS (Xdis)5 Else:6Maxstep =ABS (Ydis)7Xunitstep = Xdis/maxstep#x increments per step8Yunitstep = Ydis/maxstep#increment per step of y9x =X1Teny =Y1 One  forKinchRange (maxstep): Ax = x +Xunitstep -y = y +Yunitstep -     Print("x:%d, y:%d"% (x, y))

OpenCV using the Hough probability transform to get a straight line, the coordinate of Pixel point on the line is obtained by DDA algorithm.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.