dimanche 27 avril 2014

c ++ - Capture 2 cadres non consécutifs à l'aide d'une webcam dans OpenCV - Stack Overflow


These are the steps I want this project to follow using OpenCV:


1) Capture one frame when I press the 1 key.


2) Move the webcam


3) Capture a second frame when I press the 2 key.


4) Show both images.


Here is the code I'm working with:


int main(int, char**){
VideoCapture cap(1);
Mat img1, img2;
int input;
namedWindow("Imagen 1",CV_WINDOW_AUTOSIZE);
namedWindow("Imagen 2",CV_WINDOW_AUTOSIZE);

for(;;){
input = cvWaitKey(40);
if((char) input ==27)
break;
if ((char) input == 49){
cap >> img1;
imshow("Imagen 1",img1);
}
if ((char) input == 50){
cap >> img2;
imshow("Imagen 2",img2);
}
}
return 0;
}

However, when I do run this I get the same image in both windows. Can anyone explain why this is happening? What can I do to make it work the way I have explained?




If You are working on linux, then You will have to empty the buffer from capturing device. I do it by running a separate thread that reads the frames and remembers only the last one. When I want to take a frame for further processing, I clone the one which is now remembered. But, in Your case it might be a slight overkill.


Also, You might like to do something like this instead of Your current main loop:


cv::Mat temp,img1,img2;
cv::VideoCapture cap(1);
char control=' ';
cv::namedWindow("current",CV_AUTOSIZE);
cv::namedWindow("img1",CV_AUTOSIZE);
cv::namedWindow("img2",CV_AUTOSIZE);
do{
if(49 == control){
img1=temp.clone();
cv::imshow("img1",img1);
}else if(50 == control){
img2=temp.clone();
cv::imshow("img2",img2);
}
cap>>tmp; //emptying buffer all the time
cv::imshow("current",tmp);
control=cv::waitKey(40);//if You are faster than captures fps
}while(27 != control);



You need to put the cap() call inside the loop - otherwise you only do one capture




Instead of using cap >> img1 and cap >> img2I added a Mat variable called "captura" to store the current frame and used img1 = captura.clone() and img2 = captura.clone() respectively and now it's working.



These are the steps I want this project to follow using OpenCV:


1) Capture one frame when I press the 1 key.


2) Move the webcam


3) Capture a second frame when I press the 2 key.


4) Show both images.


Here is the code I'm working with:


int main(int, char**){
VideoCapture cap(1);
Mat img1, img2;
int input;
namedWindow("Imagen 1",CV_WINDOW_AUTOSIZE);
namedWindow("Imagen 2",CV_WINDOW_AUTOSIZE);

for(;;){
input = cvWaitKey(40);
if((char) input ==27)
break;
if ((char) input == 49){
cap >> img1;
imshow("Imagen 1",img1);
}
if ((char) input == 50){
cap >> img2;
imshow("Imagen 2",img2);
}
}
return 0;
}

However, when I do run this I get the same image in both windows. Can anyone explain why this is happening? What can I do to make it work the way I have explained?



If You are working on linux, then You will have to empty the buffer from capturing device. I do it by running a separate thread that reads the frames and remembers only the last one. When I want to take a frame for further processing, I clone the one which is now remembered. But, in Your case it might be a slight overkill.


Also, You might like to do something like this instead of Your current main loop:


cv::Mat temp,img1,img2;
cv::VideoCapture cap(1);
char control=' ';
cv::namedWindow("current",CV_AUTOSIZE);
cv::namedWindow("img1",CV_AUTOSIZE);
cv::namedWindow("img2",CV_AUTOSIZE);
do{
if(49 == control){
img1=temp.clone();
cv::imshow("img1",img1);
}else if(50 == control){
img2=temp.clone();
cv::imshow("img2",img2);
}
cap>>tmp; //emptying buffer all the time
cv::imshow("current",tmp);
control=cv::waitKey(40);//if You are faster than captures fps
}while(27 != control);


You need to put the cap() call inside the loop - otherwise you only do one capture



Instead of using cap >> img1 and cap >> img2I added a Mat variable called "captura" to store the current frame and used img1 = captura.clone() and img2 = captura.clone() respectively and now it's working.


Related Posts:

0 commentaires:

Enregistrer un commentaire