Blob跟踪算法

时间:2012-09-08 12:32:15

标签: c++ opencv computer-vision tracking

我正在尝试使用OpenCV创建简单的blob跟踪。我使用findcontours检测到blob。我想给这些blob一个不变的ID。

我收集了前一帧和当前帧中的blob列表。然后我获取前一帧中每个blob与当前帧之间的距离。我想知道还有什么需要跟踪blob并给他们一个ID。我只是采用了前一帧和当前帧blob之间的距离,但是如何使用blob之间测量的距离为blob分配一致的ID?

3 个答案:

答案 0 :(得分:6)

在第一帧中,您可以任意方式分配ID,第一个找到1,第二个为2 ......或者根据它们在集合中的位置简单地给它们ID。

然后在下一帧,你将不得不使用最佳匹配。找到斑点,计算当前斑点与前一图像的所有斑点之间的所有距离,并将每个先前的ID分配给最近的斑点。刚进入该字段的Blob将获得新的ID。

现在您有两个框架,您可以为下一个框架进行运动预测。只需计算blob的前一个位置和当前位置之间的deltaX和deltaY。您可以使用此信息来猜测未来的位置。与未来的这个位置相匹配。

如果您没有很多重叠的斑点,并且每个帧之间的移动速度不是太快且不稳定,那么这应该有效。

通过几个图像使用评分系统可以更准确:
获取前3或5张图像的位置。对于帧中的一个的任何团块,寻求最接近的上框架2,计算速度(DELTAX移动deltaY),求最接近预测的位置为帧3,4,5 ...总结预测当前位置和接近的斑点它将之间的所有距离得分。使用第2帧最近的第二个(它将在另一个方向寻找)。分数越低,最有可能是好的斑点。

如果你有很多blob,你应该使用四叉树来加速进程。比较平方距离;它会避免大量的sqrt计算。

重要的是要知道你的blob如何调整你的算法。

答案 1 :(得分:3)

这是blob跟踪的OpenCV代码示例:

#include "stdafx.h"

#include <opencv2\opencv.hpp>

IplImage* GetThresholdedImage(IplImage* img)
{
    // Convert the image into an HSV image
    IplImage* imgHSV = cvCreateImage(cvGetSize(img), 8, 3);
    cvCvtColor(img, imgHSV, CV_BGR2HSV);

    IplImage* imgThreshed = cvCreateImage(cvGetSize(img), 8, 1);

    // Values 20,100,100 to 30,255,255 working perfect for yellow at around 6pm
    cvInRangeS(imgHSV, cvScalar(112, 100, 100), cvScalar(124, 255, 255), imgThreshed);

    cvReleaseImage(&imgHSV);

    return imgThreshed;
}

int main()
{
    // Initialize capturing live feed from the camera
    CvCapture* capture = 0;
    capture = cvCaptureFromCAM(0);  

    // Couldn't get a device? Throw an error and quit
    if(!capture)
    {
        printf("Could not initialize capturing...\n");
        return -1;
    }

    // The two windows we'll be using
    cvNamedWindow("video");
    cvNamedWindow("thresh");

    // This image holds the "scribble" data...
    // the tracked positions of the ball
    IplImage* imgScribble = NULL;

    // An infinite loop
    while(true)
    {
        // Will hold a frame captured from the camera
        IplImage* frame = 0;
        frame = cvQueryFrame(capture);

        // If we couldn't grab a frame... quit
        if(!frame)
            break;

        // If this is the first frame, we need to initialize it
        if(imgScribble == NULL)
        {
            imgScribble = cvCreateImage(cvGetSize(frame), 8, 3);
        }

        // Holds the yellow thresholded image (yellow = white, rest = black)
        IplImage* imgYellowThresh = GetThresholdedImage(frame);

        // Calculate the moments to estimate the position of the ball
        CvMoments *moments = (CvMoments*)malloc(sizeof(CvMoments));
        cvMoments(imgYellowThresh, moments, 1);

        // The actual moment values
        double moment10 = cvGetSpatialMoment(moments, 1, 0);
        double moment01 = cvGetSpatialMoment(moments, 0, 1);
        double area = cvGetCentralMoment(moments, 0, 0);

        // Holding the last and current ball positions
        static int posX = 0;
        static int posY = 0;

        int lastX = posX;
        int lastY = posY;

        posX = moment10/area;
        posY = moment01/area;

        // Print it out for debugging purposes
        printf("position (%d,%d)\n", posX, posY);

        // We want to draw a line only if its a valid position
        if(lastX>0 && lastY>0 && posX>0 && posY>0)
        {
            // Draw a yellow line from the previous point to the current point
            cvLine(imgScribble, cvPoint(posX, posY), cvPoint(lastX, lastY), cvScalar(0,255,255), 5);
        }

        // Add the scribbling image and the frame... and we get a combination of the two
        cvAdd(frame, imgScribble, frame);
        cvShowImage("thresh", imgYellowThresh);
        cvShowImage("video", frame);

        // Wait for a keypress
        int c = cvWaitKey(10);
        if(c!=-1)
        {
            // If pressed, break out of the loop
            break;
        }

        // Release the thresholded image... we need no memory leaks.. please
        cvReleaseImage(&imgYellowThresh);

        delete moments;
    }

    // We're done using the camera. Other applications can now use it
    cvReleaseCapture(&capture);
    return 0;
}

答案 2 :(得分:1)

你可以使用cvblobslib库进行斑点检测......

  1. 如果您的帧间斑点移动小于间隙距离..即斑点位移小于间隙距离那么您可以创建一个列表并继续在每个当前帧中添加blob上一帧中的斑点......
  2. 如果你的blob有一些常量的特征,比如椭圆度......宽高比(在将一个边界框装到它之后),你可以将带有这些特征的blob分组到一个列表中。
相关问题