使用opencv进行运动检测的错误
问题描述:
我是使用opencv进行运动检测的初学者。我已经运行了代码,需要人为检测的最终决定。问题是代码没有显示出最终决定。任何人都可以帮助并告诉我代码中的问题是什么?我可以解决这个问题吗?使用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_count
和frame_count
是整数,整数divison使用,否则,结果是始终为0。 (至少只要frame_count
大于detected_motion_count
)
另外通常你还想输出一个检测结果,如果比例是以上某个阈值,不完全等于该阈值。
+0
非常感谢..它解决了我的问题 – CBP
你期望输出什么,你会得到什么? – 2016-04-26 17:34:31
提供了printf的值(“帧数:%d \ n”,frame_count); printf(“检测到计数动作:%d \ n”,detected_motion_count); – piyushj
我的预期输出是当检测到的运动比总帧,“人体检测”wi显示超过45%时。 – CBP