# 3D Hough变换点云平面检测算法

1.适用场景分析：

2.算法思路：

2.1“投票”算法

2.2法向量的转换

2.3累加器

2.4需要考虑的问题：

3.针对具体应用的改进：

4.分析以及与类似算法进行对比：

HT算法的优点：

HT的缺点：

5.参考文献：

6.实现代码：

#define  PI 3.141592653
void HoughTransform(const std::vector<Point>& input, double& A, double& B, double& C, double& D)
{
int n = input.size();
if (n < 3)
return;
double theta_start=0, theta_end=PI;
double phi_start=0, phi_end=PI;
//double phi_start = 0.25*PI, phi_end = 0.75*PI;
double anglestep=PI/90, disstep=0.1;
boundingbox box;
calcboundbox(input, box);
double d_start = -box.diag() / 2.0, d_end = box.diag() / 2.0;
int thetas = ceil((theta_end - theta_start) / anglestep);
int phis = ceil((phi_end - phi_start) / anglestep);
int dises = ceil( box.diag()/disstep);
int*** cube = new int**[thetas];
for (int i = 0; i < thetas;++i)
{
cube[i] = new int*[phis];
for (int j = 0; j < phis; ++j)
{
cube[i][j] = new int[dises];
memset(cube[i][j], 0, sizeof(int)*dises);
}
}
//cos(theta)sin(phi)X+sin(theta)sin(phi)Y+cos(phi)Z = D
Point ptCenter = box.center();
for (int i = 0; i < n;++i)
{
const Point& ptOrigin = input[i];
Point point = ptOrigin - ptCenter;
double theta = theta_start;
for(int j = 0; j < thetas; ++j)
{
int** row = cube[j];
double phi = phi_start;
for (int k = 0; k < phis; ++k)
{
int* col = row[k];
double sinphi = sin(phi);
double d = cos(theta)*sinphi*point.x + sin(theta)*sinphi*point.y + cos(phi)*point.z;
int d_index = floor((d - d_start) / disstep);
++(col[d_index]);
phi += anglestep;
if (phi > phi_end)
break;
}
theta += anglestep;
if (theta > theta_end)
break;
}
}//all points
int buf = 1;
int maxcount = 0;
int xmax, ymax, zmax;
for (int i = 0; i < thetas;++i)
for (int j = 0; j < phis; ++j)
for (int k = buf; k < dises - buf;++k)
{
int count = 0;
for (int x = i - buf; x <= i + buf; ++x)
for (int y = j - buf; y <= j + buf; ++y)
for (int z = k - buf; z <= k + buf; ++z)
{
count += cube[x<0?x+thetas:x%thetas][y<0?y+phis:y%phis][z];
}
if (count > maxcount)
{
xmax = i;
ymax = j;
zmax = k;
maxcount = count;
}
}
double theta = theta_start + xmax*anglestep;
double phi = phi_start + ymax*anglestep;
double d = d_start + zmax*disstep;
A = cos(theta)*sin(phi);
B = sin(theta)*sin(phi);
C = cos(phi);
D = -d - (A*ptCenter.x + B*ptCenter.y+C*ptCenter.z);
//std::cout << A << " , " << B << " , " << C << " , "<< D << std::endl;
//释放cube
for (int i = 0; i < thetas; ++i)
{
int** row = cube[i];
for (int j = 0; j < phis;++j)
{
int* col = row[j];
delete[] col;
}
delete[] row;
}
delete[] cube;
}

class Point
{
public:
double x, y, z;
Point(double ix,double iy,double iz) :
x(ix), y(iy), z(iz){}
Point operator-(const Point& pt) const
{
return Point(x - pt.x, y - pt.y, z - pt.z);
}
};
typedef Point Vector;
class boundingbox
{
public:
double x_min, x_max;
double y_min, y_max;
double z_min, z_max;
public:
double diag() const
{
double dx = x_max - x_min;
double dy = y_max - y_min;
double dz = z_max - z_min;
return sqrt(dx*dx + dy*dy + dz*dz);
}
boundingbox():
x_min(std::numeric_limits<double>::max()),
y_min(std::numeric_limits<double>::max()),
z_min(std::numeric_limits<double>::max()),
x_max(-std::numeric_limits<double>::max()),
y_max(-std::numeric_limits<double>::max()),
z_max(-std::numeric_limits<double>::max())
{}
Point center() const
{
return Point((x_max + x_min) / 2.0,(y_min+y_max) / 2.0, (z_min+z_max) / 2.0);
}
};
void calcboundbox(const std::vector<Point>& input, boundingbox& box)
{
for (int i = 0, n = input.size(); i < n;++i)
{
auto point = input[i];
if (point.x < box.x_min)
box.x_min = point.x;
if (point.y < box.y_min)
box.y_min = point.y;
if (point.z < box.z_min)
box.z_min = point.z;
if (point.x > box.x_max)
box.x_max = point.x;
if (point.y > box.y_max)
box.y_max = point.y;
if (point.z > box.z_max)
box.z_max = point.z;
}
}

|
30天前
|

8月更文挑战第11天
35 6
|
1月前
|

8月更文挑战第5天
55 8
|
1月前
|

8月更文挑战第8天
36 2
|
24天前
|

9 0
|
2月前
|

【7月更文第18天】目标检测，作为计算机视觉领域的核心任务之一，旨在识别图像或视频中特定对象的位置及其类别。这一技术在自动驾驶、视频监控、医疗影像分析等多个领域发挥着至关重要的作用。本文将深入浅出地介绍目标检测的基本概念、主流算法，并通过一个实际的代码示例，带您领略YOLOv5这一高效目标检测模型的魅力。
294 11
|
2月前
|

81 5
|
1月前
|

23 0
|
1月前
|

14 0
|
2月前
|

【7月更文挑战第13天】目标检测作为计算机视觉领域的重要研究方向，近年来在深度学习技术的推动下取得了显著进展。然而，面对复杂多变的实际应用场景，仍需不断研究和探索更加高效、鲁棒的目标检测算法。随着技术的不断发展和应用场景的不断拓展，相信目标检测算法将在更多领域发挥重要作用。
88 9
|
8天前
|

39 20