【发布时间】:2013-08-02 09:10:09
【问题描述】:
我需要在 Qt GUI 应用程序中显示网络摄像头源。我如何使用 OpenCv 做到这一点?从早上开始,我就一直在为这件事头疼。如果有人可以提供示例代码,我将不胜感激。
【问题讨论】:
-
stackoverflow.com/questions/11606657/… 认为这可能会间接回答您的问题
我需要在 Qt GUI 应用程序中显示网络摄像头源。我如何使用 OpenCv 做到这一点?从早上开始,我就一直在为这件事头疼。如果有人可以提供示例代码,我将不胜感激。
【问题讨论】:
这对我有用:
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));
....
【讨论】: