Jetson Nano使用CSI摄像头教程(c++)

一、 人脸检测

C++下开发Opencv需要进行一些额外的配置,先看一下opencv的文件位置。Jetson Nano预装的Opencv4.1.1的头文件位置如下图所示:

库文件位置如下图所示:

只需要在Qt的pro文件中将上述两个目录包含进来。另外注意头文件和lib文件的添加方法。
QT的pro文件如下:

QT -= gui
 
CONFIG += c++11 console
CONFIG -= app_bundle
CONFIG += C++11  # 添加对C++11的支持
 
# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS
 
# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0
 
INCLUDEPATH += /usr/include/opencv4 #添加头文件路径
 
LIBS += -L/usr/lib/aarch64-linux-gnu -lopencv_core -lopencv_imgcodecs -lopencv_imgproc -lopencv_highgui -lopencv_objdetect  #添加需要链接的库
 
 
SOURCES += main.cpp

重新生成整个项目,然后将test.jpeg和haarcascade_frontalface_default.xml文件放置在编译生成的build-QTtest-unknown-Debug文件夹中,运行项目效果图如下所示:

二、读取CSI摄像头

使用C++编程读取CSI摄像头,可以看到已经可以正常的显示视频流图像了,但是由于树莓派摄像头本身的原因,其图像中还有很多的噪点,颜色也有些失真(真实工业场景中建议购买更好的摄像头)。C++版本的main.cpp文件如下:


#include <iostream>
#include <string>
#include <opencv4/opencv2/opencv.hpp>
#include <opencv4/opencv2/core.hpp>
#include <opencv4/opencv2/highgui.hpp>
#include <opencv4/opencv2/imgproc.hpp>
#include <opencv4/opencv2/objdetect.hpp>
#include <opencv4/opencv2/imgproc/types_c.h>
#include <opencv4/opencv2/videoio.hpp>

using namespace std;
using namespace cv;

string gstreamer_pipeline (int capture_width, int capture_height, int display_width, int display_height, int framerate, int flip_method)
{
    return "nvarguscamerasrc ! video/x-raw(memory:NVMM), width=(int)" + to_string(capture_width) + ", height=(int)" +
           to_string(capture_height) + ", format=(string)NV12, framerate=(fraction)" + to_string(framerate) +
           "/1 ! nvvidconv flip-method=" + to_string(flip_method) + " ! video/x-raw, width=(int)" + to_string(display_width) + ", height=(int)" +
           to_string(display_height) + ", format=(string)BGRx ! videoconvert ! video/x-raw, format=(string)BGR ! appsink";
}

int main( int argc, char** argv )
{
    int capture_width = 1280 ;
    int capture_height = 720 ;
    int display_width = 1280 ;
    int display_height = 720 ;
    int framerate = 60 ;
    int flip_method = 0 ;

    //创建管道
    string pipeline = gstreamer_pipeline(capture_width,
    capture_height,
    display_width,
    display_height,
    framerate,
    flip_method);
    std::cout << "使用gstreamer管道: \n\t" << pipeline << "\n";

    //管道与视频流绑定
    VideoCapture cap(pipeline, CAP_GSTREAMER);
    if(!cap.isOpened())
    {
        std::cout<<"打开摄像头失败."<<std::endl;
        return (-1);
    }

    //创建显示窗口
    namedWindow("CSI Camera", WINDOW_AUTOSIZE);
    Mat img;

    //逐帧显示
    while(true)
    {
        if (!cap.read(img))
        {
            std::cout<<"捕获失败"<<std::endl;
            break;
        }
        int new_width,new_height,width,height,channel;
            width=img.cols;
            height=img.rows;
            channel=img.channels();


        //调整图像大小
        new_width=640;
        if(width>800)
          {
             new_height=int(new_width*1.0/width*height);
           }
         resize(img, img, cv::Size(new_width, new_height));

        imshow("CSI Camera",img);

        int keycode = cv::waitKey(30) & 0xff ; //ESC键退出
            if (keycode == 27) break ;
    }

    cap.release();
    destroyAllWindows() ;
}

其中需要额外的添加opencv用于视频处理的头文件#include <opencv4/opencv2/videoio.hpp>。另外,还需要修改对pro文件,将视频处理对应的opencv_videoio库包含进来,完整的pro文件如下:

QT -= gui

CONFIG += c++11 console
CONFIG -= app_bundle
CONFIG += C++11  # 添加对C++11的支持

# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked deprecated (the exact warnings
# depend on your compiler). Please consult the documentation of the
# deprecated API in order to know how to port your code away from it.
DEFINES += QT_DEPRECATED_WARNINGS

# You can also make your code fail to compile if you use deprecated APIs.
# In order to do so, uncomment the following line.
# You can also select to disable deprecated APIs only up to a certain version of Qt.
#DEFINES += QT_DISABLE_DEPRECATED_BEFORE=0x060000    # disables all the APIs deprecated before Qt 6.0.0

INCLUDEPATH += /usr/include/opencv4 #添加头文件路径

LIBS += -L/usr/lib/aarch64-linux-gnu -lopencv_core -lopencv_imgcodecs -lopencv_imgproc -lopencv_highgui -lopencv_objdetect -lopencv_videoio  #添加需要链接的库


SOURCES += main.cpp

三、二维码检测和识读

本小节将使用Opencv实现二维码检测和识读功能。在opencv4.0以后,已经集成了二维码识读模块,因此,我们可以采用最新的opencv来实现二维码检测和识读。二维码检测和识别主要分为3步:使用QRCodeDetector()函数创建二维码检测器;使用detectAndDecode函数对图像进行二维码检测和识别;将检测结果输出。main.cpp文件如下


#include <iostream>
#include <string>
#include <opencv4/opencv2/opencv.hpp>
#include <opencv4/opencv2/core.hpp>
#include <opencv4/opencv2/highgui.hpp>
#include <opencv4/opencv2/imgproc.hpp>
#include <opencv4/opencv2/objdetect.hpp>
#include <opencv4/opencv2/imgproc/types_c.h>
#include <opencv4/opencv2/videoio.hpp>

using namespace std;
using namespace cv;

string gstreamer_pipeline (int capture_width, int capture_height, int display_width, int display_height, int framerate, int flip_method)
{
    return "nvarguscamerasrc ! video/x-raw(memory:NVMM), width=(int)" + to_string(capture_width) + ", height=(int)" +
           to_string(capture_height) + ", format=(string)NV12, framerate=(fraction)" + to_string(framerate) +
           "/1 ! nvvidconv flip-method=" + to_string(flip_method) + " ! video/x-raw, width=(int)" + to_string(display_width) + ", height=(int)" +
           to_string(display_height) + ", format=(string)BGRx ! videoconvert ! video/x-raw, format=(string)BGR ! appsink";
}

int main( int argc, char** argv )
{
    int capture_width = 1280 ;
    int capture_height = 720 ;
    int display_width = 1280 ;
    int display_height = 720 ;
    int framerate = 60 ;
    int flip_method = 0 ;

    //创建管道
    string pipeline = gstreamer_pipeline(capture_width,
    capture_height,
    display_width,
    display_height,
    framerate,
    flip_method);
    std::cout << "使用gstreamer管道: \n\t" << pipeline << "\n";

    //管道与视频流绑定
    VideoCapture cap(pipeline, CAP_GSTREAMER);
    if(!cap.isOpened())
    {
        std::cout<<"打开摄像头失败."<<std::endl;
        return (-1);
    }

    //创建显示窗口
    namedWindow("CSI Camera", WINDOW_AUTOSIZE);
    Mat img;
    //创建二维码检测器
    QRCodeDetector qrDecoder = QRCodeDetector();

    //逐帧显示
    while(true)
    {
        if (!cap.read(img))
        {
            std::cout<<"捕获失败"<<std::endl;
            break;
        }
        int new_width,new_height,width,height,channel;
            width=img.cols;
            height=img.rows;
            channel=img.channels();


        //调整图像大小
        new_width=640;
        if(width>800)
          {
             new_height=int(new_width*1.0/width*height);
           }
         resize(img, img, cv::Size(new_width, new_height));
         
         //二维码检测和识读
         Mat bbox, rectifiedImage;
         std::string data = qrDecoder.detectAndDecode(img, bbox, rectifiedImage);
         if(data.length()>0)
         {
            cout << "解码数据: " << data << endl;

                  int n = bbox.rows;
                  for(int i = 0 ; i < n ; i++)
                  {
                      line(img, Point2i(bbox.at<float>(i,0),bbox.at<float>(i,1)), Point2i(bbox.at<float>((i+1) % n,0), bbox.at<float>((i+1) % n,1)), Scalar(255,0,0), 3);
                  }
                 }
          else
              cout << "没有检测到二维码" << endl;


        imshow("CSI Camera",img);

        int keycode = cv::waitKey(30) & 0xff ; //ESC键退出
            if (keycode == 27) break ;
    }

    cap.release();
    destroyAllWindows() ;
}

  • 6
    点赞
  • 90
    收藏
    觉得还不错? 一键收藏
  • 7
    评论
### 回答1: Jetson Nano可以使用CSI摄像头CSI摄像头是一种专门为Jetson Nano设计的摄像头,可以通过CSI接口连接到Jetson Nano上。使用CSI摄像头可以实现高清晰度的图像和视频采集,适用于机器视觉、智能家居、智能安防等领域。在使用CSI摄像头时,需要先安装相应的驱动和软件,然后通过代码控制摄像头进行图像和视频采集。 ### 回答2: Jetson Nano是一款由英伟达推出的低功耗、高性能的开发板,该开发板具有强大的图像处理功能,可以很好地与CSI摄像机配合使用,从而实现图像采集、处理和分析等一系列操作。 CSI摄像头是一种接口标准,也就是说,其和Jetson Nano之间的连接采用的是CSI接口,因此,在使用CSI摄像头之前,你需要确定摄像头的接口类型和支持的分辨率等参数,以确保与Jetson Nano芯片的兼容性。 通常情况下,Jetson Nano可以支持多种不同品牌和型号的CSI摄像头,并且可以通过软件进行配置和管理。在使用CSI摄像头时,你需要先安装支持CSI接口的驱动程序,然后在系统中启用该摄像头,并确认其工作状态和连接方式。你可以使用JetPack中提供的NVIDIA Jetson Nano Developer Kit使得这些过程变得更加轻松。 一旦设备连接起来,并且系统能够识别它,你就可以开始采集图像和视频信号了。此时,你可以使用各种开源工具和框架,如OpenCV和TensorFlow等,对图像数据进行处理和分析。这些工具和框架都可以很好地兼容Jetson Nano,并且可以利用其强大的计算能力和GPU加速性能,进行快速而准确的图像处理和分析。 总之,Jetson Nano与CSI摄像头的完美结合,让人们可以更加便捷地进行图像和视频应用的开发和部署,同时也为物联网和人工智能等领域的应用提供了一种高效、低成本的解决方案。 ### 回答3: Jetson Nano是一款基于NVIDIA Jetson平台的低成本的人工智能开发板,它可以用于机器人、智能家居、自动驾驶等场景。其核心是NVIDIA Maxwell™架构GPU,拥有128个CUDA®核和4GB LPDDR4内存,性能非常出色。 Jetson Nano支持使用CSI摄像头,这是一种串行接口摄像头CSI(Camera Serial Interface)接口与传统的摄像头接口不同,它可以传输大量数据和控制信号,因此能够支持高分辨率图像。CSI摄像头可以提供高质量的图像,使得机器学习和计算机视觉的应用更加高效和精准。 对于Jetson Nano开发板而言,CSI接口是用于连接CSI摄像头的主要接口。Jetson Nano的CSI摄像头口是一个15针的FPC连接器,这里需要注意的是CSI摄像头的连接必须正确无误,如果连接错误或者松动,可能会导致图像数据传输失败甚至损坏开发板。因此,在安装CSI摄像头时需要特别注意这个问题。 此外,在Jetson Nano上使用CSI摄像头还需要进行相应的驱动编译和配置。Jetson Nano支持多种类型的CSI摄像头,包括IMX219、IMX219-77、IMX185、OV5670等,用户可以根据自己的需求选择合适的摄像头使用CSI摄像头时还需要进行相应的软件开发,比如使用OpenCV等图像处理库,通过编程实现相应的图像处理和识别算法。 总之,Jetson Nano是一个高性能、低成本、开放式的人工智能开发板,支持使用CSI摄像头,能够为机器学习和深度学习等应用提供高质量的图像输入,为人工智能应用开发提供极大的便利。
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值