c++ - 使用opencv进行运动检测的错误

标签 c++ opencv visual-studio-2013 motion-detection

我是使用 opencv 进行运动检测的初学者。我已经运行了代码,需要对人体检测做出最终决定。问题是代码没有显示为最终决定。任何人都可以帮助并告诉我代码中的问题是什么吗?由于这个问题,我可以有任何解决方案吗?

int main() 
{
    Mat frame;
    Mat resize_blur_Img;
    Mat fgMaskMOG;
    Mat fgMaskMOG2;
    Mat fgMaskGMG;
    Mat binaryImg;

Ptr<BackgroundSubtractor>pMOG;
pMOG = new BackgroundSubtractorMOG();
char filename[100] = "C:/Users/user/Desktop/Child Entrapment Prevention System/Motion Only/64.avi";

VideoCapture stream1(filename);
Mat element = getStructuringElement(MORPH_RECT, Size(7, 7), Point(3, 3)); 
int frame_count = 0;
int detected_motion_count=0;


while (true)
{
    if (!(stream1/*cap*/.read(frame)))
        break;

    frame_count++;

    resize(frame, resize_blur_Img, Size(frame.size().width, frame.size().height));

    //BLUR
    pMOG->operator()(resize_blur_Img, fgMaskMOG,-1);

    //MORPHOLOGY
    morphologyEx(fgMaskMOG, binaryImg, CV_MOP_CLOSE, element);

    //shadow delete (BINARY)
    threshold(binaryImg, binaryImg, 128, 255, CV_THRESH_BINARY); //(input array, output array, threshold value, max value to use threshold type, threshold type)

    imshow("Origin", resize_blur_Img);
    imshow("MOG", fgMaskMOG);

    //PIXEL
    int TotalNumberOfPixels = fgMaskMOG.rows*fgMaskMOG.cols;
    cout << "The number of pixels that are 255 is " << countNonZero(fgMaskMOG) << endl;


    if (countNonZero(fgMaskMOG) >= 1)
    {

        detected_motion_count++;
        printf("Motion DETECTED !\n\n", countNonZero(fgMaskMOG));
    }
    else
    {
        printf("Motion NOT DETECTED!\n\n", countNonZero(fgMaskMOG));
    }

}

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

if ((float)(detected_motion_count / frame_count) ==0.45)
{
    printf("\n\n------------------------FINAL DECISION------------------------------");
    printf("HUMAN DETECTED \n\n");
}
getchar();

}

最佳答案

你应该修改这一行

if ((float)(detected_motion_count / frame_count) ==0.45)

看起来像这样:

if (((float)detected_motion_count / (float)frame_count) >=0.45)

由于 detected_motion_countframe_count 是整数,否则使用整数除法,结果始终为 0。(至少与 frame_count 一样长大于 detected_motion_count)

如果比率高于某个阈值,而不是完全等于阈值,通常您还希望输出检测结果。

关于c++ - 使用opencv进行运动检测的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36871876/

相关文章:

python - OpenCV 和 Python : arithmetic operations between scalar and all pixels and channels

c++ - 使用 Cmake 和 Visual Studio 2013 Unresolved external 问题

c++ - 如何设置 Visual Studio 来调试 DLL ("Unable to start program error")

intellisense - Visual Studio 2013 智能感知错误

c++ - 将 0 传递给共享指针,删除器作为第一个参数

c++ - 字符串到字符数组不起作用

c++ - 线性链式工厂和 move 语义

c++ - 查找连续出现次数最多的数字的算法 - C++

python - 属性错误 : 'module' object has no attribute 'ORB'

c++ - 受光照影响的边缘检测