对于 OpenCV,我的 Python 代码可以显示网络摄像头,但我的 C++ 不能 [英] For OpenCV, my Python code can display webcam, but my C++ cannot
问题描述
我使用的是带有 OpenCV 3.3.1、Python 3、最新 C++ 和 Visual Studio 2017 的 64 位 Windows 10.
I'm on Windows 10 64-bit with OpenCV 3.3.1,Python 3,the latest C++, and Visual Studio 2017.
这是我的 Python 3 代码,可以正确显示我的网络摄像头:
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()
以下是不显示我的网络摄像头的 C++ 代码.此代码仅显示灰色框:
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;
}
有人可以帮我解决这个问题吗?我尝试将我的 C++ 修改为 VideoCapture cap(1)、VideoCapture cap(2)、VideoCapture cap(3) 和 VideoCapture cap,但我的网络摄像头仍然没有获得实时视频.
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.
我正在运行的 Python 代码的屏幕截图:
Screenshot of my running Python Code:
我正在运行的 C++ 代码的屏幕截图:
Screenshot of my running C++ Code:
推荐答案
我终于让我的网络摄像头正确显示在 Visual Studio C++ OpenCV 中.这是我所做的:
I finally got my webcam to show up properly for Visual Studio C++ OpenCV. Here's what I did:
- 使用 C++ 和 Python 工具安装 Visual Studio 2015.
- 下载并解压 OpenCV 3.4.2
- 添加到路径 C:\<...>\opencv\build\x64\vc14\bin
- 在 Visual Studio 2015 中,创建一个 Win32 控制台应用程序.
- 右键单击您的项目并选择属性.进行以下更改:
一个.C/C++ - 常规 - 附加包含目录:C:\<...>\opencv\build\include
a. C/C++ - General - Additional Include Directories: C:\<...>\opencv\build\include
B.链接器 - 通用 - 附加库目录:C:\<...>\opencv\build\x64\vc14\lib
b. Linker - General - Additional Library Directories: C:\<...>\opencv\build\x64\vc14\lib
c.链接器 - 输入:opencv_world342d.lib
c. Linker - Input: opencv_world342d.lib
谢谢大家的回复!
这篇关于对于 OpenCV,我的 Python 代码可以显示网络摄像头,但我的 C++ 不能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!