【opencv学习之四十二】简单运动检测

视频运动检测基本思想是进行每一帧对比,检测不同然后得出是否运动,一般分为两种,背景法和差帧法;

背景法是:将一幅图作为背景,让后和每一帧对比;缺点是一开始存入的背景可能随光照变法而造成错误,但是可以用在光照环境稳定的地方,优点是可以检测之前背景没有的景象;

差帧法是:将前一帧和后一帧进行对比;缺点是无法对运动后突然又静止的景象进行识别,优点是光照不影响;

实例背景法:

///运动检测,摄像头检测,背景法
Mat MoveDetectBack(Mat background,Mat frame);//声明运动检测函数
void imgMoveDetectionBack()
{
    /*VideoCapture结构体,保存图像信息,open()参数为int index(0为默认摄像头),读入摄像头视频,
               open()参数为路径,读入视频文件*/
    VideoCapture cap(0); // 打开摄像头0
    if (!cap.isOpened())  // 异常处理
    {
        QMessageBox mesg;
        mesg.about(NULL,"提示","摄像头打开失败");
        waitKey(1000);
    }
    Mat frame;//存储帧
    Mat background;//存储背景图像
    Mat result;//存储结果图像
    
    //先存储背景
    bool firstF= cap.read(frame); // 先获取一帧
    //异常处理
    if (!firstF) { QMessageBox mesg;
        mesg.about(NULL,"提示","Cannot read a frame from video stream"); }
    else
    {background = frame.clone();}//存储背景
    
    //循环检测视频
    while (1)
    {
        bool bSuccess = cap.read(frame); // 读取
        if (!bSuccess) //异常处理
        {
            QMessageBox mesg;
            mesg.about(NULL,"提示","Cannot read a frame from video stream");
            break;
        }
        ///
        if (frame.empty())//对帧进行异常检测
        {
            cout << "frame is empty!" << endl;
            break;
        } 
        //将背景和每一帧做差
        result = MoveDetectBack(background, frame);//调用MoveDetect()进行运动物体检测,返回值存入result
        imshow("result", result);      
        //
        if (waitKey(30) == 27) //按键退出
        {
            QMessageBox mesg;
            mesg.about(NULL,"信息","退出摄像");
            break;
        }
    }
    waitKey(10);
    cap.release();
}

//检测函数
Mat MoveDetectBack(Mat background, Mat frame)
{
    Mat result = frame.clone();
    //1.将background和frame转为灰度图
    Mat gray1, gray2;
    cvtColor(background, gray1, CV_BGR2GRAY);
    cvtColor(frame, gray2, CV_BGR2GRAY);
    //2.将background和frame做差
    Mat m_different;
    absdiff(gray1, gray2, m_different);
    imshow("m_different", m_different);
    //3.对差值图diff_thresh进行阈值化处理
    Mat Dif_Thresh;
    threshold(m_different, Dif_Thresh, 50, 255, CV_THRESH_BINARY);
    imshow("Dif_Thresh",Dif_Thresh);
    //4.腐蚀
    Mat kernel_erode = getStructuringElement(MORPH_RECT, Size(3, 3));
    Mat kernel_dilate = getStructuringElement(MORPH_RECT, Size(15, 15));
    erode(Dif_Thresh, Dif_Thresh, kernel_erode);
    imshow("erode", Dif_Thresh);
    //5.膨胀
    dilate(Dif_Thresh, Dif_Thresh, kernel_dilate);
    imshow("dilate", Dif_Thresh);
    //6.查找轮廓并绘制轮廓
    vector> contours;
    findContours(Dif_Thresh, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
    drawContours(result, contours, -1, Scalar(0, 0, 255), 2);//在result上绘制轮廓
    //7.查找正外接矩形
    vector boundRect(contours.size());
    for (int i = 0; i < contours.size(); i++)
    {
        boundRect[i] = boundingRect(contours[i]);
        rectangle(result, boundRect[i], Scalar(0, 255, 0), 2);//在result上绘制正外接矩形
    }
    return result;//返回result
}

效果:

【opencv学习之四十二】简单运动检测_第1张图片

【opencv学习之四十二】简单运动检测_第2张图片

【opencv学习之四十二】简单运动检测_第3张图片

【opencv学习之四十二】简单运动检测_第4张图片

【opencv学习之四十二】简单运动检测_第5张图片

差帧法:

///运动检测3,摄像头检测,差帧法
Mat MoveDetect3(Mat background,Mat frame);//声明运动检测函数
void imgMoveDetection3()
{
    /*VideoCapture结构体,保存图像信息,open()参数为int index(0为默认摄像头),读入摄像头视频,
               open()参数为路径,读入视频文件*/
    VideoCapture cap(0); // 打开摄像头0
    if (!cap.isOpened())  // 异常处理
    {
        QMessageBox mesg;
        mesg.about(NULL,"提示","摄像头打开失败");
        waitKey(1000);
    }
    Mat frame;//存储帧
    Mat temp;//存储前一帧图像
    Mat result;//存储结果图像
    while (1)
    {
        bool bSuccess = cap.read(frame); // 获取一帧
        if (!bSuccess) //异常处理
        {
            QMessageBox mesg;
            mesg.about(NULL,"提示","Cannot read a frame from video stream");
            break;
        }
        ///
        if (frame.empty())//对帧进行异常检测
        {
            cout << "frame is empty!" << endl;
            break;
        }
        if ( temp.empty())//如果temp为空则为第一帧
        {
            result = MoveDetect3(frame, frame);//调用MoveDetect()进行运动物体检测,返回值存入result
        }
        else//若不是第一帧(temp有值了)
        {
            result = MoveDetect3(temp, frame);//调用MoveDetect()进行运动物体检测,返回值存入result
        }
        imshow("result", result);
        temp = frame.clone();
        //
        if (waitKey(30) == 27) //按键退出
        {
            QMessageBox mesg;
            mesg.about(NULL,"信息","退出摄像");
            break;
        }
    }
    waitKey(10);
    cap.release();
}

//检测函数
Mat MoveDetect3(Mat background, Mat frame)
{
    Mat result = frame.clone();
    //1.将background和frame转为灰度图
    Mat gray1, gray2;
    cvtColor(background, gray1, CV_BGR2GRAY);
    cvtColor(frame, gray2, CV_BGR2GRAY);
    //2.将background和frame做差
    Mat m_different;
    absdiff(gray1, gray2, m_different);
    imshow("m_different", m_different);
    //3.对差值图diff_thresh进行阈值化处理
    Mat Dif_Thresh;
    threshold(m_different, Dif_Thresh, 50, 255, CV_THRESH_BINARY);
    imshow("Dif_Thresh",Dif_Thresh);
    //4.腐蚀
    Mat kernel_erode = getStructuringElement(MORPH_RECT, Size(3, 3));
    Mat kernel_dilate = getStructuringElement(MORPH_RECT, Size(15, 15));
    erode(Dif_Thresh, Dif_Thresh, kernel_erode);
    imshow("erode", Dif_Thresh);
    //5.膨胀
    dilate(Dif_Thresh, Dif_Thresh, kernel_dilate);
    imshow("dilate", Dif_Thresh);
    //6.查找轮廓并绘制轮廓
    vector> contours;
    findContours(Dif_Thresh, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
    drawContours(result, contours, -1, Scalar(0, 0, 255), 2);//在result上绘制轮廓
    //7.查找正外接矩形
    vector boundRect(contours.size());
    for (int i = 0; i < contours.size(); i++)
    {
        boundRect[i] = boundingRect(contours[i]);
        rectangle(result, boundRect[i], Scalar(0, 255, 0), 2);//在result上绘制正外接矩形
    }
    return result;//返回result
}

效果:

【opencv学习之四十二】简单运动检测_第6张图片

【opencv学习之四十二】简单运动检测_第7张图片

【opencv学习之四十二】简单运动检测_第8张图片

【opencv学习之四十二】简单运动检测_第9张图片

【opencv学习之四十二】简单运动检测_第10张图片


你可能感兴趣的:(opencv)