使用OpenCV实时隔离和跟踪多个对象?
我目前正在制作一个程序来跟踪4种不同颜色的桨。 我很难理解如何最好地继续,以及我现在拥有的知识,以及如何降低运行项目的计算成本。 这篇文章末尾列出了一些代码示例。
该程序包含一个名为Controllers的类文件,它具有简单的获取和设置函数,如X和Y位置,以及哪些HSV值用于阈值处理。
处于未优化状态的程序现在执行以下操作:
在这一点上,一切都在技术上起作用,但是通过从网络摄像头读取图像的while循环每循环执行三次形态操作所需的资源会极大地减慢程序的运行速度(在3 640处应用2次侵蚀迭代和3次扩张*以可接受的帧速率拍摄480张图像。)
不同桨的阈值图像
inRange(HSV, playerOne.getHSVmin(), playerOne.getHSVmax(), threshold1);
inRange(HSV, playerTwo.getHSVmin(), playerTwo.getHSVmax(), threshold2);
inRange(HSV, powerController.getHSVmin(), powerController.getHSVmax(), threshold3);
执行形态学操作
morphOps(threshold1);
void morphOps(Mat &thresh)
{
//Create a structuring element to be used for morph operations.
Mat structuringElement = getStructuringElement(MORPH_RECT, Size(3,3));
Mat dilateElement = getStructuringElement(MORPH_RECT, Size(6, 6));
//Perform the morphological operations, using two/three iterations because the noise is horrible.
erode(thresh, thresh, structuringElement, Point(-1, -1), 3);
dilate(thresh, thresh, dilateElement, Point(-1, -1), 2);
}
跟踪图像
trackFilteredObject(playerOne, threshold1, cameraFeed);
trackFilteredObject(playerTwo, threshold2, cameraFeed);
trackFilteredObject(powerController, threshold3, cameraFeed);
void trackFilteredObject(Controllers theControllers, Mat threshold, Mat HSV, Mat &cameraFeed)
{
vector <Controllers> players;
Mat temp;
threshold.copyTo(temp);
//these vectors are needed to save the output of findCountours
vector< vector<Point> > contours;
vector<Vec4i> hierarchy;
//Find the contours of the image
findContours(temp, contours, hierarchy, CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE);
//Moments are used to find the filtered objects.
double refArea = 0;
bool objectFound = false;
if (hierarchy.size() > 0)
{
int numObjects = hierarchy.size();
//If there are more objects than the maximum number of objects we want to track, the filter may be noisy.
if (numObjects < MAX_NUM_OBJECTS)
{
for (int i = 0; i >= 0; i = hierarchy[i][0])
{
Moments moment = moments((Mat)contours[i]);
double area = moment.m00;
//If the area is less than min area, then it is probably noise
if (area > MIN_AREA)
{
Controllers player;
player.setXPos(moment.m10 / area);
player.setYPos(moment.m01 / area);
player.setType(theControllers.getType());
player.setColor(theControllers.getColor());
players.push_back(player);
objectFound = true;
}
else objectFound = false;
}
//Draw the object location on screen if an object is found
if (objectFound)
{
drawObject(players, cameraFeed);
}
}
}
}
我的想法是,我希望能够隔离每个对象,并将X和Y位置用作三角形的点,并使用这些信息来计算箭头拍摄的角度和强度。 所以我想知道是否有更好的方法来隔离彩色桨并消除噪音,这并不需要我为每种颜色执行这些形态操作。
链接地址: http://www.djcxy.com/p/23693.html上一篇: Isolating and tracking multiple objects in real time using OpenCV?
下一篇: How to pass data from selected rows using checkboxes from JSP to the server