如何在OpenCV中利用SIFT提取的特征得到目标物体周围的矩形

发布于 2024-12-27 11:38:43 字数 125 浏览 2 评论 0原文

我正在 OpenCV 中进行对象检测项目,其中包括将模板图像中的对象与参考图像进行匹配。使用 SIFT 算法可以准确地检测和匹配特征,但我想要围绕匹配特征的矩形 我的算法使用 KD-Tree est ean First 技术来获取匹配项

I'm doing project in OpenCV on object detection which consists of matching the object in template image with the reference image. Using SIFT algorithm the features get acurately detected and matched but I want a rectagle around the matched features
My algorithm uses the KD-Tree est ean First technique to get the matches

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

雨轻弹 2025-01-03 11:38:43

如果您想要在检测到的对象周围有一个矩形,此处您可以找到与此完全相同的代码示例。您只需要在单应性 H 周围绘制一个矩形

。希望它有所帮助。祝你好运。

If you want a rectangle around the detected object, here you have code example with exactly that. You just need to draw a rectangle around the homography H.

Hope it helps. Good luck.

不奢求什么 2025-01-03 11:38:43

我使用以下代码(改编自 OpenCV 中的 SURF 算法(modules/features2d/src/surf.cpp))来提取关键点的周围。

除了基于矩形和 ROI 的其他示例之外,此代码根据特征检测算法确定的方向和比例返回正确定向的面片(两者都在 KeyPoint 结构中可用)。

对几个不同图像的检测结果的示例:

SIFT keypoint patch extract example

const int PATCH_SZ = 20;
Mat extractKeyPoint(const Mat& image, KeyPoint kp)
{
    int x = (int)kp.pt.x;
    int y = (int)kp.pt.y;
    float size = kp.size;
    float angle = kp.angle;

    int win_size = (int)((PATCH_SZ+1)*size*1.2f/9.0);
    Mat win(win_size, win_size, CV_8UC3);

    float descriptor_dir = angle * (CV_PI/180);
    float sin_dir = sin(descriptor_dir);
    float cos_dir = cos(descriptor_dir);
    float win_offset = -(float)(win_size-1)/2;
    float start_x = x + win_offset*cos_dir + win_offset*sin_dir;
    float start_y = y - win_offset*sin_dir + win_offset*cos_dir;
    uchar* WIN = win.data;
    uchar* IMG = image.data;
    for( int i = 0; i < win_size; i++, start_x += sin_dir, start_y += cos_dir )
    {
        float pixel_x = start_x;
        float pixel_y = start_y;
        for( int j = 0; j < win_size; j++, pixel_x += cos_dir, pixel_y -= sin_dir )
        {
            int x = std::min(std::max(cvRound(pixel_x), 0), image.cols-1);
            int y = std::min(std::max(cvRound(pixel_y), 0), image.rows-1);
            for (int c=0; c<3; c++) {
                WIN[i*win_size*3 + j*3 + c] = IMG[y*image.step1() + x*3 + c];
            }
        }
    }
    return win;
}

我不确定比例是否是完全没问题,但它取自 SURF 源,结果看起来与我相关。

I use the following code, adapted from the SURF algoritm in OpenCV (modules/features2d/src/surf.cpp) to extract a surrounding of a keypoint.

Apart from other examples based on rectangles and ROI, this code returns the patch correctly oriented according to the orientation and scale determined by the feature detection algorithm (both available in the KeyPoint struct).

An example of the results of the detection on several different images:

SIFT keypoint patch extract example

const int PATCH_SZ = 20;
Mat extractKeyPoint(const Mat& image, KeyPoint kp)
{
    int x = (int)kp.pt.x;
    int y = (int)kp.pt.y;
    float size = kp.size;
    float angle = kp.angle;

    int win_size = (int)((PATCH_SZ+1)*size*1.2f/9.0);
    Mat win(win_size, win_size, CV_8UC3);

    float descriptor_dir = angle * (CV_PI/180);
    float sin_dir = sin(descriptor_dir);
    float cos_dir = cos(descriptor_dir);
    float win_offset = -(float)(win_size-1)/2;
    float start_x = x + win_offset*cos_dir + win_offset*sin_dir;
    float start_y = y - win_offset*sin_dir + win_offset*cos_dir;
    uchar* WIN = win.data;
    uchar* IMG = image.data;
    for( int i = 0; i < win_size; i++, start_x += sin_dir, start_y += cos_dir )
    {
        float pixel_x = start_x;
        float pixel_y = start_y;
        for( int j = 0; j < win_size; j++, pixel_x += cos_dir, pixel_y -= sin_dir )
        {
            int x = std::min(std::max(cvRound(pixel_x), 0), image.cols-1);
            int y = std::min(std::max(cvRound(pixel_y), 0), image.rows-1);
            for (int c=0; c<3; c++) {
                WIN[i*win_size*3 + j*3 + c] = IMG[y*image.step1() + x*3 + c];
            }
        }
    }
    return win;
}

I am not sure if the scale is entirely OK, but it is taken from the SURF source and the results look relevant to me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文