0
waitKey(30) == 27
を使用してopencv imshow
を閉じようとしましたが、その理由がわかりません。キーボードのesc
キーを押すと閉じられるはずです。そこに何かがありませんか?waitKeyが宣言されていてもOpenCVがimshowを閉じることができません
これは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;
}
}
これはimshow
が含まれているdetectMotion
という名前の関数である:
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 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;
}
もimshow
を含まcaptureImg
機能です