c++ - OpenCV : How to display webcam capture in windows form application?

标签 c++ forms video opencv webcam

通常我们在 opencv 窗口中显示网络摄像头或视频运动:

      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;
    }
   }

我尝试使用 pictureBox 成功地在 Windows 窗体中显示图像:

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

但是当我试图显示从视频中捕获的图像时它不会工作,这是来源:

            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;
    }
   }

有没有办法使用 windows 窗体而不是 opencv windows 来显示视频或网络摄像头?

还是我的代码有问题? 感谢您的帮助..:)

最佳答案

一条建议:使用VideoInput而不是 CvCapture(CvCapture 是 highgui 库的一部分,它不是用于生产用途,而是用于快速测试)。是的,VideoInput 主页看起来很奇怪,但是这个库非常值得。

这里是使用 VideoInput 的快速示例(从 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);

关于c++ - OpenCV : How to display webcam capture in windows form application?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2863918/

相关文章:

c++ - 在 Opengl 中渲染。重叠对象

css - 对于 CSS,如果我们将显示设置为表格、表格行或表格单元格,那么我们为什么不直接使用 <table><tr><td> 来做呢?

javascript - 将 jQuery 脚本挂接到 &lt;input&gt; 和 <button> 标签中

javascript - 如何使用 JavaScript 将多个 HTML 表单作为一个表单提交给 PHP 脚本?

node.js - NodeJS 视频流实时转码

video - HLS - ffmpeg 失败,无法为输出文件 #0 写入 header (编解码器参数不正确?): No such file or directory

c++ - 为什么 C++ 中可以用撇号分隔数字,而 C 中却不能?

c++ - make_heap 的意义何在?

c++ - 如何创建 DDE 服务器

video - 你能控制在 Quartz Composer 中播放视频剪辑的速度吗?