mardi 22 avril 2014

c ++ - ne peut pas saisir la trame dans OpenCV - Stack Overflow


I have been trying to get openCV to read an image from my computer's webcam. The code below successfully opens the webcam (green light turns on). However, attempts to grab a frame and hence read a frame fail. I am at a loss here. Can anyone help?


Many Thanks, Hillary


P.S. I am running Mac OS X 10.9 on a MacBook Pro. And my opencv version is 2.4.6.1


And here is the code:


#include "opencv.hpp"
using namespace cv;

int main(int, char**) {

VideoCapture cap = VideoCapture(0);

if(!cap.isOpened()){
printf("failed to open camera\n");
return -1;
}

namedWindow("edges",1);

for(;;){
if(waitKey(50) >= 0 ) break;

if(!cap.grab()){
printf("failed to grab from camera\n");
}
}

return 0;
}



You forgot to read new frames in your loop and show them! There:


for(;;){
if(waitKey(50) >= 0 ) break;
Mat frame;
if(!cap.grab()){
printf("failed to grab from camera\n");
break;
}
cap >> frame;

if(frame.empty()){
printf("failed to grab from camera\n");
break;
}

imshow("edges", frame);
}


I have been trying to get openCV to read an image from my computer's webcam. The code below successfully opens the webcam (green light turns on). However, attempts to grab a frame and hence read a frame fail. I am at a loss here. Can anyone help?


Many Thanks, Hillary


P.S. I am running Mac OS X 10.9 on a MacBook Pro. And my opencv version is 2.4.6.1


And here is the code:


#include "opencv.hpp"
using namespace cv;

int main(int, char**) {

VideoCapture cap = VideoCapture(0);

if(!cap.isOpened()){
printf("failed to open camera\n");
return -1;
}

namedWindow("edges",1);

for(;;){
if(waitKey(50) >= 0 ) break;

if(!cap.grab()){
printf("failed to grab from camera\n");
}
}

return 0;
}


You forgot to read new frames in your loop and show them! There:


for(;;){
if(waitKey(50) >= 0 ) break;
Mat frame;
if(!cap.grab()){
printf("failed to grab from camera\n");
break;
}
cap >> frame;

if(frame.empty()){
printf("failed to grab from camera\n");
break;
}

imshow("edges", frame);
}

0 commentaires:

Enregistrer un commentaire