I'm on Windows 10 64-bit with OpenCV 3.3.1,Python 3,the latest C++, and Visual Studio 2017.
Here is my Python 3 code that properly displays my webcam:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
ret, last_frame = cap.read()
row, col, ch = last_frame.shape
if last_frame is None:
exit()
while(cap.isOpened()):
ret, frame = cap.read()
if frame is None:
exit()
cv2.imshow('frame', frame)
if cv2.waitKey(33) == ord('q'):
break
cap.release()
cv2.destroyAllWindows()
Below is a C++ code that does not display my webcam. This code only displays a grey frame:
#include "opencv2/opencv.hpp"
using namespace cv;
#include <iostream>
using namespace std;
int main()
{
VideoCapture cap(0); // 1st device, DSHOW
while (cap.isOpened())
{
Mat frame;
cap >> frame;
imshow("ocv", frame);
int k = waitKey(10);
if (k == 27) break;
}
return 0;
}
Can someone please help me with this issue? I tried modifying my C++ to VideoCapture cap(1), VideoCapture cap(2), VideoCapture cap(3), and VideoCapture cap but I still get no live video from my webcam.