Two pixels on the image to determine a straight line, can be done to find out what the position of these pixels by linear equation? We would expect a straight line equation calculation, but the position of the image pixel coordinates are integer values, by y = k * x + b y is calculated may be a decimal, even if the y do rounding operation, can not well describe points on a straight line, it may lead to describe a straight line in a "dotted line" form, such as a linear equation y = 0.2 * x + 0.3:
0.2 + 1 * 0.5 = 0.3, coordinates (1, 0.5);
0.2 * 2 + 0.7 = 0.3, coordinates (2,0.7);
0.9 + 0.3 * 3 = 0.2, coordinates (3.0.9).
Describe a straight line as shown in FIG tend, as shown below.
Videos straight line function of the conventional method of drawing a straight line matlab
Had also think it is a very easy to solve the problem, but to no avail trace out a straight line, a bit rough search online information in this regard is very small. Therefore, the authors give a quick and easy method of calculation, the pixel position on these lines to find out.
We know A (x1, y1), B (x2, y2) two points can be determined and the slope k AB linear equation y = k * x + b coordinate values of the intercept B;
Algorithm steps:
1. The abscissa and ordinate respectively calculate the difference between two points:
deltaH = abs(y1 - y2);
deltaW = abs(x1 - x2);
2. Set the loop variable range , if deltaH <deltaW, the range of the argument [x1, x2] (assuming x1 <x2), and vice versa argument range [y1, y2];
3. When deltaH <DeltaW time , select A, B do any starting point, respectively, when calculated from the abscissa variable is j, the ordinate is i, the difference between the intercept of the linear equation and b Tmpb obtained according to , corresponding to a minimum difference value from the position variable i is the ordinate, the code is (Coor code is stored on a straight line pixel positions):
for J = X1: X2 Hb of = Y1 - . 1 ;% point A as the starting point of He = Y1 + . 1 ; H = 0 ; W is = 0 ; Min = 1000 ; for I = Hb of: of He Tmpb = I - K * J; Delta = ABS (B - Tmpb); IF Delta < Min Min = Delta; H= I; W is = J; End End IF H ~ = 0 && W is ~ = 0 the Num = + the Num . 1 ;% on the line number of pixels of Coor (the Num, . 1 ) = H;% on a straight line the pixel coordinates of the position of Coor ( NUM, 2 ) = W is; Y1 = H; End End
When deltaH> deltaW
for I = Y1: Y2 Min = 1000 ; H = 0 ; W is = 0 ; Wb = X1 - . 4 ;% independent variable range can be set according to the linear characteristic We = X1 + . 4 ; for J = Wb: We Tmpb = I - K * J; Delta = ABS (B - Tmpb); IF Delta < Min Min = Delta; H= i; W = j; end end if H ~= 0 && W ~= 0 Num = Num + 1; Coor(Num,1) = H; Coor(Num,2) = W; x1 = W; end end
This article is drawn from a straight line
Reproduced in: https: //www.cnblogs.com/ImageVision/archive/2012/11/17/2775196.html