Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/153.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

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++ 即使已声明waitKey,OpenCV也无法关闭imshow_C++_Opencv - Fatal编程技术网

C++ 即使已声明waitKey,OpenCV也无法关闭imshow

C++ 即使已声明waitKey,OpenCV也无法关闭imshow,c++,opencv,C++,Opencv,我尝试使用waitKey(30)==27关闭opencvimshow,但我不知道为什么它不能这样做。当我按下键盘上的esc键时,它应该关闭。我是不是错过了什么 这是我在main函数中的循环: while (1) { frame = cam1.captureImg(); number_of_changes=cam1.detectMotion(); if(number_of_changes>=there_is_motion) { cout<

我尝试使用
waitKey(30)==27关闭opencv
imshow
,但我不知道为什么它不能这样做。当我按下键盘上的
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;
    }

}
下面是
captureImg
函数,它还包含
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;

}
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;
}

在主功能中,将检查更改为:

int ch = waitKey(); //or waitKey(0)
if (ch == 27)
{
    cap.release();
    destroyAllWindows();
    cout<<"turning off"<<endl;
    break;
}
int ch=waitKey()//或等待键(0)
如果(ch==27)
{
释放帽();
销毁所有窗口();
库特