I'm trying to capture image from a webcam following tutorial from this book : Building Computer Vision Projects with OpenCV 4 and C++ (2019). I've updated severeal code following OpenCV 4.2.0 documentation to use VideoCapture
for capturing. For the sake of simplicity, I'm gonna put only code related to calling the camera here :
#include "opencv2/core.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/videoio.hpp"
#include <iostream>
#include <string>
#include <sstream>
#include <GL/glut.h>
using namespace cv;
using namespace std;
Mat frame;
GLfloat angle = 0.0;
GLuint texture;
VideoCapture camera;
int main(int argc, const char** argv){
int deviceID = 2;
int apiID = 0;
camera.open(deviceID + apiID);
if (!camera.isOpened())
{
cout << "ERROR : Can't open camera" << endl;
return -1;
}
namedWindow("OpenGL Camera", WINDOW_OPENGL);
glEnable(GL_TEXTURE_2D);
glGenTextures(1, &texture);
setOpenGlDrawCallback("OpenGL Camera", on_opengl);
while(waitKey(30) !='q'){
camera >> frame;
loadTexture();
updateWindow("OpenGL Camera");
angle = angle+4;
}
destroyWindow("OpenGL Camera");
return 0;
}
Here's the full code in gist. I encountered this error :
[ WARN:0] global /home/raisa/Documents/OpenCV/opencv-4.2.0_source/modules/videoio/src/cap_gstreamer.cpp (935) open OpenCV | GStreamer warning: Cannot query video position: status=0, value=-1, duration=-1
I'm using Camera 2 (external webcam - Logitech C930e), that is tested before and is working fine on Ubuntu. I've set it as default camera as well for guvcview and Cheese with no problem. But even when I tried with Camera 0 (integrated webcam), I still encounter exactly the same issue that lead me to believe it's not camera issue, but coding issue.
I came across this question : Capturing 1080p at 30fps from Logitech C920 with openCV 2.4.3 which using a Logitech camera as well but using very old version of OpenCV (2.4.3), anyway I tried to implement his/her approach by using this :
camera.open(2, CAP_DSHOW);
camera.set(CAP_PROP_FRAME_WIDTH, 1920);
camera.set(CAP_PROP_FRAME_HEIGHT, 1080);
There's no error output but I only get black screen. I thought maybe it needed more time to load, so I also tried to increase waitKey
to waitKey(300)
, but still black screen.
There's also the same problem here : Display a image from the Webcam using openCV and openGL, but it's using Modern OpenGL which I don't use at the moment since I'm following a specific tutorial from a book. If this information helps I'm compiling with CMake in terminal Ubuntu, what I have installed on my machine :
Ubuntu 18.04.4 LTS
OpenCV version : 4.2.0
OpenGL version : 4.6 (Compatibility Profile) Mesa 20.1.0-devel
Cmake version : 3.10.2
Any advice ?