错误在OpenCV的代码用于运动检测(Error in opencv code for motion

2019-08-06 17:34发布

我寻找执行人体运动跟踪,也讨论了在多个移动体检测利用差分分析和卢卡斯Kanade光方法从几个视频帧提取。

我发现的科学论文,发现我们不得不使用连接成分过滤连接部件的连续运动跟踪,但我不知道如何去了解这个过程。 所有我需要的是骨架轨迹与人的步态运动的坐标。

我在OpenCV中和C ++的工作,但对物体检测OpenCV的文档不会在我的情况就足够了。 我是从医学背景和需要,这是一个儿科医生项目的一部分。

我发现这个代码, 移动侦测 ,并试图执行它(不要不知道,如果它检测和跟踪运动,还)。 然而,它会返回这些错误,我感到困惑的错误是琐碎和其他意见提到,他们都能够运行该代码。 但我不能缓解这些错误回报我也不能明白其中的道理对他们存在的。 我使用OpenCv2.3和以下是错误

  1. 无法打开surce stdafx.h文件
  2. 警告C4996:“的fopen”:此函数或变量可能是不安全的。 考虑使用fopen_s代替。 要禁用弃用,使用_CRT_SECURE_NO_WARNINGS。 详细信息请参见在线帮助。
  3. 错误C2086: 'CvSize imgSize':重新定义
  4. 错误C2065:温度:未声明的标识符
  5. 错误C4430:缺少类型说明符 - 假定为int。 注意:C ++不支持默认int
    • 错误C2365: 'cvReleaseImage':重新定义; 以前的定义是 '功能' 1> C:\ opencv2.3 \的OpenCV \建立\包括\ opencv2 \芯\ core_c.h(87):见 'cvReleaseImage' 的声明
  6. 错误C2065:差:未声明的标识符
  7. 错误C4430:缺少类型说明符 - 假定为int。 注意:C ++不支持默认int
  8. 错误C2365: 'cvReleaseImage':重新定义; 以前的定义是 '功能' 1> C:\ opencv2.3 \的OpenCV \建立\包括\ opencv2 \芯\ core_c.h(87):见 'cvReleaseImage' 的声明
  9. 错误C2065:greyImage':未声明的标识符
  10. 错误C4430:缺少类型说明符 - 假定为int。 注意:C ++不支持默认int
  11. 错误C2365: 'cvReleaseImage':重新定义; 以前的定义是“功能”
  12. \ opencv2.3 \的OpenCV \建立\包括\ opencv2 \芯\ core_c.h(87):见 'cvReleaseImage' 错误C2065声明: 'movingAverage':未声明的标识符C4430 -error:缺少类型说明 - 假定为int。 注意:C ++不支持默认int - 错误C2365: 'cvReleaseImage':重新定义; 以前的定义是 '功能' -1> C:\ opencv2.3 \的OpenCV \建立\包括\ opencv2 \芯\ core_c.h(87):见C4430 -error 'cvReleaseImage' 的声明:缺少类型说明 - 假定为int。 注意:C ++不支持默认int - 错误C2365: 'cvDestroyWindow':重新定义; 以前的定义是“功能”
  13. C:\ opencv2.3 \的OpenCV \建立\包括\ opencv2 \ highgui \ highgui_c.h(136):见 'cvDestroyWindow' 的声明
  14. 错误C2440:初始化:不能从“为const char [10]”到“INT”转换-1>没有上下文,其中该转换是可能的-error C2065:输入:未声明的标识符-error C4430:缺少类型符 - 假定为int。 注意:C ++不支持默认int
  15. 错误C2365: 'cvReleaseCapture':重新定义; 以前的定义是 '功能' -1> C:\ opencv2.3 \的OpenCV \建立\包括\ opencv2 \ highgui \ highgui_c.h(311):见 'cvReleaseCapture' -error C2065的声明: 'outputMovie':未声明的标识符
  16. 错误C4430:缺少类型说明符 - 假定为int。 注意:C ++不支持默认int - 错误C2365: 'cvReleaseVideoWriter':重新定义; 以前的定义是 '功能' -1 C:\ opencv2.3 \ OpenCV的\建设\包括\ opencv2 \ highgui \ highgui_c.h(436):看 'cvReleaseVideoWriter' 的声明 - 错误C2059:语法错误: '回报' == ========编译:0成功,1失败,0上最新,0已跳过==========

// 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;

}
  • 请帮助解决这些错误和问题。
  • 如何做运动(人)追踪其可能是通过骨架方法返回轨迹的坐标。

Answer 1:

1)我想您已经直接复制从网站(纠正我,如果我错了代码..)。 但是,由于您使用的OpenCV 2.3所以大部分的API都在不同的模块。 下面包括你应该是...

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

和相应的库。

2.)用于过滤连接的组件可以使用cvblob库 。 我认为的OpenCV提供的旧斑点库是用VC 6,可能是需要积累stdafx.h

3)通过代码去慢慢对一些语法和重新申报错误。

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;

}

其编译正确ATLEAST ......它有一些运行时错误...我没有一个调试器,现在对其进行跟踪......试试吧...我也在努力吧..



Answer 2:

我尝试编译的代码,我可以当场约3-4错误为止。

  1. 你并不需要#include "stdafx.h"
  2. 您重新声明imgSize在您使用行cvGetSize(frame)
  3. 最后的支架不匹配。
  4. 您可以检查功能_itoa()因为它是不是在代码中声明。


文章来源: Error in opencv code for motion detection