基于OpenCv调用摄像头并显示

Opencv3 计算机视觉 python实现 P25
在窗口显示摄像头帧
出现错误:
[ WARN:0] videoio(MSMF): OnReadSample() is called with error status: -1072875772 [ WARN:0] videoio(MSMF): async ReadSample() call is failed with error status: -1072875772 [ WARN:1] videoio(MSMF): can’t grab frame. Error: -1072875772

解决方法:

import cv2

cameraCapture = cv2.VideoCapture(0 + cv2.CAP_DSHOW)

success, frame = cameraCapture.read()
while(cameraCapture.isOpened() & success):
    success, frame = cameraCapture.read()
    cv2.imshow('image',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cameraCapture.release()
cv2.destroyAllWindows()

改了这么多,感觉还是因为照相机被别的进程占用了,把pycharm里的其他terminal关了就好了,原文的程序也可以用。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
你可以使用Qt和OpenCV调用摄像头。以下是一个简单的示例代码,演示如何使用Qt和OpenCV进行摄像头捕捉: ```cpp #include <QApplication> #include <QMainWindow> #include <QVBoxLayout> #include <QLabel> #include <QTimer> #include <opencv2/opencv.hpp> using namespace cv; class MainWindow : public QMainWindow { Q_OBJECT public: MainWindow(QWidget *parent = nullptr) : QMainWindow(parent) { // 创建布局和标签 QVBoxLayout *layout = new QVBoxLayout(); QLabel *label = new QLabel(this); layout->addWidget(label); // 设置布局为主窗口的中心部分 QWidget *centralWidget = new QWidget(this); centralWidget->setLayout(layout); setCentralWidget(centralWidget); // 创建定时器,用于定时更新摄像头帧 QTimer *timer = new QTimer(this); connect(timer, &QTimer::timeout, this, &MainWindow::updateFrame); timer->start(30); // 设置时间间隔 // 打开摄像头 capture.open(0); if (!capture.isOpened()) { label->setText("无法打开摄像头"); return; } } ~MainWindow() { // 关闭摄像头 capture.release(); } private slots: void updateFrame() { Mat frame; capture >> frame; // 从摄像头获取一帧图像 // 将图像转换为Qt显示的格式 QImage image(frame.data, frame.cols, frame.rows, frame.step, QImage::Format_BGR888); // 调整图像大小以适应标签的大小 image = image.scaled(label->size(), Qt::KeepAspectRatio); // 在标签上显示图像 label->setPixmap(QPixmap::fromImage(image)); } private: VideoCapture capture; QLabel *label; }; int main(int argc, char *argv[]) { QApplication a(argc, argv); MainWindow w; w.show(); return a.exec(); } ``` 这个示例代码创建了一个基于Qt的主窗口,该窗口使用OpenCV捕捉并显示摄像头的帧。注意,需要在项目中添加OpenCV和Qt的头文件和库。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值