Unable to open VideoCapture and capture frame

[Question] Unable to open VideoCapture or capture frame

I have tried opening a camera using VideoCapture like so:

vidcap = new cv::VideoCapture(0, cv::CAP_V4L2);

if (!vidcap->isOpened()) {
	std::cerr << "Couldn’t open capture" << std::ends;
	return 1;

cv::Mat frame;

(*vidcap) >> frame;

if (frame.empty()) {
	std::cerr << "Frame is empty" << std::endl;

But I get “Frame is empty” as output.

I tried changing the first line to:

std::string pipeline = "v4l2src device=/dev/video0 ! video/x-raw, format=BGRx, depth=8, width=1920, height=1080 ! videoconvert";
vidcap = new cv::VideoCapture(pipeline, cv::CAP_V4L2);

But this give the output “Couldn’t open capture”

I’m not sure where to debug from here, any help on this?

pipelines like this only work with the gstreamer backend:
vidcap = new cv::VideoCapture(pipeline, cv::CAP_GSTREAMER);

(check cv::getBuildInformation() for builtin backends)