簡體   English   中英

即使已經聲明了waitKey,OpenCV也無法關閉imshow

[英]OpenCV fail to close imshow even though waitKey had been declared

我嘗試使用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;
    }

}

這是名為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;
}

在您的主要功能中,將檢查更改為:

int ch = waitKey(); //or waitKey(0)
if (ch == 27)
{
    cap.release();
    destroyAllWindows();
    cout<<"turning off"<<endl;
    break;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM