2010-05-19 18 views
6

general Mostramos cámara web o de movimiento de vídeo en ventanas OpenCV con:OpenCV: ¿Cómo mostrar la captura de cámara web en la aplicación de formulario de Windows?

 CvCapture* capture = cvCreateCameraCapture(0); 
      cvNamedWindow("title", CV_WINDOW_AUTOSIZE); 
    cvMoveWindow("title",x,y); 
    while(1) 
    { 
    frame = cvQueryFrame(capture); 
    if(!frame) 
    { 
    break; 
    } 
    cvShowImage("title", frame); 
    char c = cvWaitKey(33); 
    if(c == 27) 
    { 
    break; 
    } 
    } 

i intentado utilizar cuadro de imagen que tiene éxito para mostrar la imagen en las ventanas de formar con esto:

pictureBox1->Image = gcnew System::Drawing::Bitmap(image->width,image->height,image->widthStep,System::Drawing::Imaging::PixelFormat::Undefined, (System::IntPtr) image-> imageData); 

pero cuando estoy tratando de visualizar la imagen capturada del vídeo no lo puedo funciona, aquí está la fuente:

  CvCapture* capture = cvCreateCameraCapture(0); 
    while(1) 
    { 
    frame = cvQueryFrame(capture); 
    if(!frame) 
    { 
    break; 
    } 
    pictureBox1->Image = gcnew System::Drawing::Bitmap(frame->width,frame->height,frame->widthStep,System::Drawing::Imaging::PixelFormat::Undefined, (System::IntPtr) frame-> imageData); 
    char c = cvWaitKey(33); 
    if(c == 27) 
    { 
    break; 
    } 
    } 

hay alguna forma de utilizar ventanas formulario en lugar de ventana OpenCV s para mostrar video o webcam?

o hay algún problema con mi código? gracias por su ayuda .. :)

Respuesta

1

consejo: utilizar VideoInput en lugar de CvCapture (CvCapture es una parte de highgui una biblioteca que no está destinado para uso en producción, pero sólo para las pruebas rápida). Sí, la página de inicio de VideoInput se ve extraña, pero la biblioteca vale la pena.

Aquí es una muestra rápida para el uso de VideoInput (extraída del archivo VideoInput.h):

//create a videoInput object 
videoInput VI; 

//Prints out a list of available devices and returns num of devices found 
int numDevices = VI.listDevices(); 

int device1 = 0; //this could be any deviceID that shows up in listDevices 
int device2 = 1; //this could be any deviceID that shows up in listDevices 

//if you want to capture at a different frame rate (default is 30) 
//specify it here, you are not guaranteed to get this fps though. 
//VI.setIdealFramerate(dev, 60);  

//setup the first device - there are a number of options: 

VI.setupDevice(device1);       //setup the first device with the default settings 
//VI.setupDevice(device1, VI_COMPOSITE);    //or setup device with specific connection type 
//VI.setupDevice(device1, 320, 240);     //or setup device with specified video size 
//VI.setupDevice(device1, 320, 240, VI_COMPOSITE); //or setup device with video size and connection type 

//VI.setFormat(device1, VI_NTSC_M);     //if your card doesn't remember what format it should be 
                //call this with the appropriate format listed above 
                //NOTE: must be called after setupDevice! 

//optionally setup a second (or third, fourth ...) device - same options as above 
VI.setupDevice(device2);       

//As requested width and height can not always be accomodated 
//make sure to check the size once the device is setup 

int width = VI.getWidth(device1); 
int height = VI.getHeight(device1); 
int size = VI.getSize(device1); 

unsigned char * yourBuffer1 = new unsigned char[size]; 
unsigned char * yourBuffer2 = new unsigned char[size]; 

//to get the data from the device first check if the data is new 
if(VI.isFrameNew(device1)){ 
    VI.getPixels(device1, yourBuffer1, false, false); //fills pixels as a BGR (for openCV) unsigned char array - no flipping 
    VI.getPixels(device1, yourBuffer2, true, true);  //fills pixels as a RGB (for openGL) unsigned char array - flipping! 
} 

//same applies to device2 etc 

//to get a settings dialog for the device 
VI.showSettingsWindow(device1); 


//Shut down devices properly 
VI.stopDevice(device1); 
VI.stopDevice(device2); 
+0

¿La salida simultánea de video y el procesamiento de la imagen de fondo de la misma información afectan significativamente el rendimiento? –

+0

No estoy seguro de entender su pregunta correctamente. Por lo que recuerdo, la imagen tomada no se copia directamente al buffer de la pantalla. Sin embargo, VideoInput se basa en DirectShow, lo que implica dos cosas: es muy rápido, y es un PITA para compilar (debe obtener la implementación correspondiente de DirectShow de Microsoft). La última vez que lo intenté, tuve que obtener una versión anterior de la biblioteca de DirectShow. Sin embargo, en la descarga se proporciona una versión compilada de la biblioteca VideoInput. –

1

El formato de píxel debe ser conocido cuando se captura imágenes de una cámara, es muy probable que el formato de BGR de 24 bits. System::Drawing::Imaging::PixelFormat::Format24bppRgb será el formato más cercano, pero es posible que obtenga una visualización de color extraña. Una reorganización del componente de color resolverá este problema.

En realidad, hay biblioteca OpenCV versión .NET disponible aquí: http://code.google.com/p/opencvdotnet/ y aquí: http://www.emgu.com/wiki/index.php/Main_Page

espero que ayude!

1

No sé si te gustará esto, pero podrías usar OpenGL para mostrar el flujo de video en otras ventanas que no sean las que se proporcionan con opencv. (Capture el cuadro y muéstrelo en un rectángulo ... o algo así ...)

0

Otra opción que quizás desee considerar es usar emgu. Este es un contenedor .Net para opencv con controles winforms.

Cuestiones relacionadas