Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 如何实时进行代码到最终决策?_C++_Opencv - Fatal编程技术网

C++ 如何实时进行代码到最终决策?

C++ 如何实时进行代码到最终决策?,c++,opencv,C++,Opencv,我已经开发了代码。问题是,当我将其转换为实时时,它不会进入最终解决方案。我应该添加什么以使代码继续到最终解决方案?我希望它读取代码,并给出一帧一帧的结果,仅持续30秒,然后它将进入最终决定。有人能帮我吗 int main(int argc,char** argv) { VideoCapture capture(0); Mat frame,resize_blur_Img,fgmaskMOG,binaryImg; Ptr<BackgroundSubtractor>

我已经开发了代码。问题是,当我将其转换为实时时,它不会进入最终解决方案。我应该添加什么以使代码继续到最终解决方案?我希望它读取代码,并给出一帧一帧的结果,仅持续30秒,然后它将进入最终决定。有人能帮我吗

int main(int argc,char** argv)
{
    VideoCapture capture(0);
    Mat frame,resize_blur_Img,fgmaskMOG,binaryImg;
    Ptr<BackgroundSubtractor>pMOG;
    pMOG = new BackgroundSubtractorMOG();
    int frame_count=0;
    int detected_face_count = 0;
    int detected_motion_count = 0;
    CascadeClassifier cascade;
    if (!cascade.load("C:/opencv2410/sources/data/haarcascades/haarcascade_frontalface_alt.xml"))
    {
        cerr << "ERROR: Could not load classifier cascade" << endl;
        return -1;
    }
    Mat element = getStructuringElement(MORPH_RECT, Size(7, 7), Point(3, 3));

    while (true)
    {
        //MOTION DETECTION
        if (!(capture.read(frame)))
            break;
        frame_count++;
        resize(frame, resize_blur_Img, Size(frame.size().width, frame.size().height));
        pMOG->operator()(resize_blur_Img, fgmaskMOG, -1);
        threshold(binaryImg, binaryImg, 128, 255, CV_THRESH_BINARY);
        int TotalNumberOfPixels = fgmaskMOG.rows*fgmaskMOG.cols;

        //FACE DETECTION
        capture >> frame;
        if (frame.empty())
            break;

        imshow("original", frame);
        vector<Rect>faces;
        cascade.detectMultiScale(frame, faces, 1.1, 4, 0, Size(40, 50));//(const Mat& image, vector <Rect>& objects, double scaleFactor, int minNeighbors,int flags, Size minSize,Size maxSize)

        for (int i = 0; i < faces.size(); i++)
        {
            rectangle(frame, faces[i], Scalar(0, 0, 255));
        }

        imshow("MOTION DETECTION", fgmaskMOG);
        imshow("FACE DETECTION", frame);
        char c = waitKey(10);

        printf("count of pixels: %d \n", countNonZero(fgmaskMOG));
        printf("%d Face Found !\n", faces.size());

        if (faces.size()>0 &&countNonZero(fgmaskMOG)>1)
        {
            detected_face_count++;
            detected_motion_count++;
            printf("FACE AND MOTION DETECTED !\n\n");
        }
        else if (faces.size()>0 && countNonZero(fgmaskMOG)==0)
        {
            detected_face_count++;
            printf("ONLY FACE DETECTED !\n\n");
        }
        else if (faces.size() == 0 && countNonZero(fgmaskMOG) > 1)
        {
            detected_motion_count++;
            printf("ONLY MOTION DETECTED !\n\n");
        }
        else
        {
            printf("NOTHING DETECTED !\n\n");
        }

//I want it to stop until here after 30 second and show the final decision according to rule below

    }

    printf("count of frames: %d \n", frame_count);
    printf("count of frames has detected face: %d \n", detected_face_count);
    printf("count of motion detected: %d \n", detected_motion_count);
    printf("Face Found %d percent of frames ! \n", (int)(100 * detected_face_count / frame_count));
    printf("Motion Found %d percent of frames ! \n", (int)(100 * detected_motion_count / frame_count));

    //FINAL DECISION FROM ALL THE FRAME
    if (((float)(detected_face_count / frame_count) > 0.49) && (((float)detected_motion_count / (float)frame_count)>0.19))
    {
        printf("HUMAN DETECTED FROM BOTH DETECTION ! \n\n");
    }
    else if (((float)detected_motion_count / (float)frame_count)>0.19)
    {
        printf("POSSIBLE HUMAN DETECTED FROM MOTION DETECTION ONLY !\n\n");
    }
    else if ((float)(detected_face_count / frame_count) > 0.49)
    {
        printf("POSSIBLE HUMAN DETECTED FROM FACE DETECTION ONLY !\n\n");
    }
    else
    {
        printf("HUMAN NOT DETECTED \n\n");
    }

    getch();
}
int main(int argc,char**argv)
{
视频捕获(0);
Mat frame,resize_blur_Img,fgmaskMOG,binarymg;
PtrpMOG;
pMOG=新背景减去ormog();
整数帧计数=0;
检测到的int_面_计数=0;
检测到的int\u运动\u计数=0;
级联分类器级联;
如果(!cascade.load(“C:/opencv2410/sources/data/haarcascade/haarcascade\u frontalface\u alt.xml”))
{
cerr>frame;
if(frame.empty())
打破
imshow(“原创”,框架);
矢量面;
级联检测多尺度(帧、面、1.1、4、0、大小(40、50));/(常数矩阵和图像、向量和对象、双尺度因子、int minNeighbors、int标志、Size minSize、Size maxSize)
对于(int i=0;i0&&countNonZero(fgmaskMOG)>1)
{
检测到_面部_计数++;
检测到_运动_计数++;
printf(“检测到面部和运动!\n\n”);
}
else if(faces.size()>0&&countNonZero(fgmaskMOG)==0)
{
检测到_面部_计数++;
printf(“仅检测到面!\n\n”);
}
else if(faces.size()==0&&countNonZero(fgmaskMOG)>1)
{
检测到_运动_计数++;
printf(“仅检测到运动!\n\n”);
}
其他的
{
printf(“未检测到任何内容!\n\n”);
}
//我希望它在30秒后停止,并根据下面的规则显示最终决定
}
printf(“帧计数:%d\n”,帧计数);
printf(“检测到面的帧数:%d\n”,检测到的面数);
printf(“检测到的运动计数:%d\n”,检测到的运动计数);
printf(“找到的面占帧的%d!\n”,(int)(100*检测到的面数/帧数));
printf(“运动找到帧的%d%!\n”,(int)(100*检测到的运动计数/帧计数));
//所有框架的最终决定
如果((浮动)(检测到的面部计数/帧计数>0.49)和((浮动)检测到的运动计数/(浮动)帧计数>0.19))
{
printf(“从两个检测中检测到人类!\n\n”);
}
如果((浮动)检测到运动计数/(浮动)帧计数>0.19,则为其他情况)
{
printf(“仅从运动检测中检测到可能的人体!\n\n”);
}
否则((浮动)(检测到的面计数/帧计数)>0.49)
{
printf(“仅从人脸检测中检测到可能的人类!\n\n”);
}
其他的
{
printf(“未检测到人\n\n”);
}
getch();
}

如果您愿意加入boost,有一种准确的、独立于平台的方法可以做到这一点:

#include <boost/date_time/posix_time/posix_time.hpp>
namespace posixtime = boost::posix_time;
...
posixtime::ptime startTime = posixtime::microsec_clock::local_time();
while ((posixtime::microsec_clock::local_time() - start).total_seconds() < 30)
{
    ...
#包括
命名空间posixtime=boost::posix_-time;
...
posixtime::ptime startTime=posixtime::microsec_clock::local_time();
while((posixtime::microsec_clock::local_time()-start)。总秒数()<30)
{
...

详细信息请参见。当然,有无数种方法可以做到这一点,但我觉得这很方便。

你说的“实时化”是什么意思?从实时摄像机而不是录制的视频中获取视频?你能发布一个代码的最简例吗?我已经编辑了上面的代码。这是更简单的代码吗?我无法运行代码,因为我不知道保存文件的路径。如果你有C++11,你应该有具有相同功能的库