问题
I implemented Hough Transform in C# this way:
List<Point> forme = new List<Point>();
forme.Add(new Point(260, 307));
forme.Add(new Point(268, 302));
forme.Add(new Point(273, 299));
forme.Add(new Point(279, 295));
forme.Add(new Point(285, 292));
forme.Add(new Point(291, 288));
forme.Add(new Point(298, 283));
forme.Add(new Point(305, 280));
forme.Add(new Point(312, 277));
forme.Add(new Point(319, 274));
forme.Add(new Point(325, 271));
forme.Add(new Point(333, 268));
forme.Add(new Point(340, 264));
forme.Add(new Point(350, 259));
forme.Add(new Point(356, 256));
int width = Math.Abs(forme[0].X - forme[forme.Count - 1].X);
int height = Math.Abs(forme[0].Y - forme[forme.Count - 1].Y);
int halfWidth = width / 2; int halfHeigh = height / 2;
double pmax = Math.Sqrt((width * width) + (height * height));
double tmax = Math.PI * 2;
// step sizes
double dp = pmax / (double)width;
double dt = tmax / (double)height;
int[,] A = new int[width , height]; // accumulator array
foreach (Point p in forme)
{
for (int Theta = 1; Theta < height; Theta++)
{
double radius = ((double)(p.X) * Math.Cos(dt * (double)Theta)) + ((double)(p.Y) * Math.Sin(dt * (double)Theta)) ;
int k = (int)((radius / pmax) * width);
if (k >= 0 && k < width) A[k, Theta]++;
}
}
int goodTheta = 0;
int goodRadius = 0;
// maxMapIntensity c'est l'intensité maximale
int maxMapIntensity = 0;
for (int radius = 0; radius < width; radius++)
{
for (int theta = 0; theta < height; theta++)
{
if (A[radius, theta] > maxMapIntensity)
{
maxMapIntensity = A[radius, theta];
goodRadius = radius;
goodTheta = theta;
}
}
}
So, up to my understanding, i have now found the theta and radius of the intersecting point of all the curves. Then how can i find the real line ?
Some claim that I need to find the slope and a point, but it is really not clear to me what to do now.
Thanks for help, Jonathan
回答1:
The "maxMapIntensity" code is finding the coordinates of the single brightest point in the Hough output, so this will only find one line (which you've defined with your set of points). A single bright spot in the Hough output corresponds to a single line in the original image. You're finding a line that all these points go through. The goodRadius
and goodTheta
are the variables you're looking for - the parameters of that line.
To calculate the original line, you first would calculate the tangent line starting at the origin (start at the origin, make an angle of goodTheta, and then travel away from the origin goodRadius). Then at that point, the line of interest (the one you found) is perpendicular to the line from the origin you just created. In this diagram, goodRadius is written as ρ, and goodTheta is written as θ.
explanation of r theta http://saadjomaa.com/projects/ht/images/image002.jpg
来源:https://stackoverflow.com/questions/1409922/hough-transform-question