我正在尝试使用OpenCV创建简单的斑点跟踪。我使用findcontours检测到了斑点。我想给这些blob一个恒定的ID。Blob跟踪算法
我收集了前一帧和当前帧的斑点的列表。然后我取前一帧中每个斑点与当前帧之间的距离。我想知道还需要跟踪斑点并给他们一个ID。我只是把前一帧和当前帧blob之间的距离,但我怎样才能使用blob之间测量的距离分配一个一致的ID?
我正在尝试使用OpenCV创建简单的斑点跟踪。我使用findcontours检测到了斑点。我想给这些blob一个恒定的ID。Blob跟踪算法
我收集了前一帧和当前帧的斑点的列表。然后我取前一帧中每个斑点与当前帧之间的距离。我想知道还需要跟踪斑点并给他们一个ID。我只是把前一帧和当前帧blob之间的距离,但我怎样才能使用blob之间测量的距离分配一个一致的ID?
在第一帧,你可以分配ID的方式,1代表第一个你发现,2第二......或者干脆根据自己收集的位置给他们的ID。
然后在下一帧,你将不得不使用最佳匹配。找到斑点,计算当前斑点和之前图像的所有斑点之间的所有距离,并将每个先前的ID分配给最近的斑点。刚进入该字段的Blob将获得新的ID。
现在你有两个框架,可以为下一个做动作预测。只需计算blob的前一个和当前位置之间的deltaX和deltaY。您可以使用这些信息来猜测未来的位置。匹配这个未来的位置。
如果你没有很多重叠的斑点,如果运动不是太快,每个帧之间飘忽这应该工作。
通过几个图像可以更准确地使用评分系统:
获取头3或5张图像的位置。对于第一帧的任何斑点,寻找帧2上最接近的帧,计算速度(deltaX deltaY),为帧3,4,5求最接近预测的位置...总结预测位置和最接近斑点之间的所有距离,得分。使用第2帧在第2帧(它将在另一个方向寻找)执行相同的操作。分数越低,最有可能的就是好的一团。
如果你有很多的斑点,你应该使用一个四叉树用来加快进程。比较平方距离;它会避免大量的sqrt计算。
知道自己的斑点tipically移动来调整你的algotrithm是很重要的。
谢谢,请您通过示例和计算或图片给我更多解释吗?我很乐意了解这一过程。或者你可以给我看看那个 –
@MoazELdeen我编辑了答案 – bokan
这里的斑点跟踪的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;
}
u可以使用cvblobslib库斑点检测...
你能说明你的问题吗? – bokan
修改了问题 –