2013-08-01 121 views
1

我需要在Qt GUI應用程序中顯示攝像頭源。我如何使用OpenCv做到這一點?自上午以來,我一直在爲此而頭痛。如果任何人都可以展示示例代碼,我會非常感激。如何使用OpenCv在Qt GUI應用程序中顯示攝像頭源?

+0

http://stackoverflow.com/questions/11606657/qt-gui-freezes-when-capturing-video-from-webcam-using-opencv 認爲這可能會間接回答你的問題 – GPPK

回答

3

這爲我工作:

QImage MatToQImage(const Mat& mat) 
{ 
    // 8-bits unsigned, NO. OF CHANNELS=1 
    if(mat.type()==CV_8UC1) 
    { 
     // Set the color table (used to translate colour indexes to qRgb values) 
     QVector<QRgb> colorTable; 
     for (int i=0; i<256; i++) 
      colorTable.push_back(qRgb(i,i,i)); 
     // Copy input Mat 
     const uchar *qImageBuffer = (const uchar*)mat.data; 
     // Create QImage with same dimensions as input Mat 
     QImage img(qImageBuffer, mat.cols, mat.rows, mat.step, QImage::Format_Indexed8); 
     img.setColorTable(colorTable); 
     return img; 
    } 
    // 8-bits unsigned, NO. OF CHANNELS=3 
    if(mat.type()==CV_8UC3) 
    { 
     // Copy input Mat 
     const uchar *qImageBuffer = (const uchar*)mat.data; 
     // Create QImage with same dimensions as input Mat 
     QImage img(qImageBuffer, mat.cols, mat.rows, mat.step, QImage::Format_RGB888); 
     return img.rgbSwapped(); 
    } 
    else 
    { 
     qDebug() << "ERROR: Mat could not be converted to QImage."; 
     return QImage(); 
    } 
} // MatToQImage() 
.... 
// Then use it in main code as follows 
// Display frame in main window 
frameLabel->setPixmap(QPixmap::fromImage(frame)); 
.... 
相關問題