所以我的代码使用我的手捕获一个特定动作的帧,唯一的问题是我需要等待我的手在帧存储之前移出。
我尝试过使用Sleep(3000),所有视频输出都会冻结3秒钟,但它会在视频输出冻结之前捕获帧,而不是在它们之后,所以基本上当我的手还在路上时。
有什么方法可以让capture >> saveImage;
停止运行几秒钟?
//bools for gestures
bool bSavePic = false;
bool bSingleFrame = true;
int main(int argc, char* argv[])
{
//Following for measuring FPS
time_t startFPS, endFPS;
double fps;
int counter = 0;
double sec;
time(&startFPS);
//if we would like to calibrate our filter values, set to true.
bool calibrationMode = true;
//Matrix to store each frame of the webcam feed
Mat cameraFeed;
Mat threshold;
Mat HSV;
//Initalise count at 0 for image saving
int c = 0;
if(calibrationMode)
{
//create slider bars for HSV filtering
createTrackbars();
cout << "The program is currently in calibration mode" << endl;
cout << "this is so that HSV values can be recorded for" << endl;
cout << "each marker." << endl;
}//if calibrationMode
//video capture object to acquire webcam feed
VideoCapture capture;
//open capture object at location zero (default location for webcam)
capture.open(2);
//When using video files.
//capture.open("My Recording1.avi");
//set height and width of capture frame
capture.set(CV_CAP_PROP_FRAME_WIDTH,FRAME_WIDTH);
capture.set(CV_CAP_PROP_FRAME_HEIGHT,FRAME_HEIGHT);
//start an infinite loop where webcam feed is copied to cameraFeed matrix
//all of our operations will be performed within this loop
while(capture.isOpened())
{
//store image to matrix
capture.read(cameraFeed);
//After each frame in captured measure FPS
time(&endFPS);
++counter;
sec = difftime (endFPS, startFPS);
fps = counter / sec;
printf("FPS = %.2f\n", fps);
//convert frame from BGR to HSV colorspace
cvtColor(cameraFeed,HSV,COLOR_BGR2HSV);
if(calibrationMode==true)
{
//if in calibration mode, we track objects based on the HSV slider values.
inRange(HSV,Scalar(H_MIN,S_MIN,V_MIN),Scalar(H_MAX,S_MAX,V_MAX),threshold);
morphOps(threshold);
//Show thresholded image
imshow(windowName2,threshold);
trackFilteredObject(threshold,HSV,cameraFeed);
}else{
}
//Following function checks if distance is <100
//Uses another one time bool flag, bSingleFrame, which is initially set to true
//Checks if bSingleFrame && bSavePic are true, if so captures a frame
//bSingleFrame is then changed to false if the distance is still <100
//Once the distance is >100, bSingleFrame is changed back to true, and another frame can be captured
//Means only one frame is captured when the conditions are met
//If distance<100
if (bSavePic == true)
{
if (bSingleFrame == true)
{
//Display that the frame captured is being saved
putText(cameraFeed,"Saving Image",Point(50,70),1,1,Scalar(0,0,255),1);
//*****SLEEP FUNCTION*****
Sleep(3000);
//Matrix to store image for saving
Mat saveImage;
capture >> saveImage;
//string stream initialised through each passing of while loop
stringstream ssFileName;
//Directory to save image & File name based on frame captured 0++
ssFileName << "gestureimages/Image-" << c << ".png";
ssFileName >> sFileName;
//If file exists function ***NEEDS MODIFIED***
if(!fileExists == 0)
//{
// //need file name search function, find number after image
// //start c at this number
// c++;
// stringstream ssFileName; //string stream initialised through each passing of while loop
// ssFileName << "Image-" << c << ".jpg"; //File name based on frame captured 0++
// ssFileName >> sFileName;
//}
//check if frame captured is stored in matrix
if(!saveImage.empty())
{
cout << "Frame captured." << endl;
//Save frame
imwrite(sFileName.c_str(), saveImage);
saveImage.release();
c++;
//If distance is still <100 switch bSingleFrame
if (bSavePic == true)
{
bSingleFrame = false;
}
Mat readImage;
//Load saved frame form file
readImage = imread(sFileName.c_str(),CV_LOAD_IMAGE_COLOR);
//show captured frame in new window
imshow(windowName4, readImage);
}else{
cout << "Error, could not capture frame to save." << endl;
}//if(!saveImage.empty())
}//bSingleFrame
}//ifbSavePic
if (bSavePic == false)
{
//if distance back >100 can capture another frame
bSingleFrame = true;
}
//Display webcam output
imshow(windowName,cameraFeed);
//Display thresholded output
imshow(windowName2,threshold);
//delay 30ms so that screen can refresh.
//image will not appear without this waitKey() command
waitKey(30);
}//while
return 0;
}//main
这只是我的主要功能的一小部分,我的完整代码可用here
答案 0 :(得分:1)
首先尝试使用sleep
并手动拨打grab。这应确保仅在一定时间后捕获图像。
如果这应该失败,请尝试阅读两张图片,只保留第二张图片。 (这不好,但无论如何你都不是时间依赖)。
如果也失败,请检查您的睡眠功能是否有异常行为;)