0
votes

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.

Screenshot of my running Python Code: Python Code

Screenshot of my running C++ Code: Cpp Code

1
I find it strange, your code is correct, check if you are missing some dll, you could also explain how you have installed opencveyllanesc
For a quick response regarding my OpenCV installation for C++ and Visual Studio 2017, I followed the instructions in this video: youtube.com/…. The instructions are also available here in text: deciphertechnic.com/install-opencv-with-visual-studio I can elaborate more on my installation process if it helps.Robin Alvarenga
Try running a release build instead of a debug build as folks sometimes get their library linking mixed up.Mark Setchell
@MarkSetchell I set the Solution Configuration to Release, but that just creates red wiggly lines on my #include "opencv2/opencv.hpp"Robin Alvarenga
That suggests your settings are different between debug and release and slightly wrong at least. You need to check the setting for where "include" (or "header") files are located - sorry I don't use MS products so I can't tell you the actual name. Check your DLL and linker settings too.Mark Setchell

1 Answers

1
votes

I finally got my webcam to show up properly for Visual Studio C++ OpenCV. Here's what I did:

  1. Install Visual Studio 2015 with C++ and Python Tools.
  2. Download and extract OpenCV 3.4.2
  3. Add to Path C:\<...>\opencv\build\x64\vc14\bin
  4. In Visual Studio 2015, create a Win32 CONSOLE Application.
  5. Right click on your project and select properties. Makes the following changes:

a. C/C++ - General - Additional Include Directories: C:\<...>\opencv\build\include

b. Linker - General - Additional Library Directories: C:\<...>\opencv\build\x64\vc14\lib

c. Linker - Input: opencv_world342d.lib

Thank you everyone for your response!