本文介绍了运动检测的opencv代码出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找实现人类运动跟踪,也在用于连续运动跟踪,但我不明白如何去进行这个过程。所有我需要的是轨迹和人类的坐标步态运动。



我在Opencv和C ++中工作,但opencv中用于对象检测的文档在我的例子中是不够的。我来自医学背景,需要这是儿科医生项目的一部分。



我找到此代码,并试图执行它(不知道它是否检测到并跟踪运动)。但是,它返回这些错误,我很困惑的错误是微不足道的,其他意见提到,他们能够运行此代码。但我不能减轻这些错误,我也不能理解他们发生的原因。我使用OpenCv2.3和以下是错误


  1. 无法打开surce文件stdafx.h

  2. warning C4996:'fopen':此函数或变量可能不安全。请考虑使用fopen_s。要禁用弃用,请使用_CRT_SECURE_NO_WARNINGS。详情请参阅在线帮助。

  3. 错误C2086:'CvSize imgSize':redefinition

  4. 错误C2065:'temp':undeclared identifier

  5. 错误C4430:缺少类型说明符 - int。注意:C ++不支持default-int


    • 错误C2365:'cvReleaseImage':redefinition;以前的定义是'function'
      1> c:\opencv2.3\\\\\\\\\\\\\\\\\\\\\\\\ cnopencv2\core\core_c.h(87):参见'cvReleaseImage' / li>

  6. 错误C2065:'difference':undeclared标识符

  7. 错误C4430:假定。注意:C ++不支持default-int

  8. 错误C2365:'cvReleaseImage':redefinition;以前的定义是'function'
    1> c:\opencv2.3\\\\\\\\\\\\\\\\\\\\\\\\ cnopencv2\core\core_c.h(87):参见'cvReleaseImage' / li>
  9. 错误C2065:'greyImage':undeclared标识符

  10. 错误C4430:缺少类型说明符 - int。注意:C ++不支持default-int

  11. 错误C2365:'cvReleaseImage':redefinition;之前的定义是'function'

  12. \opencv2.3\opencv\build\include\opencv2\core\core_c.h(87) cvReleaseImage'
    错误C2065:'movingAverage':undeclared标识符
    -error C4430:缺少类型说明符 - int。注意:C ++不支持default-int
    -error C2365:'cvReleaseImage':redefinition;以前的定义是'function'
    -1> c:\opencv2.3\\\\\\\\\\\\\\\\\\\\\\\\\\ cn_cn.h(87):参见'cvReleaseImage'
    -error C4430:缺少类型说明符 - int。注意:C ++不支持default-int
    -error C2365:'cvDestroyWindow':redefinition;上一个定义是'function'

  13. c:\opencv2.3\opencv\build\include\opencv2\highgui\highgui_c.h(136) 'cvDestroyWindow'

  14. 错误C2440:'initializing':无法从'const char [10]'转换为'int'
    -1>可以转换
    -error C2065:'input':undeclared identifier
    -error C4430:缺少类型说明符 - int。注意:C ++不支持default-int

  15. 错误C2365:'cvReleaseCapture':redefinition;前面的定义是'function'
    -1> c:\opencv2.3\opencv\build\include\opencv2\highgui\highgui_c.h(311):参见'cvReleaseCapture'
    -error C2065:'outputMovie':undeclared identifier

  16. 错误C4430:缺少类型说明符 - int。注意:C ++不支持default-int
    -error C2365:'cvReleaseVideoWriter':redefinition;以前的定义是'function'
    -1 c:\opencv2.3\opencv\build\include\opencv2\highgui\highgui_c.h(436):参见cvReleaseVideoWriter的声明
    -error C2059:语法错误:'return'
    ========== Build:0成功,1失败,0最新,0跳过====== ====

CODE

  // MotionDetection.cpp:定义控制台应用程序的入口点。 
//


// Contourold.cpp:定义控制台应用程序的入口点。
//
#includestdafx.h

#includeiostream
#includestdlib.h

/ / OpenCV包括。
#includecv.h
#includehighgui.h
#pragma comment(lib,cv.lib)
#pragma comment(lib,cxcore .lib)
#pragma comment(lib,highgui.lib)

using namespace std;

int main(int argc,char * argv [])
{

//创建一个新窗口。
cvNamedWindow(My Window,CV_WINDOW_AUTOSIZE);

//创建一个新的电影捕获对象。
CvCapture * input;

//分配要捕获的影片。
// inputMovie = cvCaptureFromAVI(vinoth.avi);

char * fileName =E:\\highway.avi;
// char * fileName =D:\\Profile\\AVI\\ cardriving.wmv;
input = cvCaptureFromFile(fileName);
// if(!input)

// cout<< 无法打开文件<< fileName< ;



//图像大小。
CvSize imgSize;
IplImage * frame = cvQueryFrame(input);
CvSize imgSize = cvGetSize(frame);

//程序中使用的图片。
IplImage * greyImage = cvCreateImage(imgSize,IPL_DEPTH_8U,1);
IplImage * colourImage;
IplImage * movingAverage = cvCreateImage(imgSize,IPL_DEPTH_32F,3);
IplImage * difference;
IplImage * temp;
IplImage * motionHistory = cvCreateImage(imgSize,IPL_DEPTH_8U,3);

//用于放置人的矩形。
CvRect bndRect = cvRect(0,0,0,0);

//矩形边缘的点。
CvPoint pt1,pt2;

//创建字体对象。
CvFont font;


//创建要输出的视频。
char * outFilename = argc == 2? argv [1]:E:\\outputMovie.avi;
CvVideoWriter * outputMovie = cvCreateVideoWriter(outFilename,
CV_FOURCC('F','L','V','I'),29.97,cvSize(720,576)

//逐帧捕获动画。
int prevX = 0;
int numPeople = 0;

//将整数
//转换为字符串时缓冲保存的人数。
char wow [65];

//围绕运动对象的矩形的中点X位置。
int avgX = 0;

//指示这是否是帧循环中的第一次。
bool first = true;

//表示在对象之前最接近左边界的轮廓
//进入建筑物之间的区域。
int closestToLeft = 0;
//与上面一样,但是对于右边。
int closestToRight = 320;

//保持处理帧...
for(;;)
{
//从输入视频中获取一帧。
colourImage = cvQueryFrame(input);

//如果没有更多的帧,跳出for。
if(!colourImage)
{
break;
}

//如果这是第一次,请初始化图像。
if(first)
{
difference = cvCloneImage(colourImage);
temp = cvCloneImage(colourImage);
cvConvertScale(colourImage,movingAverage,1.0,0.0);
first = false;
}
// else,使运动的运行平均值。
else
{
cvRunningAvg(colourImage,movingAverage,0.020,NULL);
}

//转换移动平均值的比例。
cvConvertScale(movingAverage,temp,1.0,0.0);

//从移动平均值中减去当前帧。
cvAbsDiff(colourImage,temp,difference);

//将图像转换为灰度。
cvCvtColor(difference,greyImage,CV_RGB2GRAY);

//将图像转换为黑白图像。
cvThreshold(greyImage,greyImage,70,255,CV_THRESH_BINARY);

//扩展和侵蚀得到人blobs
cvDilate(greyImage,greyImage,0,18);
cvErode(greyImage,greyImage,0,10);

//查找框架中运动图像的轮廓。
CvMemStorage * storage = cvCreateMemStorage(0);
CvSeq * contour = 0;
cvFindContours(greyImage,storage,& contour,sizeof(CvContour),CV_RETR_CCOMP,CV_CHAIN_APPROX_SIMPLE);

//处理当前帧中的每个运动轮廓...
for(; contour!= 0; contour = contour-> h_next)
{
//获取移动对象周围的有界矩形。
bndRect = cvBoundingRect(contour,0);

pt1.x = bndRect.x;
pt1.y = bndRect.y;
pt2.x = bndRect.x + bndRect.width;
pt2.y = bndRect.y + bndRect.height;

//获取移动轮廓的平均X位置。
avgX =(pt1.x + pt2.x)/ 2;

//如果轮廓在建筑物的边缘内...
if(avgX> 90& amp; avgX< 250)
{
//如果前一轮廓在左边界的2内...
if(closestToLeft> = 88&& nearestToLeft< = 90)
{
//如果当前X位置大于前一个...
if(avgX> prevX)
{
//增加人数。
numPeople ++;

//将最近的对象重置为左指示器。
closestToLeft = 0;
}
}
//如果先前的轮廓在右边界的2内,则为else else ...
else if(closestToRight> = 250&& closestToRight< = 252)
{
//如果当前X位置小于前一个...
if(avgX

{
//增加人数。
numPeople ++;

//将最近的对象重置为右计数器。
closestToRight = 320;
}
}

//绘制移动物体周围的边界矩形。
cvRectangle(colourImage,pt1,pt2,CV_RGB(255,0,0),1);
}

//如果当前对象更接近左边界,但仍然没有跨过
//它,那么将最左边的计数器更改为此值。
if(avgX> closestToLeft& amp; avgX< = 90)
{
closestToLeft = avgX;
}

//如果当前对象更接近右边界,但是不能跨越
//,那么将最右边的计数器更改为该值。
if(avgX< nearestToRight& avgX> = 250)
{
closestToRight = avgX;
}

//保存当前X值,以在下一次迭代中用作上一个。
prevX = avgX;
}
//保存当前X值,以在下一次迭代中用作上一个。
prevX = avgX;
}


//写入计数在输出帧顶部的人数。
cvInitFont(& font,CV_FONT_HERSHEY_SIMPLEX,0.8,0.8,0,2);
cvPutText(colourImage,_itoa(numPeople,wow,10),cvPoint(60,200),& font,cvScalar(0,0,300)

//显示框架。
cvShowImage(My Window,colourImage);

//等待用户看到它。
cvWaitKey(10);

//将帧写入输出影片。
cvWriteFrame(outputMovie,colourImage);
}

//销毁图像,电影和窗口。
cvReleaseImage(& temp);
cvReleaseImage(& difference);
cvReleaseImage(& greyImage);
cvReleaseImage(& movingAverage);
cvDestroyWindow(My Window);

cvReleaseCapture(& input);
cvReleaseVideoWriter(& outputMovie);


return 0;

}




  • 错误和问题。

  • 如何做运动(人类)跟踪,可能通过骨架化方法返回轨迹的坐标。


解决方案

1。)我认为你直接从网站复制代码(如果我错了,请纠正我。然而,因为你使用OpenCV 2.3,所以大部分的API在不同的模块。以下包括你应该是...

  #include< opencv / core / core.hpp> 
#include< opencv / highgui / highgui.hpp>
#include< opencv / imgproc / imgproc.hpp>

和各个图书馆。



2 。)要过滤连接的组件,您可以使用。我认为OpenCV提供的旧Blob库是使用VC 6构建的,因此可能需要 stdafx.h






编辑代码 b $ b

  #include< iostream> 
#includestdlib.h


#include< opencv2 / core / core.hpp>
#include< Opencv2 / highgui / highgui.hpp>
#include< opencv2 / imgproc / imgproc.hpp>

#includecv.h
#includehighgui.h

using namespace std;
using namespace cv;

int main(int argc,char * argv [])
{
cvNamedWindow(My Window,CV_WINDOW_AUTOSIZE);
CvCapture * input;

// char * fileName =E:\\highway.avi;
input = cvCaptureFromCAM(0);

// input = cvCaptureFromFile(fileName);

CvSize imgSize;
IplImage * frame = cvQueryFrame(input);
imgSize = cvGetSize(frame);

IplImage * greyImage = cvCreateImage(imgSize,IPL_DEPTH_8U,1);
IplImage * colourImage;
IplImage * movingAverage = cvCreateImage(imgSize,IPL_DEPTH_32F,3);
IplImage * difference;
IplImage * temp;
IplImage * motionHistory = cvCreateImage(imgSize,IPL_DEPTH_8U,3);

CvRect bndRect = cvRect(0,0,0,0);

CvPoint pt1,pt2;

CvFont字体;


char * outFilename = argc == 2? argv [1]:E:\\outputMovie.avi;
CvVideoWriter * outputMovie = cvCreateVideoWriter(outFilename,
CV_FOURCC('F','L','V','I'),29.97,cvSize(720,576)

int prevX = 0;
int numPeople = 0;

char wow [65];

int avgX = 0;

bool first = true;

int closestToLeft = 0;
int closestToRight = 320;

for(;;)
{
colourImage = cvQueryFrame(input);

if(!colourImage)
{
break;
}
if(first)
{
difference = cvCloneImage(colourImage);
temp = cvCloneImage(colourImage);
cvConvertScale(colourImage,movingAverage,1.0,0.0);
first = false;
}
else

{
cvRunningAvg(colourImage,movingAverage,0.020,NULL);
}

cvConvertScale(movingAverage,temp,1.0,0.0);

cvAbsDiff(colourImage,temp,difference);

cvCvtColor(difference,greyImage,CV_RGB2GRAY);

cvThreshold(greyImage,greyImage,70,255,CV_THRESH_BINARY);

cvDilate(greyImage,greyImage,0,18);
cvErode(greyImage,greyImage,0,10);

CvMemStorage * storage = cvCreateMemStorage(0);
CvSeq * contour = 0;

cvFindContours(greyImage,storage,& contour,sizeof(CvContour),CV_RETR_CCOMP,CV_CHAIN_APPROX_SIMPLE);


for(; contour!= 0; contour = contour-> h_next)
{
bndRect = cvBoundingRect(contour,0);
pt1.x = bndRect.x;
pt1.y = bndRect.y;
pt2.x = bndRect.x + bndRect.width;
pt2.y = bndRect.y + bndRect.height;

avgX =(pt1.x + pt2.x)/ 2;

if(avgX> 90&& avgX< 250)
{
if(closestToLeft> = 88&& amp; closestToLeft< = 90)
{
if(avgX> prevX)
{
numPeople ++;
closestToLeft = 0;
}
}
else if(closestToRight> = 250&& closestToRight< = 252)
{
if(avgX< prevX)
{
numPeople ++;
closestToRight = 320;
}
}
cvRectangle(colourImage,pt1,pt2,CV_RGB(255,0,0),1);
}

if(avgX> closestToLeft& avgX< = 90)
{
closestToLeft = avgX;
}

if(avgX< closestToRight& avgX> = 250)
{
closestToRight = avgX;
}

prevX = avgX;
prevX = avgX;

}

cvInitFont(& font,CV_FONT_HERSHEY_SIMPLEX,0.8,0.8,0,2);
cvPutText(colourImage,_itoa(numPeople,wow,10),cvPoint(60,200),& font,cvScalar(0,0,300)
cvShowImage(My Window,colourImage);

cvWaitKey(10);
cvWriteFrame(outputMovie,colourImage);

}


cvReleaseImage(& temp);
cvReleaseImage(& difference);
cvReleaseImage(& greyImage);
cvReleaseImage(& movingAverage);
cvDestroyWindow(My Window);

cvReleaseCapture(& input);
cvReleaseVideoWriter(& outputMovie);


return 0;

}

正确编译它至少...它有一些运行时错误...我没有一个调试器来跟踪它现在...试试...我也试试.. ..


I am looking for implementation of human motion tracking, also discussed about in multiple moving object detection extracted from several video frames using differential analysis and lukas kanade optical method.

I found scientific papers and found that we have to use connected components filtering connected components for continuous motion tracking, but I do not understand how to go about this process. All I need are the skeletonization trajectory and the coordinates of human gait motion.

I am working in Opencv and C++, but the documentation in opencv for object detection won't suffice in my case. I am from medical background and need this is a part of a paediatrician project.

I found this code motion detection and was trying to execute it (don not know if it detects and tracks motion, yet). However, it returns these errors and I am perplexed for the errors are trivial and other comments mention that they are able to run this code. But I cannot mitigate these erros nor can I understand the reason for them occuring. I am using OpenCv2.3 and following are the errors

  1. Cannot open surce file stdafx.h
  2. warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.
  3. error C2086: 'CvSize imgSize' : redefinition
  4. error C2065: 'temp' : undeclared identifier
  5. error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
    • error C2365: 'cvReleaseImage' : redefinition; previous definition was 'function'1> c:\opencv2.3\opencv\build\include\opencv2\core\core_c.h(87) : see declaration of 'cvReleaseImage'
  6. error C2065: 'difference' : undeclared identifier
  7. error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
  8. error C2365: 'cvReleaseImage' : redefinition; previous definition was 'function'1> c:\opencv2.3\opencv\build\include\opencv2\core\core_c.h(87) : see declaration of 'cvReleaseImage'
  9. error C2065: 'greyImage' : undeclared identifier
  10. error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
  11. error C2365: 'cvReleaseImage' : redefinition; previous definition was 'function'
  12. \opencv2.3\opencv\build\include\opencv2\core\core_c.h(87) : see declaration of 'cvReleaseImage'error C2065: 'movingAverage' : undeclared identifier-error C4430: missing type specifier - int assumed. Note: C++ does not support default-int-error C2365: 'cvReleaseImage' : redefinition; previous definition was 'function'-1> c:\opencv2.3\opencv\build\include\opencv2\core\core_c.h(87) : see declaration of 'cvReleaseImage'-error C4430: missing type specifier - int assumed. Note: C++ does not support default-int-error C2365: 'cvDestroyWindow' : redefinition; previous definition was 'function'
  13. c:\opencv2.3\opencv\build\include\opencv2\highgui\highgui_c.h(136) : see declaration of 'cvDestroyWindow'
  14. error C2440: 'initializing' : cannot convert from 'const char [10]' to 'int'-1> There is no context in which this conversion is possible-error C2065: 'input' : undeclared identifier-error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
  15. error C2365: 'cvReleaseCapture' : redefinition; previous definition was 'function'-1> c:\opencv2.3\opencv\build\include\opencv2\highgui\highgui_c.h(311) : see declaration of 'cvReleaseCapture'-error C2065: 'outputMovie' : undeclared identifier
  16. error C4430: missing type specifier - int assumed. Note: C++ does not support default-int-error C2365: 'cvReleaseVideoWriter' : redefinition; previous definition was 'function'-1 c:\opencv2.3\opencv\build\include\opencv2\highgui\highgui_c.h(436) : see declaration of 'cvReleaseVideoWriter'-error C2059: syntax error : 'return'========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

CODE

// MotionDetection.cpp : Defines the entry point for the console application.
//


// Contourold.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"

#include "iostream"
#include "stdlib.h"

// OpenCV includes.
#include "cv.h"
#include "highgui.h"
#pragma comment(lib,"cv.lib")
#pragma comment(lib,"cxcore.lib")
#pragma comment(lib,"highgui.lib")

using namespace std;

int main(int argc, char* argv[])
{

//Create a new window.
cvNamedWindow("My Window", CV_WINDOW_AUTOSIZE);

//Create a new movie capture object.
CvCapture *input;

//Assign the movie to capture.
//inputMovie = cvCaptureFromAVI("vinoth.avi");

char *fileName = "E:\\highway.avi";
//char *fileName = "D:\\Profile\\AVI\\cardriving.wmv";
input = cvCaptureFromFile(fileName);
//if (!input)

//cout << "Can't open file" << fileName < ;



//Size of the image.
CvSize imgSize;
IplImage* frame = cvQueryFrame(input);
CvSize imgSize = cvGetSize(frame);

//Images to use in the program.
IplImage* greyImage = cvCreateImage( imgSize, IPL_DEPTH_8U, 1);
IplImage* colourImage;
IplImage* movingAverage = cvCreateImage( imgSize, IPL_DEPTH_32F, 3);
IplImage* difference;
IplImage* temp;
IplImage* motionHistory = cvCreateImage( imgSize, IPL_DEPTH_8U, 3);

//Rectangle to use to put around the people.
CvRect bndRect = cvRect(0,0,0,0);

//Points for the edges of the rectangle.
CvPoint pt1, pt2;

//Create a font object.
CvFont font;


//Create video to output to.
char* outFilename = argc==2 ? argv[1] : "E:\\outputMovie.avi";
CvVideoWriter* outputMovie = cvCreateVideoWriter(outFilename,
CV_FOURCC('F', 'L', 'V', 'I'), 29.97, cvSize(720, 576));

//Capture the movie frame by frame.
int prevX = 0;
int numPeople = 0;

//Buffer to save the number of people when converting the integer
//to a string.
char wow[65];

//The midpoint X position of the rectangle surrounding the moving objects.
int avgX = 0;

//Indicates whether this is the first time in the loop of frames.
bool first = true;

//Indicates the contour which was closest to the left boundary before the object
//entered the region between the buildings.
int closestToLeft = 0;
//Same as above, but for the right.
int closestToRight = 320;

//Keep processing frames...
for(;;)
{
//Get a frame from the input video.
colourImage = cvQueryFrame(input);

//If there are no more frames, jump out of the for.
if( !colourImage )
{
break;
}

//If this is the first time, initialize the images.
if(first)
{
difference = cvCloneImage(colourImage);
temp = cvCloneImage(colourImage);
cvConvertScale(colourImage, movingAverage, 1.0, 0.0);
first = false;
}
//else, make a running average of the motion.
else
{
cvRunningAvg(colourImage, movingAverage, 0.020, NULL);
}

//Convert the scale of the moving average.
cvConvertScale(movingAverage,temp, 1.0, 0.0);

//Minus the current frame from the moving average.
cvAbsDiff(colourImage,temp,difference);

//Convert the image to grayscale.
cvCvtColor(difference,greyImage,CV_RGB2GRAY);

//Convert the image to black and white.
cvThreshold(greyImage, greyImage, 70, 255, CV_THRESH_BINARY);

//Dilate and erode to get people blobs
cvDilate(greyImage, greyImage, 0, 18);
cvErode(greyImage, greyImage, 0, 10);

//Find the contours of the moving images in the frame.
CvMemStorage* storage = cvCreateMemStorage(0);
CvSeq* contour = 0;
cvFindContours( greyImage, storage, &contour, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );

//Process each moving contour in the current frame...
for( ; contour != 0; contour = contour->h_next )
{
//Get a bounding rectangle around the moving object.
bndRect = cvBoundingRect(contour, 0);

pt1.x = bndRect.x;
pt1.y = bndRect.y;
pt2.x = bndRect.x + bndRect.width;
pt2.y = bndRect.y + bndRect.height;

//Get an average X position of the moving contour.
avgX = (pt1.x + pt2.x) / 2;

//If the contour is within the edges of the building...
if(avgX > 90 && avgX < 250)
{
//If the the previous contour was within 2 of the left boundary...
if(closestToLeft >= 88 && closestToLeft <= 90)
{
//If the current X position is greater than the previous...
if(avgX > prevX)
{
//Increase the number of people.
numPeople++;

//Reset the closest object to the left indicator.
closestToLeft = 0;
}
}
//else if the previous contour was within 2 of the right boundary...
else if(closestToRight >= 250 && closestToRight <= 252)
{
//If the current X position is less than the previous...
if(avgX < prevX)
{
//Increase the number of people.
numPeople++;

//Reset the closest object to the right counter.
closestToRight = 320;
}
}

//Draw the bounding rectangle around the moving object.
cvRectangle(colourImage, pt1, pt2, CV_RGB(255,0,0), 1);
}

//If the current object is closer to the left boundary but still not across
//it, then change the closest to the left counter to this value.
if(avgX > closestToLeft && avgX <= 90)
{
closestToLeft = avgX;
}

//If the current object is closer to the right boundary but still not across
//it, then change the closest to the right counter to this value.
if(avgX < closestToRight && avgX >= 250)
{
closestToRight = avgX;
}

//Save the current X value to use as the previous in the next iteration.
prevX = avgX;
}
//Save the current X value to use as the previous in the next iteration.
prevX = avgX;
}


//Write the number of people counted at the top of the output frame.
cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.8, 0.8, 0, 2);
cvPutText(colourImage, _itoa(numPeople, wow, 10), cvPoint(60, 200), &font, cvScalar(0, 0, 300));

//Show the frame.
cvShowImage("My Window", colourImage);

//Wait for the user to see it.
cvWaitKey(10);

//Write the frame to the output movie.
cvWriteFrame(outputMovie, colourImage);
}

// Destroy the image, movies, and window.
cvReleaseImage(&temp);
cvReleaseImage(&difference);
cvReleaseImage(&greyImage);
cvReleaseImage(&movingAverage);
cvDestroyWindow("My Window");

cvReleaseCapture(&input);
cvReleaseVideoWriter(&outputMovie);


return 0;

}

  • Kindly help to resolve the errors and issues.
  • How to do motion (human) tracking which returns the coordinates of the trajectories probably by skeletonization method.

解决方案

1.) I presume you have directly copied the code from the website (correct me if I am wrong..). However since you are using OpenCV 2.3 so most of the APIs are in different modules. The following include you should have are...

#include <opencv/core/core.hpp>
#include <opencv/highgui/highgui.hpp>
#include <opencv/imgproc/imgproc.hpp>

and the respective libraries.

2.) For filtering connected components you can use cvblob library. I think the old Blob library provided by OpenCV was build using VC 6 so may be that required stdafx.h

3.) Go through the code slowly for some syntax and re-declaration error.

EDITTED CODE

#include <iostream>
#include "stdlib.h"


#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>

#include "cv.h"
#include "highgui.h"

using namespace std;
using namespace cv;

int main(int argc, char* argv[])
{
    cvNamedWindow("My Window", CV_WINDOW_AUTOSIZE);
    CvCapture *input;

    //char *fileName = "E:\\highway.avi";
    input = cvCaptureFromCAM(0);

    //input = cvCaptureFromFile(fileName);

    CvSize imgSize;
    IplImage* frame = cvQueryFrame(input);
    imgSize = cvGetSize(frame);

    IplImage* greyImage = cvCreateImage( imgSize, IPL_DEPTH_8U, 1);
    IplImage* colourImage;
    IplImage* movingAverage = cvCreateImage( imgSize, IPL_DEPTH_32F, 3);
    IplImage* difference;
    IplImage* temp;
    IplImage* motionHistory = cvCreateImage( imgSize, IPL_DEPTH_8U, 3);

    CvRect bndRect = cvRect(0,0,0,0);

    CvPoint pt1, pt2;

    CvFont font;


    char* outFilename = argc==2 ? argv[1] : "E:\\outputMovie.avi";
    CvVideoWriter* outputMovie = cvCreateVideoWriter(outFilename,
    CV_FOURCC('F', 'L', 'V', 'I'), 29.97, cvSize(720, 576));

    int prevX = 0;
    int numPeople = 0;

    char wow[65];

    int avgX = 0;

    bool first = true;

    int closestToLeft = 0;
    int closestToRight = 320;

    for(;;)
        {
        colourImage = cvQueryFrame(input);

        if( !colourImage )
            {
            break;
            }
        if(first)
            {
            difference = cvCloneImage(colourImage);
            temp = cvCloneImage(colourImage);
            cvConvertScale(colourImage, movingAverage, 1.0, 0.0);
            first = false;
            }
        else

            {
            cvRunningAvg(colourImage, movingAverage, 0.020, NULL);
            }

        cvConvertScale(movingAverage,temp, 1.0, 0.0);

        cvAbsDiff(colourImage,temp,difference);

        cvCvtColor(difference,greyImage,CV_RGB2GRAY);

        cvThreshold(greyImage, greyImage, 70, 255, CV_THRESH_BINARY);

        cvDilate(greyImage, greyImage, 0, 18);
        cvErode(greyImage, greyImage, 0, 10);

        CvMemStorage* storage = cvCreateMemStorage(0);
        CvSeq* contour = 0;

        cvFindContours( greyImage, storage, &contour, sizeof(CvContour), CV_RETR_CCOMP, CV_CHAIN_APPROX_SIMPLE );


        for( ; contour != 0; contour = contour->h_next )
        {
            bndRect = cvBoundingRect(contour, 0);
            pt1.x = bndRect.x;
            pt1.y = bndRect.y;
            pt2.x = bndRect.x + bndRect.width;
            pt2.y = bndRect.y + bndRect.height;

            avgX = (pt1.x + pt2.x) / 2;

        if(avgX > 90 && avgX < 250)
        {
            if(closestToLeft >= 88 && closestToLeft <= 90)
                {
                if(avgX > prevX)
                    {
                    numPeople++;
                    closestToLeft = 0;
                    }
                }
            else if(closestToRight >= 250 && closestToRight <= 252)
                {
                if(avgX < prevX)
                    {
                    numPeople++;
                    closestToRight = 320;
                    }
                }
            cvRectangle(colourImage, pt1, pt2, CV_RGB(255,0,0), 1);
         }

        if(avgX > closestToLeft && avgX <= 90)
            {
            closestToLeft = avgX;
            }

        if(avgX < closestToRight && avgX >= 250)
            {
            closestToRight = avgX;
            }

        prevX = avgX;
        prevX = avgX;

        }

        cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.8, 0.8, 0, 2);
        cvPutText(colourImage, _itoa(numPeople, wow, 10), cvPoint(60, 200), &font, cvScalar(0, 0, 300));
        cvShowImage("My Window", colourImage);

        cvWaitKey(10);
        cvWriteFrame(outputMovie, colourImage);

    }


    cvReleaseImage(&temp);
    cvReleaseImage(&difference);
    cvReleaseImage(&greyImage);
    cvReleaseImage(&movingAverage);
    cvDestroyWindow("My Window");

    cvReleaseCapture(&input);
    cvReleaseVideoWriter(&outputMovie);


return 0;

}

its compiling correctly atleast...its having some run time error...I dont have a debugger to track it right now...try it...i am also trying it..

这篇关于运动检测的opencv代码出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-16 08:06