首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >视频中的多重跟踪

视频中的多重跟踪
EN

Stack Overflow用户
提问于 2014-09-10 17:11:13
回答 3查看 4.3K关注 0票数 6

我正在工作的小图像处理任务,我需要跟踪4红色物体。我知道如何追踪单曲了。我想知道追踪不止一个点的最佳方法是什么。

有四个点是定位成一个矩形,所以我可以使用形状检测或角点检测和跟踪点,请参阅下图。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-09-14 21:27:57

我的简单实现使用了OpenCV包围盒描述的一种技术来跟踪红色斑点。

下面是一个帮助函数,用于检索所有检测到的红色对象的中心:

代码语言:javascript
运行
复制
/* get_positions: a function to retrieve the center of the detected blobs.
 * largely based on OpenCV's "Creating Bounding boxes and circles for contours" tutorial.
 */
std::vector<cv::Point2f> get_positions(cv::Mat& image)
{
    if (image.channels() > 1)
    {
        std::cout << "get_positions: !!! Input image must have a single channel" << std::endl;
        return std::vector<cv::Point2f>();
    }

    std::vector<std::vector<cv::Point> > contours;
    cv::findContours(image, contours, cv::RETR_LIST, cv::CHAIN_APPROX_SIMPLE);

    // Approximate contours to polygons and then get the center of the objects
    std::vector<std::vector<cv::Point> > contours_poly(contours.size());
    std::vector<cv::Point2f> center(contours.size());
    std::vector<float> radius(contours.size());
    for (unsigned int i = 0; i < contours.size(); i++ )
    {
        cv::approxPolyDP(cv::Mat(contours[i]), contours_poly[i], 5, true );
        cv::minEnclosingCircle((cv::Mat)contours_poly[i], center[i], radius[i]);
    }

    return center;
}   

我编写了代码来测试我的方法,通过从网络摄像头捕捉帧来实时测试我的方法。整个过程与@Dennis描述的非常相似(对不起,当您提交答案时,我已经在编写代码)。

好吧,这就是真正的乐趣开始。

代码语言:javascript
运行
复制
int main()
{
    // Open the capture device. My webcam ID is 0:
    cv::VideoCapture cap(0);
    if (!cap.isOpened())
    {
        std::cout << "!!! Failed to open webcam" << std::endl;
        return -1;
    }

    // Let's create a few window titles for debugging purposes
    std::string wnd1 = "Input", wnd2 = "Red Objs", wnd3 = "Output";   

    // These are the HSV values used later to isolate RED-ish colors
    int low_h = 160, low_s = 140, low_v = 50;
    int high_h = 179, high_s = 255, high_v = 255;

    cv::Mat frame, hsv_frame, red_objs;
    while (true)
    {
        // Retrieve a new frame from the camera
        if (!cap.read(frame))
            break;

        cv::Mat orig_frame = frame.clone();
        cv::imshow(wnd1, orig_frame);

orig_frame

代码语言:javascript
运行
复制
        // Convert BGR frame to HSV to be easier to separate the colors
        cv::cvtColor(frame, hsv_frame, CV_BGR2HSV);

        // Isolate red colored objects and save them in a binary image
        cv::inRange(hsv_frame,
                    cv::Scalar(low_h,  low_s,  low_v),
                    cv::Scalar(high_h, high_s, high_v),
                    red_objs);

        // Remove really small objects (mostly noises)
        cv::erode(red_objs, red_objs, cv::getStructuringElement(cv::MORPH_RECT, cv::Size(3, 3)));
        cv::dilate(red_objs, red_objs, cv::getStructuringElement(cv::MORPH_RECT, cv::Size(7, 7)));

        cv::Mat objs = red_objs.clone();
        cv::imshow(wnd2, objs);

objs

代码语言:javascript
运行
复制
        // Retrieve a vector of points with the (x,y) location of the objects
        std::vector<cv::Point2f> points = get_positions(objs);

        // Draw a small green circle at those locations for educational purposes
        for (unsigned int i = 0; i < points.size(); i++)
            cv::circle(frame, points[i], 3, cv::Scalar(0, 255, 0), -1, 8, 0);

        cv::imshow(wnd3, frame);

代码语言:javascript
运行
复制
        char key = cv::waitKey(33);
        if (key == 27) {   /* ESC was pressed */
            //cv::imwrite("out1.png", orig_frame);
            //cv::imwrite("out2.png", red_objs);
            //cv::imwrite("out3.png", frame);
            break;
        }
    }

    cap.release();

    return 0;
}
票数 4
EN

Stack Overflow用户

发布于 2014-09-10 18:31:54

以下是我在GitHub上的实现:youtube上的https://github.com/Smorodov/Multitarget-tracker视频:3 3RXRu pdVw

简言之:

  1. 探测物体。此步骤提供了一组点(检测到的对象坐标)。
  2. 求解一个赋值问题(矩形匈牙利算法)。此步骤将检测到的对象分配给现有轨道。
  3. 管理未分配/丢失的航迹。这一步删除多个连续漏检的轨道,并为新的检测添加跟踪。
  4. 应用统计滤波器对每个轨道(在这种情况下的卡尔曼滤波),用于预测丢失的检测和平滑跟踪对象的动力学信息(定义在卡尔曼滤波矩阵)。

顺便说一下,要得到4个点的坐标,你只需要知道3个点的坐标,因为你的图案是矩形的,你可以计算第4个点。

票数 6
EN

Stack Overflow用户

发布于 2014-09-14 18:42:10

以下是多色对象跟踪的步骤:

对于每个帧,执行以下步骤:

  1. 使用简历:cvtColor()将输入图像转换为HSV颜色空间
  2. 使用简历:inRange()分割红色对象
  3. 使用简历:findContours()从图像中提取每个blob
  4. 对于每个blob,使用简历:拟()计算其中心
  5. 匹配上一个帧中的小块,再加上一些移动到当前的小块,比较它们的中心之间的距离->如果它们的距离最小,就匹配它们。

这是算法的基础。然后,当blob进入图像时(在当前帧中有blob,但在前一个帧中没有关闭blob ),或者离开图像(在前一个帧中有一个blob,但在当前帧中没有关闭blob),您必须处理这些情况。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/25771252

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档