Problem: OpenCv not able to detect the camera while Qt can
Hello all,
I am using Qt 5.11 on Windows 10 and Opencv 3.2.0.
I am trying to make a simple application which can detect a camera, display in QLabel and record it. The problem is - when I use opencv to detect integrated webcam and display it, it works fine. When I try to attach a microscopic camera OP TV10 HD link for camera , opencv gives output as black screen even after installing the drivers for the camera. Code for recording is not yet written as I am stuck on the display part.
MainWindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QActionGroup *videoDevicesGroup = new QActionGroup(this);
videoDevicesGroup->setExclusive(true);
foreach (const QCameraInfo &cameraInfo, QCameraInfo::availableCameras())
{
QAction *videoDeviceAction = new QAction(cameraInfo.description(), videoDevicesGroup);
videoDeviceAction->setCheckable(true);
videoDeviceAction->setData(QVariant::fromValue(cameraInfo));
if (cameraInfo == QCameraInfo::defaultCamera())
videoDeviceAction->setChecked(true);
ui->comboBox_listDevices->addItem(cameraInfo.description());
}
connect(ui->comboBox_listDevices, SIGNAL(currentIndexChanged(int)), this, SLOT(comboBoxIndexChanged(int)));
}
void MainWindow::comboBoxIndexChanged(int camIndex)
{
disconnect(videoTimer, SIGNAL(timeout()), this, SLOT(initializeCam()));
videoTimer->stop();
startCam(camIndex);
}
void MainWindow::startCam(int camIndex)
{
m_videoCapture = new VideoCapture(camIndex);
videoTimer = new QTimer;
connect(videoTimer, SIGNAL(timeout()), this, SLOT(initializeCam()));
videoTimer->start();
//to set the video output as per the size of the display widget
dWidth = m_videoCapture->get(CV_CAP_PROP_FRAME_WIDTH);
dHeight = m_videoCapture->get(CV_CAP_PROP_FRAME_HEIGHT);
}
void MainWindow::initializeCam()
{
Mat frame;
bool bSuccess = m_videoCapture->read(frame);
if(!bSuccess)
qDebug()<<"Cannot read a frame from video file";
QImage qimg = QImage((uchar*)frame.data, frame.cols, frame.rows, frame.step, QImage::Format_RGB888);
qimg = qimg.rgbSwapped();
ui->label->setPixmap(QPixmap::fromImage(qimg).scaled(ui->label->size(), Qt::KeepAspectRatio, Qt::FastTransformation));
}
On the other hand, without using opencv when I try to use Qt inbuilt class of QCamera and QCameraViewfinder to detect and display the camera output, it works fine for both the cameras but QMediaRecorder doesn't work on Windows to record a video therefore I was using opencv to display and record the video for the above mentioned cameras. Code when Qt inbuilt class is used to detect and display.
MainWindow.cpp
MainWindow::MainWindow(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::MainWindow)
{
ui->setupUi(this);
QActionGroup *videoDevicesGroup = new QActionGroup(this);
videoDevicesGroup->setExclusive(true);
foreach (const QCameraInfo &cameraInfo, QCameraInfo::availableCameras())
{
QAction *videoDeviceAction = new QAction(cameraInfo.description(), videoDevicesGroup);
videoDeviceAction->setCheckable(true);
videoDeviceAction->setData(QVariant::fromValue(cameraInfo));
if (cameraInfo == QCameraInfo::defaultCamera())
videoDeviceAction->setChecked(true);
ui->comboBox_listDevices->addItem(cameraInfo.description());
}
connect(ui->comboBox_listDevices, SIGNAL(currentIndexChanged(int)), this, SLOT(comboBoxIndexChanged(int)));
}
void MainWindow::comboBoxIndexChanged(int camIndex)
{
QCameraInfo cameraInfo;
QList<QCameraInfo> cameras = QCameraInfo::availableCameras();
foreach (cameraInfo, cameras)
qDebug()<<cameraInfo.description();
camera = new QCamera(cameraInfo);
viewfinder = new QCameraViewfinder();
camera->setViewfinder(viewfinder);
ui->verticalLayout_display->addWidget(viewfinder);
camera->start();
}
I get two debugging messages when using the microscopic camera:
Failed to get the video control
failed to find the video proc amp
Can anyone help me solve the problem of black screen using openCV or help me in recording a video using Qt inbuilt class of QCamera and QCameraViewfinder on Windows 10?
Thank you for your time and knowledge.
it might be a good idea, to update it to latest 3.4.1-dev (3.4 branch),alot of work went into the video io parts recently
you could also try to select a specific backend, like :
cap.open(0,CAP_MSMF);
(IF you have that compiled in)last, but not least, you could use the Qt capture, and somehow grab a cv::Mat from that, and use VideoWriter to save your video.