【问题标题】:Hausdorff Distance Object DetectionHausdorff 距离目标检测
【发布时间】:2018-03-20 03:05:53
【问题描述】:

我一直在努力尝试实现herehere 描述的大纲算法。

论文的总体思路是确定二值图像的豪斯多夫距离,并使用它从测试图像中找到模板图像。

对于模板匹配,建议构建image pyramids 以及滑动窗口,您将使用滑动窗口在测试图像上滑动以进行检测。我也能做到这两点。

我被困在如何从这里继续前进。我是否将模板从不同的金字塔层滑过测试图像?还是模板上的测试图像?关于滑动窗口,它们是/是否意味着测试或模板图像的 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;
}

【问题讨论】:

    标签: c++ opencv object-detection template-matching


    【解决方案1】:

    编辑我:更多分析我的解决方案可以找到here


    这是一个双向任务。

    前进方向


    1.翻译

    对于每个轮廓,计算其moment。然后对于该轮廓中的每个点,将其转换为有关时刻,即contour.point[i] = contour.point[i] - contour.moment[i]。这会将所有轮廓点移动到原点。

    PS:您需要跟踪每个轮廓的产生时刻,因为它将在下一部分中使用

    2。轮换

    使用新翻译的点,计算它们的rotated rect。这将为您提供旋转角度。根据这个角度,您需要计算要旋转此轮廓的新角度; this answer 会很有帮助。

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

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

    使用此矩阵,您可以继续并通过它旋转轮廓中的每个点,如图所示here*

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


    后向

    必须撤消第一部分中完成的所有操作,以便我们将有效轮廓绘制到我们的相机源上。


    1.轮换

    回想一下,每个检测到的轮廓都会产生一个旋转矩阵。您想要撤消有效轮廓的旋转。只需执行相同的轮换,但使用 inverse matrix

    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:在计算逆时,如果生成的矩阵不是一个平方,它会失败。 cv2.DECOMP_SVD 将产生一个逆矩阵,即使原始矩阵是非正方形的。

    2。翻译

    将有效轮廓的点向后旋转,您只需撤消之前执行的平移。无需减去,只需将时刻添加到每个点。

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


    缩放


    这是图像金字塔发挥作用的时候。

    您所要做的就是将模板图​​像的大小调整为固定大小/比例,直至达到您想要的次数(称为图层)。教程发现 here 很好地解释了如何在 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)
    

    【讨论】:

      猜你喜欢
      • 2021-06-17
      • 1970-01-01
      • 1970-01-01
      • 2019-07-09
      • 2020-01-29
      • 1970-01-01
      • 2012-05-24
      • 2023-02-07
      • 1970-01-01
      相关资源
      最近更新 更多