Finding points on a line [closed] - image

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 11 years ago.
Improve this question
(This question could be better off on math, but im not sure)
http://i.imgur.com/TVINP.png
This is probably very simple but the way I'm thinking of doing it doesn't seem very easy and there must be a simpler method.I've got an image and I want to find some points that fall on a line so in this example image below the starting point of my line is (39,75) and the ending point is (75,142) from there I want to find 5 points (or any number really 5 is just an example) that are all on that line.
Is there some equation I can use that will get me a certain amount of points given any start and end coordinates?

yes.
suppose (x0,y0) and (x1,y1) are the starting and ending points on the line.
t*(x0,y0) + (t-1)*(x1,y1) are also going to be points on that line where t ranges from 0 to 1.
note:
if t = 0, you get (x0,y0)
if t = 1, you get (x1,y1)
if t is any value inside (0,1) you get that "percentage" of the way from (x0,y0) to (x1,y1)
(if t = 0.5, you are halfway between the points)
this is what is often called "tweening" in computer graphics

Yes. Your line segment can be described by this equation:
x = 39 + t * (75 - 39)
y = 75 + t * (142 - 75)
where, t can take on any value between 0 and 1.
So, to get random points on the line, just choose a random value for t (between 0 and 1), and calculate what x, y are.
The idea is, x is traveling from 39 to 75, while y is traveling from 75 to 142, and t represents the fraction of travel that has been completed.

A line can be defined by the function y = mx + b where x and y are coordinates on the cartesian plane, m is the slope of the line defined by (y2 - y1)/(x2 - x1), and b is the point where the line intersects the y-axis.
Given that information and two points on the line, you can fill in the blanks with some basic algebra to determine a function that defines the line. Note that if your coordinate plane for the image places (0, 0) in the top left corner, you may have to flip the sign of the y-coordinate.

Related

Understanding Hough Transform [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 5 years ago.
Improve this question
I am trying to understand MATLAB's code for the Hough Transform.
Some items are clear to me in this picture,
binary_image is the monochrome version of input_image.
hough_lines is a vector containing detected lines in the image. I see that, four lines have been detected.
T contain the thetas in the (ϴ, ρ) space of the image.
R contain the rhos in the (ϴ, ρ) space of the image.
I have the following questions,
Why is the image rotated before applying Hough Transform?
What do the entries in H represent?
Why is H(Hough Matrix) of size 45x180? Where does this size come from?
Why is T of size 1x180? Where does this size come from?
Why is R of size 1x45? Where does this size come from?
What do the entries in P represent? Are they (x, y) or (ϴ, ρ) ?
29 162
29 165
28 170
21 5
29 158
Why is the value 5 passed into houghpeaks()?
What is the logic behind ceil(0.3*max(H(:)))?
Relevant source code
% Read image into workspace.
input_image = imread('Untitled.bmp');
%Rotate the image.
rotated_image = imrotate(input_image,33,'crop');
% convert rgb to grascale
rotated_image = rgb2gray(rotated_image);
%Create a binary image.
binary_image = edge(rotated_image,'canny');
%Create the Hough transform using the binary image.
[H,T,R] = hough(binary_image);
%Find peaks in the Hough transform of the image.
P = houghpeaks(H,5,'threshold',ceil(0.3*max(H(:))));
%Find lines
hough_lines = houghlines(binary_image,T,R,P,'FillGap',5,'MinLength',7);
% Plot the detected lines
figure, imshow(rotated_image), hold on
max_len = 0;
for k = 1:length(hough_lines)
xy = [hough_lines(k).point1; hough_lines(k).point2];
plot(xy(:,1),xy(:,2),'LineWidth',2,'Color','green');
% Plot beginnings and ends of lines
plot(xy(1,1),xy(1,2),'x','LineWidth',2,'Color','yellow');
plot(xy(2,1),xy(2,2),'x','LineWidth',2,'Color','red');
% Determine the endpoints of the longest line segment
len = norm(hough_lines(k).point1 - hough_lines(k).point2);
if ( len > max_len)
max_len = len;
xy_long = xy;
end
end
% Highlight the longest line segment by coloring it cyan.
plot(xy_long(:,1),xy_long(:,2),'LineWidth',2,'Color','cyan');
Those are some good questions. Here are my answers for you:
Why is the image rotated before applying Hough Transform?
This I don't believe is MATLAB's "official example". I just took a quick look at the documentation page for the function. I believe you pulled this from another website that we don't have access to. In any case, in general it is not necessary for you to rotate the images prior to using the Hough Transform. The goal of the Hough Transform is to find lines in the image in any orientation. Rotating them should not affect the results. However, if I were to guess the rotation was performed as a preemptive measure because the lines in the "example image" were most likely oriented at a 33 degree angle clockwise. Performing the reverse rotation would make the lines more or less straight.
What do the entries in H represent?
H is what is known as an accumulator matrix. Before we get into what the purpose of H is and how to interpret the matrix, you need to know how the Hough Transform works. With the Hough transform, we first perform an edge detection on the image. This is done using the Canny edge detector in your case. If you recall the Hough Transform, we can parameterize a line using the following relationship:
rho = x*cos(theta) + y*sin(theta)
x and y are points in the image and most customarily they are edge points. theta would be the angle made from the intersection of a line drawn from the origin meeting with the line drawn through the edge point. rho would be the perpendicular distance from the origin to this line drawn through (x, y) at the angle theta.
Note that the equation can yield infinity many lines located at (x, y) so it's common to bin or discretize the total number of possible angles to a predefined amount. MATLAB by default assumes there are 180 possible angles that range from [-90, 90) with a sampling factor of 1. Therefore [-90, -89, -88, ... , 88, 89]. What you generally do is for each edge point, you search over a predefined number of angles, determine what the corresponding rho is. After, we count how many times you see each rho and theta pair. Here's a quick example pulled from Wikipedia:
Source: Wikipedia: Hough Transform
Here we see three black dots that follow a straight line. Ideally, the Hough Transform should determine that these black dots together form a straight line. To give you a sense of the calculations, take a look at the example at 30 degrees. Consulting earlier, when we extend a line where the angle made from the origin to this line is 30 degrees through each point, we find the perpendicular distance from this line to the origin.
Now what's interesting is if you see the perpendicular distance shown at 60 degrees for each point, the distance is more or less the same at about 80 pixels. Seeing this rho and theta pair for each of the three points is the driving force behind the Hough Transform. Also, what's nice about the above formula is that it will implicitly find the perpendicular distance for you.
The process of the Hough Transform is very simple. Suppose we have an edge detected image I and a set of angles theta:
For each point (x, y) in the image:
For each angle A in the angles theta:
Substitute theta into: rho = x*cos(theta) + y*sin(theta)
Solve for rho to find the perpendicular distance
Remember this rho and theta and count up the number of times you see this by 1
So ideally, if we had edge points that follow a straight line, we should see a rho and theta pair where the count of how many times we see this pair is relatively high. This is the purpose of the accumulator matrix H. The rows denote a unique rho value and the columns denote a unique theta value.
An example of this is shown below:
Source: Google Patents
Therefore using an example from this matrix, located at theta between 25 - 30 with a rho of 4 - 4.5, we have found that there are 8 edge points that would be characterized by a line given this rho, theta range pair.
Note that the range of rho is also infinitely many values so you need to not only restrict the range of rho that you have, but you also have to discretize the rho with a sampling interval. The default in MATLAB is 1. Therefore, if you calculate a rho value it will inevitably have floating point values, so you remove the decimal precision to determine the final rho.
For the above example the rho resolution is 0.5, so that means that for example if you calculated a rho value that falls between 2 to 2.5, it falls in the first column. Also note that the theta values are binned in intervals of 5. You traditionally would compute the Hough Transform with a theta sampling interval of 1, then you merge the bins together. However for the defaults of MATLAB, the bin size is 1. This accumulator matrix tells you how many times an edge point fits a particular rho and theta combination. Therefore, if we see many points that get mapped to a particular rho and theta value, this is a great potential for a line to be detected here and that is defined by rho = x*cos(theta) + y*sin(theta).
Why is H(Hough Matrix) of size 45x180? Where does this size come from?
This is a consequence of the previous point. Take note that the largest distance we would expect from the origin to any point in the image is bounded by the diagonal of the image. This makes sense because going from the top left corner to the bottom right corner, or from the bottom left corner to the top right corner would give you the greatest distance expected in the image. In general, this is defined as D = sqrt(rows^2 + cols^2) where rows and cols are the rows and columns of the image.
For the MATLAB defaults, the range of rho is such that it spans from -round(D) to round(D) in steps of 1. Therefore, your rows and columns are both 16, and so D = sqrt(16^2 + 16^2) = 22.45... and so the range of D will span from -22 to 22 and hence this results in 45 unique rho values. Remember that the default resolution of theta goes from [-90, 90) (with steps of 1) resulting in 180 unique angle values. Going with this, we have 45 rows and 180 columns in the accumulator matrix and hence H is 45 x 180.
Why is T of size 1x180? Where does this size come from?
This is an array that tells you all of the angles that were being used in the Hough Transform. This should be an array going from -90 to 89 in steps of 1.
Why is R of size 1x45? Where does this size come from?
This is an array that tells you all of the rho values that were being used in the Hough Transform. This should be an array that spans from -22 to 22 in steps of 1.
What you should take away from this is that each value in H determines how many times we have seen a particular pair of rho and theta such that for R(i) <= rho < R(i + 1) and T(j) <= theta < T(j + 1), where i spans from 1 to 44 and j spans from 1 to 179, this determines how many times we see edge points for a particular range of rho and theta defined previously.
What do the entries in P represent? Are they (x, y) or (ϴ, ρ)?
P is the output of the houghpeaks function. Basically, this determines what the possible lines are by finding where the peaks in the accumulator matrix happen. This gives you the actual physical locations in P where there is a peak. These locations are:
29 162
29 165
28 170
21 5
29 158
Each row gives you a gateway to the rho and theta parameters required to generate the detected line. Specifically, the first line is characterized by rho = R(29) and theta = T(162). The second line is characterized by rho = R(29) and theta = T(165) etc. To answer your question, the values in P are neither (x, y) or (ρ, ϴ). They represent the physical locations in P where cross-referencing R and T, it would give you the parameters to characterize the line that was detected in the image.
Why is the value 5 passed into houghpeaks()?
The extra 5 in houghpeaks returns the total number of lines you'd like to detect ideally. We can see that P is 5 rows, corresponding to 5 lines. If you can't find 5 lines, then MATLAB will return as many lines possible.
What is the logic behind ceil(0.3*max(H(:)))?
The logic behind this is that if you want to determine peaks in the accumulator matrix, you have to define a minimum threshold that would tell you whether the particular rho and theta combination would be considered a valid line. Making this threshold too low would report a lot of false lines and making this threshold too high misses a lot of lines. What they decided to do here was find the largest bin count in the accumulator matrix, take 30% of that, take the mathematical ceiling and any values in the accumulator matrix that are larger than this amount, those would be candidate lines.
Hope this helps!

Geometry matrix colinearity prove

Let's say I have this matrix with n=4 and m=5
1 2 3 4 5
6 7 8 9 10
11 12 13 14 15
16 17 18 19 20
Let's say I have a diagonal from the (1,2) point to the (4,5) point. And I have a point P(3,4). How can I check in my algorithm that P is on the diagonal?
TL;DR
Instead of an n-by-m matrix, think about it like a x-y grid. You can get the equation of a line on that grid, and once you have that equation, you put the x coordinate of the point you are interested in checking into your equation. If the y value you calculate from the equation matches the y coordinate of the point you are checking, the point lies on the line.
But How Do I Maths?
First some quick terminology. We have 3 points of interest in this case - the two points that define the line (or "diagonal", as the OP calls it), and the one point that we want to check. I'm going to designate the coordinates of the "diagonal" points with the numbers 1 and 2, and the point we want to check with the letter i. Additionally, for the math we need to do later, I need to treat the horizontal and vertical coordinates of the points separately, and I'll use your n-by-m convention to do so. So when I write n1 in an equation below, that is the n coordinate of the first point used to define the diagonal (so the 1 part of the point (1,2) that you give in your example).
What we are looking for is the equation of a line on our grid. This equation will have the form n = (slope) * m + (intercept).
Okay, now that we have the definitions taken care of, we can write the equations. The first step to solving the problem is finding the slope of your line. This will be the change in the vertical coordinate divided by the change in the horizontal component between the two points that define the line (so (n2 - n1) / (m2 - m1)). Using the values from your example, this will be (4 - 1) / (5 - 2) = 3 / 3 = 1. Note that since you are doing a division here, it is possible that your answer will not be a whole number, so make sure you keep that in mind when declaring your variables in whatever programming language you end up using - unintentional rounding in this step can really mess things up later.
Once we have our slope, the next step is calculating our intercept. We can do this by plugging our slope and the m and n coordinates into the equation for the line we are trying to get. So we start with the equation n1 = (slope) * m1 + (intercept). We can rearrange this equation to (intercept) = n1 - (slope) * m1. Plugging in the values from our example, we get (intercept) = 1 - (1 * 2) = -1.
So now we have the general equation of our line, which for our example is n = (1) * m + (-1).
Now that we have the (slope) and (intercept), we can plug in the coordinates of any point we want to check and see if the numbers match up. Our example point has a m coordinate of 4, so we can plug that into our equation.
n = (1) * (4) + (-1) = 3
Since the n coordinate we calculated using our equation matches the n coordinate of our point in our example, we can say that the sample point DOES fall on the line.
Suppose we wanted to also check to see if the point (2,5) was also on the line. When we plug that point's m coordinate into our equation, we get...
n = (1) * (5) + (-1) = 4
Since the n coordinate we calculated with our equation (4) doesn't match the n coordinate of the point we were checking (2), we know this point DOES NOT fall on the line.

how to calculate shortest distance between two moving objects [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this question
the question is simple one object is moving from east-west with a velocity of v1 and another from south-north with velocity v2.
I just need the algorithm(formula) to calculate the shortest distance between them so I can write a program for it.
I do have distance between them and the meting point of their paths they are d1 and d2.
Assuming you are asking for 2-d space, at t=0, let the starting points be (d1,0) and (0,d2) on the coordinate axes. We can assume this because one object is always moving horizontally (E-W direction, along X-axis) and other vertically (S-N direction, along Y-axis). Now, after some time t, their positions will be,(d1-t*v1) and (0,d2-t*v2). (Speed-Distance-Time relation).
Now, distance between them at this time t will be,
D = d^2 = (d1-t*v1)^2 + (d2-t*v2)^2
So, differentiating both sides wrt t,
dD/dt = 2(-v1)(d1-t*v1) + 2(-v2)(d2-t*v2) ....(1)
For D to be minimum, dD/dt = 0 and second differential must be positive. Now, second differential :
d2D/dt2 = 2*v1^2 + 2*v2^2 which is positive for all real v1/v2. So, if `dD/dt = 0`, distance will be minimum.
So, equating (1) = 0, we get
t = (d1v1 + d2v2)/(v1^2 + v2^2)
So, get sqrt(D) at t = --above value-- and that shall be your answer.
PS: ask these type of questions on mathematics stackexchange.

a function that return the intersection of two rectangles [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Write a function, rec_intersection(rect1, rect2) and returns the
intersection of the two.
Rectangles are represented as a pair of coordinate-pairs: the
bottom-left and top-right coordinates (given in [x, y] notation).
Hint: You can calculate the left-most x coordinate of the
intersection by taking the maximum of the left-most x coordinate of
each rectangle. Likewise, you can calculate the top-most y
coordinate of the intersection by taking the minimum of the top most
y coordinate of each rectangle
This is the answer:
def rec_intersection(rect1, rect2)
x_min = [rect1[0][0], rect2[0][0]].max
x_max = [rect1[1][0], rect2[1][0]].min
y_min = [rect1[0][1], rect2[0][1]].max
y_max = [rect1[1][1], rect2[1][1]].min
return nil if ((x_max < x_min) || (y_max < y_min))
return [[x_min, y_min], [x_max, y_max]]
end
Can someone help explain this code? At least a bit of guidance on the few points where to start deconstructing this code so I can get a start. I've never seen a problem like this one and have been struggling to understand it for the past hour and just can't make any headway.
The intersection of two rectangles is the rectangle where the two overlap. Because of that, you want a rectangle with:
The rightmost of the left-hand sides,
The leftmost of the right-hand sides,
The bottommost of the tops, and
The topmost of the bottoms.
In other words, you want the left, right, top, and bottom that are the closest to the center, but calculating it that way is excessive. So, those are your four x_min (et al) lines.
However, if the rectangles don't overlap at all, there isn't an intersection. That's your return nil line. How do we know there's no overlap? The right and left or the top and bottom aren't where we expect them to be.
The last line just assembles the coordinates (from the first four lines) into the upper-left and lower-right points as specified.

There are 6 equilateral triangles within a hexagon, given an x,y coordinate, how can I determine which equilateral triangle the coordinate is in? [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 10 years ago.
Improve this question
I am creating a game in 3D Virtual world. I have created a hexagon using 6 equilateral triangles numbered t1, t2, t3, t4, t5, t6.
Given an x,y coordinate within the hexagon, what formula can I use to determine which triangle the point is in?
You can convert your (x,y) coordinates to polar coordinates.
You have 6 equilateral triangles in your hexagon, so each "triangle frontier" is separated by Pi/3 radians. So when you have your theta angle, you can guess which triangle you are in.
If you know the point is inside the hexagon, and you know how the triangles are oriented, you can calculate the angle of the point with respect to the center:
atan2((y-yc)/(x-xc)).
If x == xc the angle is either +90 or -90 degrees (PI/2 or -PI/2 radians) depending on y.
(If you don't have atan2, you need to check the quadrant manually: see
http://en.wikipedia.org/wiki/Polar_coordinate_system#Converting_between_polar_and_Cartesian_coordinates )
Point-inside-triangle testing (A, B and C are the vertices of the triangle represented as (x,y)):
// Compute vectors
v0 = C - A // Which means v0[x] = C[x] - A[x], v0[y] = C[y] - A[y]
v1 = B - A // etc.
v2 = P - A
// Compute dot products
dot00 = dot(v0, v0) // dot(a,b) is a[x]*b[x]+a[y]*b[y]
dot01 = dot(v0, v1)
dot02 = dot(v0, v2)
dot11 = dot(v1, v1)
dot12 = dot(v1, v2)
// Compute barycentric coordinates (faster using inverse, but clearer this way)
denom = (dot00 * dot11 - dot01 * dot01)
u = (dot11 * dot02 - dot01 * dot12) / Denom
v = (dot00 * dot12 - dot01 * dot02) / Denom
// Check if point is in triangle
return (u >= 0) && (v >= 0) && (u + v < 1)
from http://www.blackpawn.com/texts/pointinpoly/default.html
Remember that you can save 50-66% time by saving the calculations: all your triangles have two vertices in common. Also you can only test five of them, of course :-)
you can do that by checking if the point lies on the same side of the lines forming one triangle. Continue checking for six of your triangles and find whichever fits.
Find equations of the lines forming a triangle and put the co-ordinates of the point under check in each equation (make sure the'x' of each equation has got the same sign). If the resulting values have 2 same and 1 different sign, then the point is inside the triangle.
Practically, in your case, you will have 9 equations of lines. If you take the origin at the center of your hexagon, the equations of the line will be very easy. Just find those 9 values and search for 3 such values satisfying the previously mentioned condition. The parent equations of these three values constitute the triangle.
Intuitively, the point should be inside the triangle whose vertices are closed to the point in average.
The center vertex can be ignore as it is shared by all triangles.
If you have the remanding vertexes sorted by distance to the point (6 in total as they are shared by adjacent triangles), the triangle you are looking for is the one whose most distant vertex is found first in the list traversing in ascending order.

Resources