【树莓派c++图像处理起航1】

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录

  • 前言
  • 一、Qt OPENCV 安装测试?
    • 1. 安装qt
    • 2.安装opencv 的基础库
    • 3. 安装的路就决定了不会一帆风顺
      • 3.1.QT 安装出错
      • 3.2 运行Qt错误
    • 4. opencv实际路径,添加入Qt工程中
  • 二、使用步骤
    • 1.框架
      • 1. 开启摄像头
      • 2.获取显示图像的内容
    • 2.测试
      • 2.1opencv 调用摄像头发现调用失败反复查找问题,
        • 2.1.1查看摄像头所有参数指令
  • 三、接着测试
    • 1.出现问题1:
    • 2.中途去开启了摄像头做了重启
    • 3.效果如图所示:
  • 总结


前言

提示:这里可以添加本文要记录的大概内容:

嵌入式图像处理


提示:以下是本篇文章正文内容,下面案例可供参考

一、Qt OPENCV 安装测试?

示例:树莓派opencv 的安装方法很多,

  • 一种是下载源码编译然后安装,这种叫做大神。要最新,要全还得自己编译。
  • 一种是通过指令直接下载对应的opencv依赖项,应该是树莓派生态比较好,所以能应用的资源比较多。
    以下是这类安装的方法,但是就是这类的安装还是有一堆问题
    c++ opencv 树莓派运行往下将变得简单,

1. 安装qt

输入一下指令

sudo apt-get install qt5-default
sudo apt-get install qtcreator

【树莓派c++图像处理起航1】_第1张图片
Qt 安装完毕后记得检测一下编译器路径是否对了。
【树莓派c++图像处理起航1】_第2张图片

2.安装opencv 的基础库

一直以为安装opencv c++的库要重新编译树莓派环境下的源码,直到看了大佬的视频才明白,原来只安装依赖项就可以用了,这个真的很省事
命令如下:安装往后记得找一下文件的路径

sudo apt-get install libopencv-dev

sudo find / -name "libopencv_core.so"

【树莓派c++图像处理起航1】_第3张图片

3. 安装的路就决定了不会一帆风顺

3.1.QT 安装出错

安装时遇到错误,
在ubuntu22.04上安装qt5-defualt时,我会得到以下错误。

sudo apt-get install qt5-default
复制
产出:

Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Package qt5-default is not available, but is referred to by another package.
This may mean that the package is missing, has been obsoleted, or
is only available from another source

E: Package 'qt5-default' has no installation candidate

Stack Overflow用户
发布于 2022-10-18 20:28:25

Ubuntu22.04存储库没有qt5-默认包。
解决了Qt安装出错的问题

您可以通过安装qtbase5 5-dev来获得预构建的库。

sudo apt install qtbase5-dev qt5-qmake

【树莓派c++图像处理起航1】_第4张图片

3.2 运行Qt错误

在这里插入图片描述
【树莓派c++图像处理起航1】_第5张图片
更改编译器,解决了Qt运行出错的问题
【树莓派c++图像处理起航1】_第6张图片
查找摄像头设备,为测试做准备
【树莓派c++图像处理起航1】_第7张图片

【树莓派c++图像处理起航1】_第8张图片

4. opencv实际路径,添加入Qt工程中

INCLUDEPATH +=/usr/include/opencv4\
/usr/include/opencv4/opencv2

LIBS +=usr/lib/arm-linux-gnueabihf/libopencv_*.so

【树莓派c++图像处理起航1】_第9张图片
在这里插入图片描述

二、使用步骤

1.框架

【树莓派c++图像处理起航1】_第10张图片

1. 开启摄像头

测试:
静态图像
动态图像

raspistill  //静态图像
raspivid    //动态图像

2.获取显示图像的内容

代码

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

CONFIG += c++11

# You can make your code fail to compile if it uses deprecated APIs.
# In order to do so, uncomment the following line.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

SOURCES += \
    main.cpp \
    mainwindow.cpp

HEADERS += \
    mainwindow.h

FORMS += \
    mainwindow.ui

# Default rules for deployment.
qnx: target.path = /tmp/$${TARGET}/bin
else: unix:!android: target.path = /opt/$${TARGET}/bin
!isEmpty(target.path): INSTALLS += target


INCLUDEPATH +=/usr/include/opencv4\
/usr/include/opencv4/opencv2

LIBS +=/usr/lib/arm-linux-gnueabihf/libopencv_*.so
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include 
#include 
#include 
#include 


#include 
#include 


using namespace cv;

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    , ui(new Ui::MainWindow)
{
    ui->setupUi(this);
}

MainWindow::~MainWindow()
{
    delete ui;
}


void MainWindow::on_pushButton_clicked()
{

    // 读取图像文件
//    Mat image = imread("1.jpg");

//    // 将OpenCV图像转换为Qt图像类型
//    QImage qImage(image.data, image.cols, image.rows, QImage::Format_RGB888);
//    QPixmap qPixmap = QPixmap::fromImage(qImage);
//    ui->label->setPixmap(qPixmap);

    // 创建窗口和标签来显示图像
   // QWidget window;
   // QLabel label(this);

//    label.setPixmap(qPixmap);
   // window.setCentralWidget(&label);
    //window.show();


   // Mat image = imread("1.jpg");

    // 将OpenCV图像转换为Qt图像类型
//    QImage qImage(image.data, image.cols, image.rows, QImage::Format_RGB888);
//    QPixmap qPixmap = QPixmap::fromImage(qImage);
//    ui->label->setPixmap(qPixmap);

    QString fileName = QFileDialog::getOpenFileName(this, "选择图片", "", "Images (*.jpg *.png)");

          if (!fileName.isEmpty()) {
              // 读取OpenCV图片
              cv::Mat image = cv::imread(fileName.toUtf8().constData(), cv::IMREAD_COLOR);

              if (!image.empty()) {
                  // 将OpenCV图片转换为Qt格式
                  QImage qtImage = QImage((const unsigned char*)(image.data), image.cols, image.rows, image.cols * image.channels(), QImage::Format_RGB888);

                  // 显示图片在标签中
                  ui->label->setPixmap(QPixmap::fromImage(qtImage));
                  //imageLabel->setPixmap(QPixmap::fromImage(qtImage));
              } else {
                  qDebug() << "Failed to load image:" << fileName;
              }
          }

}


2.测试

2.1opencv 调用摄像头发现调用失败反复查找问题,

2.1.1查看摄像头所有参数指令
ls /dev/video*
v4l2-ctl --info -d /dev/video0 --list-formats-ext

查看结果

pi@raspberrypi:~ $ s /dev/video*
bash: s: command not found
pi@raspberrypi:~ $  /dev/video*
bash: /dev/video0: Permission denied
pi@raspberrypi:~ $ ls /dev/video*
/dev/video0   /dev/video12  /dev/video15  /dev/video19  /dev/video22
/dev/video10  /dev/video13  /dev/video16  /dev/video20  /dev/video23
/dev/video11  /dev/video14  /dev/video18  /dev/video21  /dev/video31
pi@raspberrypi:~ $ v4l2-ctl --info -d /dev/video0 --list-formats-ext
Driver Info:
	Driver name      : unicam
	Card type        : unicam
	Bus info         : platform:fe801000.csi
	Driver version   : 6.1.21
	Capabilities     : 0xa5a00001
		Video Capture
		Metadata Capture
		Read/Write
		Streaming
		Extended Pix Format
		Device Capabilities
	Device Caps      : 0x25200001
		Video Capture
		Read/Write
		Streaming
		Extended Pix Format
Media Driver Info:
	Driver name      : unicam
	Model            : unicam
	Serial           : 
	Bus info         : platform:fe801000.csi
	Media version    : 6.1.21
	Hardware revision: 0x00000000 (0)
	Driver version   : 6.1.21
Interface Info:
	ID               : 0x03000005
	Type             : V4L Video
Entity Info:
	ID               : 0x00000003 (3)
	Name             : unicam-image
	Function         : V4L2 I/O
	Flags         : default
	Pad 0x01000004   : 0: Sink
	  Link 0x02000007: from remote pad 0x1000002 of entity 'ov5647 10-0036': Data, Enabled, Immutable
ioctl: VIDIOC_ENUM_FMT
	Type: Video Capture

	[0]: 'YUYV' (YUYV 4:2:2)
		Size: Stepwise 16x16 - 16376x16376 with step 1/1
	[1]: 'UYVY' (UYVY 4:2:2)
		Size: Stepwise 16x16 - 16376x16376 with step 1/1
	[2]: 'YVYU' (YVYU 4:2:2)
		Size: Stepwise 16x16 - 16376x16376 with step 1/1
	[3]: 'VYUY' (VYUY 4:2:2)
		Size: Stepwise 16x16 - 16376x16376 with step 1/1
	[4]: 'RGBP' (16-bit RGB 5-6-5)
		Size: Stepwise 16x16 - 16376x16376 with step 1/1
	[5]: 'RGBR' (16-bit RGB 5-6-5 BE)
		Size: Stepwise 16x16 - 16376x16376 with step 1/1
	[6]: 'RGBO' (16-bit A/XRGB 1-5-5-5)
		Size: Stepwise 16x16 - 16376x16376 with step 1/1
	[7]: 'RGBQ' (16-bit A/XRGB 1-5-5-5 BE)
		Size: Stepwise 16x16 - 16376x16376 with step 1/1
	[8]: 'RGB3' (24-bit RGB 8-8-8)
		Size: Stepwise 16x16 - 16376x16376 with step 1/1
	[9]: 'BGR3' (24-bit BGR 8-8-8)
		Size: Stepwise 16x16 - 16376x16376 with step 1/1
	[10]: 'RGB4' (32-bit A/XRGB 8-8-8-8)
		Size: Stepwise 16x16 - 16376x16376 with step 1/1
	[11]: 'BA81' (8-bit Bayer BGBG/GRGR)
		Size: Stepwise 16x16 - 16376x16376 with step 1/1
	[12]: 'GBRG' (8-bit Bayer GBGB/RGRG)
		Size: Stepwise 16x16 - 16376x16376 with step 1/1
	[13]: 'GRBG' (8-bit Bayer GRGR/BGBG)
		Size: Stepwise 16x16 - 16376x16376 with step 1/1
	[14]: 'RGGB' (8-bit Bayer RGRG/GBGB)
		Size: Stepwise 16x16 - 16376x16376 with step 1/1
	[15]: 'pBAA' (10-bit Bayer BGBG/GRGR Packed)
		Size: Stepwise 16x16 - 16376x16376 with step 1/1
	[16]: 'BG10' (10-bit Bayer BGBG/GRGR)
		Size: Stepwise 16x16 - 16376x16376 with step 1/1
	[17]: 'pGAA' (10-bit Bayer GBGB/RGRG Packed)
		Size: Stepwise 16x16 - 16376x16376 with step 1/1
	[18]: 'GB10' (10-bit Bayer GBGB/RGRG)
		Size: Stepwise 16x16 - 16376x16376 with step 1/1
	[19]: 'pgAA' (10-bit Bayer GRGR/BGBG Packed)
		Size: Stepwise 16x16 - 16376x16376 with step 1/1
	[20]: 'BA10' (10-bit Bayer GRGR/BGBG)
		Size: Stepwise 16x16 - 16376x16376 with step 1/1
	[21]: 'pRAA' (10-bit Bayer RGRG/GBGB Packed)
		Size: Stepwise 16x16 - 16376x16376 with step 1/1
	[22]: 'RG10' (10-bit Bayer RGRG/GBGB)
		Size: Stepwise 16x16 - 16376x16376 with step 1/1
	[23]: 'pBCC' (12-bit Bayer BGBG/GRGR Packed)
		Size: Stepwise 16x16 - 16376x16376 with step 1/1
	[24]: 'BG12' (12-bit Bayer BGBG/GRGR)
		Size: Stepwise 16x16 - 16376x16376 with step 1/1
	[25]: 'pGCC' (12-bit Bayer GBGB/RGRG Packed)
		Size: Stepwise 16x16 - 16376x16376 with step 1/1
	[26]: 'GB12' (12-bit Bayer GBGB/RGRG)
		Size: Stepwise 16x16 - 16376x16376 with step 1/1
	[27]: 'pgCC' (12-bit Bayer GRGR/BGBG Packed)
		Size: Stepwise 16x16 - 16376x16376 with step 1/1
	[28]: 'BA12' (12-bit Bayer GRGR/BGBG)
		Size: Stepwise 16x16 - 16376x16376 with step 1/1
	[29]: 'pRCC' (12-bit Bayer RGRG/GBGB Packed)
		Size: Stepwise 16x16 - 16376x16376 with step 1/1
	[30]: 'RG12' (12-bit Bayer RGRG/GBGB)
		Size: Stepwise 16x16 - 16376x16376 with step 1/1
	[31]: 'pBEE' (14-bit Bayer BGBG/GRGR Packed)
		Size: Stepwise 16x16 - 16376x16376 with step 1/1
	[32]: 'BG14' (14-bit Bayer BGBG/GRGR)
		Size: Stepwise 16x16 - 16376x16376 with step 1/1
	[33]: 'pGEE' (14-bit Bayer GBGB/RGRG Packed)
		Size: Stepwise 16x16 - 16376x16376 with step 1/1
	[34]: 'GB14' (14-bit Bayer GBGB/RGRG)
		Size: Stepwise 16x16 - 16376x16376 with step 1/1
	[35]: 'pgEE' (14-bit Bayer GRGR/BGBG Packed)
		Size: Stepwise 16x16 - 16376x16376 with step 1/1
	[36]: 'GR14' (14-bit Bayer GRGR/BGBG)
		Size: Stepwise 16x16 - 16376x16376 with step 1/1
	[37]: 'pREE' (14-bit Bayer RGRG/GBGB Packed)
		Size: Stepwise 16x16 - 16376x16376 with step 1/1
	[38]: 'RG14' (14-bit Bayer RGRG/GBGB)
		Size: Stepwise 16x16 - 16376x16376 with step 1/1
	[39]: 'GREY' (8-bit Greyscale)
		Size: Stepwise 16x16 - 16376x16376 with step 1/1
	[40]: 'Y10P' (10-bit Greyscale (MIPI Packed))
		Size: Stepwise 16x16 - 16376x16376 with step 1/1
	[41]: 'Y10 ' (10-bit Greyscale)
		Size: Stepwise 16x16 - 16376x16376 with step 1/1
	[42]: 'Y12P' (12-bit Greyscale (MIPI Packed))
		Size: Stepwise 16x16 - 16376x16376 with step 1/1
	[43]: 'Y12 ' (12-bit Greyscale)
		Size: Stepwise 16x16 - 16376x16376 with step 1/1
	[44]: 'Y14P' (14-bit Greyscale (MIPI Packed))
		Size: Stepwise 16x16 - 16376x16376 with step 1/1
	[45]: 'Y14 ' (14-bit Greyscale)
		Size: Stepwise 16x16 - 16376x16376 with step 1/1

三、接着测试

本来以为很ok了,结果还是遇到了大问题;
调用:
videocapture =new VideoCapture(0, cv::CAP_V4L2);

1.出现问题1:

open VIDEOIO(V4L2:/dev/video0): can‘t open camera by index

解决:
参考链接:

https://blog.csdn.net/u011304078/article/details/132740921

输入: ls /dev/video* -l    
  1. 这个是权限问题,直接通过chmod修改摄像头的权限,如果摄像头挂载到系统的设备名是/dev/video0,输入如下命令后,问题得到解决。
sudo chmod 777 /dev/video0

2.中途去开启了摄像头做了重启

打开摄像头模块
1、在树莓派终端中输入以下命令,打开配置:

sudo raspi-config

【树莓派c++图像处理起航1】_第11张图片
【树莓派c++图像处理起航1】_第12张图片

【树莓派c++图像处理起航1】_第13张图片
【树莓派c++图像处理起航1】_第14张图片

3.效果如图所示:

【树莓派c++图像处理起航1】_第15张图片
Qt 安装好后的软件展示
【树莓派c++图像处理起航1】_第16张图片

总结

学习框架和测试,用一种不用编译opencv源码的方式,成功的将Qt和opencv 用c++的方式运行了起来,写了基本图像处理的框架,接下来将测试摄像头的参数和相关设置

你可能感兴趣的:(树莓派,图像处理,人工智能)