OpenCV之摄像头捕捉图像

代码运行效果代码之前我找过directshow,CameraDS,VedioCaptureFromCam之类的东西,发现都不可以用。directshow是因为版本太老了,会出现: http://www.opencv.org.cn/forum.php?mod=viewthread&action=printable&tid=33813 VedioCaptureFromCam就会显示不出图片。所以后
摘要由CSDN通过智能技术生成

代码

之前我找过directshow,CameraDS,VedioCaptureFromCam之类的东西,发现都不可以用。directshow是因为版本太老了,会出现:
http://www.opencv.org.cn/forum.php?mod=viewthread&action=printable&tid=33813
VedioCaptureFromCam就会显示不出图片。所以后来我想想应该是库的问题,要包含最新的库。我不是学习CV的,这次就算是乱搞一通了。
后来我发现我的OpenCV的版本是2.4.10

#include <opencv2/highgui/highgui.hpp>  
#include <opencv2/imgproc/imgproc.hpp>  
#include <opencv2/core/core.hpp>

using namespace cv;

int main()  
{  

    VideoCapture cap0(
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你可以使用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的头文件和库。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值