即使已经声明了waitKey,OpenCV也无法关闭imshow

时间:2017-06-24 16:02:21

标签: c++ opencv

我尝试使用imshow关闭opencv waitKey(30) == 27,但我不知道为什么不能这样做。当我按下键盘上的esc键时它应该关闭。我在那里错过了一些东西吗?

这是main函数中的循环:

while (1)
{
    frame = cam1.captureImg();
    number_of_changes=cam1.detectMotion();

    if(number_of_changes>=there_is_motion)
    {
        cout<<"Motion detected!!"<<endl;
        frameSequence++;

        if(frameSequence > lengthThreshold)
        {
            saveImg = 1;
        }
    }   
    else
    {
        frameSequence=0;
    }

    if (saveImg == 1)
    {
        saveImg = 0;

        frameSequence=0;
        cout<<"saving Img"<<endl;
        cam1.saveImg(frame);        
    }

    if (waitKey(30) == 27)
    {
        cap.release();
        destroyAllWindows();
        cout<<"turning off"<<endl;
        break;
    }

}

这是名为detectMotion的函数,其中包含imshow

int detectMotion (void)
{
    Scalar mean, stddev;
    meanStdDev(motion,mean,stddev);

        if(stddev[0] < max_deviation)
    {
        number_of_changes = 0;
        min_x = motion.cols, max_x=0;
        min_y = motion.rows, max_y=0;

        for(int j=y_start; j < y_stop; j+=2)  {     
            for(int i=x_start; i < x_stop; i+=2)  { 

                if(static_cast<int>(motion.at<uchar>(j,i)) == 255)
                {
                    number_of_changes++;
                    if(min_x>i) min_x = i;
                    if(max_x<i) max_x = i;
                    if(min_y>j) min_y = j;
                    if(max_y<j) max_y = j;
                }
            }
        }
        if(number_of_changes) 
        {

            if(min_x-10 > 0) min_x -= 10;
            if(min_y-10 > 0) min_y -= 10;
            if(max_x+10 < matriximage.cols-1) max_x += 10;
            if(max_y+10 < matriximage.rows-1) max_y += 10;

            Point x(min_x,min_y);
            Point y(max_x,max_y);
            Rect rect(x,y);
            rectangle(result,rect,Scalar(0,255,255),1,4);
            imshow("Motion Indicator", result);
        }
        return number_of_changes;   
    }       

    return 0;

}

以下是captureImg函数,其中还包含imshow

Mat captureImg(void)
{

        cap>>matriximage;
        result=matriximage;
        cvtColor(matriximage,matriximage,CV_RGB2GRAY);  //grayscale

        // Calculate differences between 3 consecutive frames...
        diffImg(prev_mframe, current_mframe, next_mframe);
        imshow("Motion Indicator", result);     // Display the current frame...

        //rellocate image in right order
        current_mframe.copyTo(prev_mframe);
        next_mframe.copyTo(current_mframe);
        matriximage.copyTo(next_mframe);

        motion = diffImg(prev_mframe, current_mframe, next_mframe);
        return result;
}

1 个答案:

答案 0 :(得分:0)

在主要功能中,将支票更改为:

print(p.to_dot())