Hausdorff距离目标检测

Hausdorff Distance Object Detection

本文关键字:检测 目标 距离 Hausdorff      更新时间:2023-10-16

我一直在努力实现这里和这里描述的概述算法。

本文的总体思想是确定二值图像的Hausdorff距离,并用它从测试图像中找到模板图像。

对于模板匹配,建议构建图像金字塔和滑动窗口,您可以使用滑动窗口在测试图像上滑动进行检测。这两件事我都能做到。

我被困在如何从现在开始前进的问题上。我要在不同金字塔层的测试图像上滑动我的模板吗?还是模板上的测试图像?关于滑动窗口,它们是否意味着测试或模板图像的ROI?

简而言之,我有一些难题,但不知道该朝哪个方向解决难题

int distance(vector<Point>const& image, vector<Point>const& tempImage)
{
int maxDistance = 0;
for(Point imagePoint: image)
{
int minDistance = numeric_limits<int>::max();
for(Point tempPoint: tempImage)
{
Point diff = imagePoint - tempPoint;
int length = (diff.x * diff.x) + (diff.y * diff.y);
if(length < minDistance) minDistance = length;
if(length == 0) break;
}
maxDistance += minDistance;
}
return maxDistance;
}
double hausdorffDistance(vector<Point>const& image, vector<Point>const& tempImage)
{
double maxDistImage = distance(image, tempImage);
double maxDistTemp = distance(tempImage, image);
return sqrt(max(maxDistImage, maxDistTemp));
}
vector<Mat> buildPyramids(Mat& frame)
{
vector<Mat> pyramids;
int count = 6;
Mat prevFrame = frame, nextFrame;
while(count > 0)
{
resize(prevFrame, nextFrame, Size(), .85, .85);
prevFrame = nextFrame;
pyramids.push_back(nextFrame);
--count;
}
return pyramids;
}
vector<Rect> slidingWindows(Mat& image, int stepSize, int width, int height)
{
vector<Rect> windows;
for(size_t row = 0; row < image.rows; row += stepSize)
{
if((row + height) > image.rows) break;
for(size_t col = 0; col < image.cols; col += stepSize)
{
if((col + width) > image.cols) break;
windows.push_back(Rect(col, row, width, height));
}
}
return windows;
}

编辑I:关于我的解决方案的更多分析可以在这里找到


这是一项双向任务。

前进方向


1.翻译

对于每个轮廓,计算其力矩。然后,对于该轮廓中的每个点,将其平移大约一个时刻,即contour.point[i] = contour.point[i] - contour.moment[i]。这会将所有轮廓点移动到原点。

PS:你需要跟踪每个轮廓产生的力矩,因为它将在下一节中使用

2.旋转

使用新平移的点,计算它们的旋转矩形。这将为您提供旋转角度。根据此角度,您可能需要计算新的角度,以旋转此轮廓;这个答案会很有帮助。

获得新角度后,计算旋转矩阵。请记住,你在这里的中心将是原点,即(0, 0)。在计算旋转矩阵时,我没有考虑缩放(这就是金字塔发挥作用的地方),因此我通过了1。

PS:您需要跟踪每个轮廓的生成矩阵,因为它将在下一节中使用

使用这个矩阵,你可以继续旋转轮廓中的每个点,如这里*所示。

完成所有这些后,您可以继续计算Hausdorff距离,并找到通过您设置的阈值的轮廓。


反向

第一部分中所做的一切都必须撤消,以便我们在相机馈送上绘制有效的轮廓。


1.旋转

回想一下,每个检测到的轮廓都产生了一个旋转矩阵。您希望撤消有效轮廓的旋转。只需执行相同的旋转,但使用逆矩阵。

For each valid contour and corresponding matrix
inverse_matrix = matrix[i].inv(cv2.DECOMP_SVD)
Use * to rotate the points but with inverse_matrix as parameter

PS:在计算逆时,如果生成的矩阵不是平方矩阵,它就会失败。即使原始矩阵是非平方矩阵,CCD_ 3也将产生逆矩阵

2.翻译

将有效轮廓的点向后旋转后,只需撤消先前执行的平移即可。不用减法,只需将力矩加到每一点上即可。

现在,您可以继续将这些轮廓绘制到您的相机提要中。


缩放


这就是图像金字塔发挥作用的地方。

你所要做的就是按照固定的大小/比例调整模板图像的大小,直到你想要的次数(称为层)。这里的教程很好地解释了如何在OpenCV中做到这一点。

不用说,你选择的调整图像大小的值和层数将在你的程序的健壮性方面发挥巨大作用。


把它们放在一起

模板图像操作

Create a pyramid consisting of n layers
For each layer in n
Find contours
Translate the contour points
Rotate the contour points

此操作只能执行一次,并且只能存储旋转点的结果。

相机进纸操作

假设

将每个级别的模板图像的旋转轮廓存储在templ_contours中。如果我说templ_contours[0],这将给我在金字塔级别0的旋转轮廓。

让图像的平移、旋转轮廓和力矩分别存储在transControtContmoment中。

image_contours = Find Contours
for each contour detected in image
moment = calculate moment
for each point in image_contours
transCont.thisPoint = forward_translate(image_contours.thisPoint)
rotCont.thisPoint = forward_rotate(transCont.thisPoint)
for each contour_layer in templ_contours
for each contour in rotCont
calculate Hausdorff Distance
valid_contours = contours_passing_distance_threshold
for each point in valid_contours
valid_point = backward_rotate(valid_point)
for each point in valid_contours
valid_point = backward_translate(valid_point)
drawContours(valid_contours, image)