C++ 两分钟后捕获的网络摄像头视频中的目标检测?
我正在研究一种方法,通过OpenCV从我的网络摄像头(已经过颜色过滤)检测对象。它已经可以正常工作,但两分钟后,fps速率由于某种原因而降低 一些事实:C++ 两分钟后捕获的网络摄像头视频中的目标检测?,c++,opencv,memory-management,object-detection,C++,Opencv,Memory Management,Object Detection,我正在研究一种方法,通过OpenCV从我的网络摄像头(已经过颜色过滤)检测对象。它已经可以正常工作,但两分钟后,fps速率由于某种原因而降低 一些事实: 操作系统:Windows 8.1 OpenCV:2.4.10 IDE:Visual Studio 2013 我认为这与内存分配有关,因为正如我所说的,它在前两分钟工作正常 为了找出它是否与电脑有关,我已经在另一台电脑上测试过了,但也出现了同样的错误 void findAndDrawRect(Mat* imgThreshold, Mat* i
- 操作系统:Windows 8.1
- OpenCV:2.4.10
- IDE:Visual Studio 2013
void findAndDrawRect(Mat* imgThreshold, Mat* imgOriginal, SerialPort^ arduino){
// contains rectangles - each rectangle exists of points- of imgThreshold
std::vector<std::vector<cv::Point> > contours;
// contains hierarchical information from black-n white image
std::vector<cv::Vec4i> hierarchy;
cv::findContours(*imgThreshold,
contours,
hierarchy,
CV_RETR_TREE,
CV_CHAIN_APPROX_SIMPLE,
cv::Point(0, 0));
std::vector<std::vector<cv::Point> > contours_poly(contours.size());
// holds place for rectangle of each contour on the image
cv::Rect boundRect;
// tempory variable to identify largest rectangle
double refArea = 0;
double area = 0;
// pointer to largest rectangle
cv::Rect* obj = NULL;
for (int i = 0; i < contours.size(); i++)
{
// approximates a polygonal curve for each contour of third grade
cv::approxPolyDP(cv::Mat(contours[i]), contours_poly[i], 3, true);
// converts polynom of thrid grade to a rectangle
boundRect = cv::boundingRect(cv::Mat(contours_poly[i]));
// calculation of the area
area = boundRect.width * boundRect.height;
// if area is less than MIN_OBJECT_AREA, the rectangle is probably a noise
// if area is larger than MAX_OBJECT_AREA, size of rectangle is larger than 2/3 of screen = filter fail or approching cyclist is too near
if (area > MIN_OBJECT_AREA && area<MAX_OBJECT_AREA && area>refArea){
refArea = area;
obj = &boundRect;
}
}
// if rectangle exists, estimate the distance to the object and send warnings to the helmet
if (obj != NULL){
//calculation of distance to object
double currentDistance = (objRealWidth * focal_length) / obj->width;
// focal length = current measured width of A4 Paper in pixel * distance to camera(measured) / real width of A4 (measured)
focal_length = (measuredPixelWidth * distanceToCam) / objRealWidth;
// printf("width: %d \n", obj->width);
// convert from cm to m
currentDistance /= 100;
sprintf_s(Distance, "Distance :%f m", currentDistance);
putText(*imgOriginal, Distance, Point(0, 50), 1, 1, Scalar(0, 255, 0), 2);
rectangle(*imgOriginal, obj->tl(), obj->br(), cv::Scalar(0, 0, 255), 2, 8, 0);
switch (modus){
case 0: // NOISE
arduino->WriteLine("1");
objDetected = '1';
break;
case 1: // VIBRATION
arduino->WriteLine("2");
objDetected = '2';
break;
case 2: // LED
arduino->WriteLine("3");
objDetected = '3';
break;
case 3:
//measurement without alerting subject
arduino->WriteLine("4");
objDetected = '4';
break;
default:
break;
}
}
else{
// no object recognized
arduino->WriteLine("0");
objDetected = '0';
}
我真的不知道如何解决这个问题,因为我尝试了我能想到的一切。例如:我在计算过程中删除了所有未使用的向量项,但这没有帮助,所以我从代码中删除了它)
如果有任何问题,请随时提问,并提前向您表示感谢
您好,
一个沮丧的OpenCV新手 首先,不要使用指向Mat或Rect的指针。Mat对象已经在内部进行了ref计数。这是全部功能吗?为什么有
SerialPort^arduino
参数?那里没有用。请添加其余的函数,包括绘图部分…好的,我添加其余的代码,我需要“arduino”参数向arduino板发送消息@米卡,所以“我只是在图片上画一个长方形”不是一切objDetected
是全局变量吗?你在函数外使用该值做什么?我使用“objDetected”通过“命名管道”与另一个程序通信。我认为这与此无关,因为我删除了带有“命名管道”的部分,错误再次发生。我添加了使用“objDetected”的部分@米卡必须开始,不要使用指向Mat或Rect的指针。Mat对象已经在内部进行了ref计数。这是全部功能吗?为什么有SerialPort^arduino
参数?那里没有用。请添加其余的函数,包括绘图部分…好的,我添加其余的代码,我需要“arduino”参数向arduino板发送消息@米卡,所以“我只是在图片上画一个长方形”不是一切objDetected
是全局变量吗?你在函数外使用该值做什么?我使用“objDetected”通过“命名管道”与另一个程序通信。我认为这与此无关,因为我删除了带有“命名管道”的部分,错误再次发生。我添加了使用“objDetected”的部分@胡本
for (;;){
// This call blocks until a client process reads all the data
//const wchar_t *data = L"*** Hello Pipe ONUR ***";
char *data = &objDetected;
DWORD numBytesWritten = 0;
result = WriteFile(
pipe, // handle to our outbound pipe
data, // data to send
1, // length of data to send (bytes) + terminating !
&numBytesWritten, // will store actual amount of data sent
NULL // not using overlapped IO
);