嵌入式爱好者

查看: 17167|回复: 2

[Linux] OK4418开发板qt opencv 如何调用USB摄像头

[复制链接]

2

主题

5

帖子

16

积分

XX18通行证

扫一扫,手机访问本帖
发表于 2019-3-22 14:40:42 | 显示全部楼层 |阅读模式
本帖最后由 njxnet 于 2019-3-22 16:10 编辑

开发板移植了opencv2.4.9。
QT代码如下:
widget.h
  1. #ifndef WIDGET_H
  2. #define WIDGET_H

  3. #include <QWidget>

  4. #include <QImage>
  5. #include <QTimer>     // 设置采集数据的间隔时间

  6. #include <opencv/highgui.h>  //包含opencv库头文件
  7. #include <opencv/cv.h>

  8. namespace Ui {
  9. class Widget;
  10. }

  11. class Widget : public QWidget
  12. {
  13.     Q_OBJECT

  14. public:
  15.     explicit Widget(QWidget *parent = 0);
  16.     ~Widget();

  17. private slots:
  18.     void openCamara();      // 打开摄像头
  19.     void readFarme();       // 读取当前帧信息
  20.     void closeCamara();     // 关闭摄像头。
  21.     void takingPictures();  // 拍照

  22. private:
  23.     Ui::Widget *ui;
  24.     QTimer    *timer;
  25.     QImage    *imag;
  26.     CvCapture *cam;// 视频获取结构, 用来作为视频获取函数的一个参数
  27.     IplImage  *frame;//申请IplImage类型指针,就是申请内存空间来存放每一帧图像
  28. };

  29. #endif // WIDGET_H
复制代码


widget.cpp
  1. #include "widget.h"
  2. #include "ui_widget.h"

  3. Widget::Widget(QWidget *parent) :
  4.     QWidget(parent),
  5.     ui(new Ui::Widget)
  6. {
  7.     ui->setupUi(this);

  8.     cam = NULL ;
  9.     timer = new QTimer(this);
  10.     imag = new QImage();

  11.     connect(timer,SIGNAL(timeout()),this,SLOT(readFarme()));   //时间到,读取当前摄像头信息
  12.     connect(ui->OpenCameraPushButton,SIGNAL(clicked()),this,SLOT(openCamara()));
  13.     connect(ui->TakePhotoPushButton,SIGNAL(clicked()),this,SLOT(takingPictures()));
  14.     connect(ui->CloseCameraPushButton,SIGNAL(clicked()),this,SLOT(closeCamara()));

  15. }

  16. void Widget::openCamara()
  17. {
  18.     cam = cvCreateCameraCapture(-1);  //打开摄像头,从摄像头中获取视频
  19.     timer->start(33);                // 开始计时,超时则发出timeout()信号
  20. }

  21. void Widget::readFarme()
  22. {
  23.     frame = cvQueryFrame(cam);

  24.     QImage image((const uchar*)frame->imageData, frame->width,frame->height,QImage::Format_RGB888);

  25.     ui->label->setPixmap(QPixmap::fromImage(image));
  26. }

  27. void Widget::takingPictures()
  28. {
  29.     frame = cvQueryFrame(cam);

  30.     QImage image((const uchar*)frame->imageData, frame->width,frame->height,QImage::Format_RGB888);

  31.     ui->label_2->setPixmap(QPixmap::fromImage(image));
  32. }

  33. void Widget::closeCamara()
  34. {
  35.     timer->stop();

  36.     cvReleaseCapture(&cam);
  37. }

  38. Widget::~Widget()
  39. {
  40.     delete ui;
  41.     delete timer;
  42.     delete imag;
  43.     delete frame;
  44. }
复制代码


main.cpp
  1. #include "widget.h"
  2. #include <QApplication>

  3. int main(int argc, char *argv[])
  4. {
  5.     QApplication a(argc, argv);
  6.     Widget w;
  7.     w.show();

  8.     return a.exec();
  9. }
复制代码


.pro
  1. #-------------------------------------------------
  2. #
  3. # Project created by QtCreator 2019-03-21T20:20:55
  4. #
  5. #-------------------------------------------------

  6. QT       += core gui

  7. greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

  8. TARGET = Opencv_pro1
  9. TEMPLATE = app


  10. SOURCES += main.cpp\
  11.         widget.cpp

  12. INCLUDEPATH += /usr/local/opencv/install_opencv/include/\
  13.             /usr/local/opencv/install_opencv/include/opencv\
  14.             /usr/local/opencv/install_opencv/include/opencv2


  15. LIBS += /usr/local/opencv/install_opencv/lib/libopencv_calib3d.so\
  16.         /usr/local/opencv/install_opencv/lib/libopencv_contrib.so\
  17.         /usr/local/opencv/install_opencv/lib/libopencv_core.so\
  18.         /usr/local/opencv/install_opencv/lib/libopencv_features2d.so\
  19.         /usr/local/opencv/install_opencv/lib/libopencv_flann.so\
  20.         /usr/local/opencv/install_opencv/lib/libopencv_gpu.so\
  21.         /usr/local/opencv/install_opencv/lib/libopencv_highgui.so \
  22.         /usr/local/opencv/install_opencv/lib/libopencv_imgproc.so\
  23.         /usr/local/opencv/install_opencv/lib/libopencv_legacy.so\
  24.         /usr/local/opencv/install_opencv/lib/libopencv_ml.so\
  25.         /usr/local/opencv/install_opencv/lib/libopencv_nonfree.so\
  26.         /usr/local/opencv/install_opencv/lib/libopencv_objdetect.so\
  27.         /usr/local/opencv/install_opencv/lib/libopencv_ocl.so\
  28.         /usr/local/opencv/install_opencv/lib/libopencv_photo.so\
  29.         /usr/local/opencv/install_opencv/lib/libopencv_stitching.so\
  30.         /usr/local/opencv/install_opencv/lib/libopencv_superres.so\
  31.         /usr/local/opencv/install_opencv/lib/libopencv_video.so\
  32.         /usr/local/opencv/install_opencv/lib/libopencv_videostab.so


  33. HEADERS  += widget.h

  34. FORMS    += widget.ui
复制代码

出现的问题:交叉编译移植ARM,点击PushButton打开摄像头,超级终端一直输出 VIDIOC_QUERYCTRL: Inappropriate ioctl for device
回复

使用道具 举报

1

主题

3670

帖子

4177

积分

发表于 2019-3-25 10:18:58 | 显示全部楼层
很抱歉,没有遇到过类似问题
是兼容性问题吗?uvc驱动里有VIDIOC_QUERYCTRL
技术支持电话:0312-3119192
技术支持邮箱:Android@forlinx.com
点评回复 支持 反对

使用道具 举报

0

主题

1

帖子

9

积分

XX18通行证

发表于 2019-10-25 15:58:12 | 显示全部楼层
遇到同样的问题,贴主解决了么?
点评回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|小黑屋| 飞凌嵌入式 ( 冀ICP备12004394号-1 )

GMT+8, 2024-12-20 18:22

Powered by Discuz! X3.4

© 2001-2013 Comsenz Inc.

快速回复 返回顶部 返回列表