Ask Your Question
0

Problem: OpenCv not able to detect the camera while Qt can

asked 2018-06-11 07:01:27 -0600

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.

edit retag flag offensive close merge delete

Comments

opencv3.2

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.

berak gravatar imageberak ( 2018-06-11 07:10:29 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-06-13 07:04:00 -0600

Hi Berak,

Based on you suggestion of passing an image into cv::Mat and using Video Writer, I am trying to do that but the video file generated is 0 bytes always. Could you have a look at the code and tell me where I am going wrong?

//initiate video writer settings
    Size frameSize(static_cast<int>(dWidth), static_cast<int>(dHeight));
    QString dateTime = QDateTime::currentDateTime().toString("dddd dd MMMM yyyy, hh-mm-ss ");
    QString fileNameVideo = "Video_" + dateTime + ".avi";
    QDir::setCurrent("Recordings/");
    VideoWriter temp_writer(fileNameVideo.toStdString().c_str(), CV_FOURCC('D', 'I', 'V', 'X'), 30, frameSize, true);//DIVX or XVID or MJPG

    m_videoWriter = temp_writer;

//passing QImage into cv::Mat continuously
cv::Mat frame(qimg.height(), qimg.width(),CV_8UC3, (void *)(qimg.constBits()), (qimg.bytesPerLine()));
if(!m_videoWriter.isOpened())
    qDebug()<<"Failed to write the video";
m_videoWriter.write(frame);

// with the help of a button releasing video writer
m_videoWriter.release();

When I try to check if the cv::Mat frame has received an image by passing it to QLabel, it shows the image that means QImage is getting passed into cv::Mat frame but it is not getting written inside VideoWriter and I don't know why.

Any suggestion would be helpful. Thank you for your time and knowledge.

edit flag offensive delete link more

Comments

make sure, opencv_ffmpeg_64.dll is on the PATH (or next to the app). all video readiing/writing fuctionality is in there. (and i think, xvid works, but divx not)

berak gravatar imageberak ( 2018-06-13 07:06:38 -0600 )edit

tried doing that as well. Still the video file size is 0 bytes. Any other methods that I could try with?

wreck_zone gravatar imagewreck_zone ( 2018-06-13 07:47:24 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2018-06-11 07:01:27 -0600

Seen: 1,708 times

Last updated: Jun 13 '18