简体   繁体   中英

OpenCV cannot open a camera by using Qt

I figured out that Qt creator is using Qt for the OpenCV functions by default.

When even running a test code (see below) which opens and shows a camera stream. Here, it is not possible to open the camera (I am using a XIMEA xiQ). With a normal webcam it is working.

In Eclipse both is working.

Brief summary of steps I have done so far:

  1. OpenCV is compiled with XIMEA camera support
  2. I recompiled OpenCV with Qt support
  3. make uninstall for the current installation of OpenCV
  4. make install for the new XIMEA & Qt support enabled installation

My test code:

#include "mainwindow.h"
#include <QApplication>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>

using namespace cv;
using namespace std;

int main(int argc, char *argv[]){
  QApplication a(argc, argv);
  MainWindow w;
  w.show();

  VideoCapture cap(0);
  if (!cap.isOpened()){
          cout << "Cannot open the video cam" << endl;
          return -1;
  }

  while (1){
          Mat frame;
          bool bSuccess = cap.read(frame);

      if (!bSuccess){
          cout << "Cannot read a frame from video stream" << endl;
          break;
      }
      imshow("MyVideo", frame);
      if (waitKey(30) == 27){
          cout << "esc key is pressed by user" << endl;
          break;
      }
  }
  return a.exec();
}

Finally, I was able to figure it out.

I don't know why (hopefully somebody will have an explanation for that behaviour) but Qt Creator does not always seems to use the newest version of OpenCV.

I found some bits of a previous installation on my computer which Qt Creator was using instead of the newer versioned library files.

After purging every OpenCV bit and recompiling it with Qt and XIMEA camera driver support (and other unimportant stuff) everything was working fine.

Hope that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM