树莓派3B--打开双目摄像头

一、介绍

1、淘宝上有专门卖树莓派专用的双目摄像头。

2、在树莓派上打开双目摄像头,可以用Python + OpenCV,也可以用Qt + OpenCV + C++。

3、博主直接在Qt 中调用 OpenCV,然后读取双目摄像头,然后显示出来。

 

二、环境

1、树莓派3b;

2、树莓派双目摄像头;

3、Qt 5.6;

4、OpenCV3.2。

 

三、工程代码

1、camera.h

#ifndef CAMERA_H
#define CAMERA_H

#include <QWidget>
#include <QTimer>
#include <QImage>
#include <QLabel>
#include <opencv2/opencv.hpp>

namespace Ui {
class Camera;
}

class Camera : public QWidget
{
    Q_OBJECT

public:
    explicit Camera(QWidget *parent = 0);
    ~Camera();
    void camera_open();
    QImage Mat2QImage(cv::Mat& cvImg);
    QImage ScaleImage2Label(QImage qImage, QLabel* qLabel);

private slots:
    void readFrame();

private:
    Ui::Camera *ui;
    QTimer *timer;
    QImage image;
    cv::VideoCapture cap;
    cv::Mat frame;
};

#endif // CAMERA_H

2、camera.cpp

#include "camera.h"
#include "ui_camera.h"

Camera::Camera(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::Camera)
{
    ui->setupUi(this);
    timer = new QTimer(this);
    /*信号和槽*/
    connect(timer, SIGNAL(timeout()), this, SLOT(readFrame()));  // 时间到,读取当前摄像头信息
}

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

void Camera::readFrame()
{
    cap >> frame;
    image = Mat2QImage(frame);
    QImage scaleImage = ScaleImage2Label( image, ui->lb_display );   // 显示到label上
    ui->lb_display->setPixmap(QPixmap::fromImage(scaleImage));
    ui->lb_display->setAlignment(Qt::AlignCenter);
    ui->lb_display->show();
}

void Camera::camera_open()
{
    cap.open(0);
    cap.set(CV_CAP_PROP_FRAME_WIDTH, 1280);
    cap.set(CV_CAP_PROP_FRAME_HEIGHT, 480);
    timer->start(33);              // 开始计时,超时则发出timeout()信号
}

QImage Camera::Mat2QImage(cv::Mat& cvImg)
{
    QImage qImg;
    if(cvImg.channels()==3)                             //3 channels color image
    {

        cv::cvtColor(cvImg,cvImg,CV_BGR2RGB);
        qImg =QImage((const unsigned char*)(cvImg.data),
                    cvImg.cols, cvImg.rows,
                    cvImg.cols*cvImg.channels(),
                    QImage::Format_RGB888);
    }
    else if(cvImg.channels()==1)                    //grayscale image
    {
        qImg =QImage((const unsigned char*)(cvImg.data),
                    cvImg.cols,cvImg.rows,
                    cvImg.cols*cvImg.channels(),
                    QImage::Format_Indexed8);
    }
    else
    {
        qImg =QImage((const unsigned char*)(cvImg.data),
                    cvImg.cols,cvImg.rows,
                    cvImg.cols*cvImg.channels(),
                    QImage::Format_RGB888);
    }

    return qImg;
}

QImage Camera::ScaleImage2Label(QImage qImage, QLabel* qLabel)
{
    QImage qScaledImage;
    QSize qImageSize = qImage.size();
    QSize qLabelSize = qLabel->size();
    double dWidthRatio = 1.0*qImageSize.width() / qLabelSize.width();
    double dHeightRatio = 1.0*qImageSize.height() / qLabelSize.height();
    if (dWidthRatio>dHeightRatio)
    {
        qScaledImage = qImage.scaledToWidth(qLabelSize.width());
    }
    else
    {
        qScaledImage = qImage.scaledToHeight(qLabelSize.height());
    }
    return qScaledImage;
}

3、main.cpp

#include "camera.h"
#include <QApplication>

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    Camera w;
    w.camera_open();
    w.show();

    return a.exec();
}

4、.pro

#-------------------------------------------------
#
# Project created by Sen 2018-02-12T16:47:37
#
#-------------------------------------------------

QT       += core gui

greaterThan(QT_MAJOR_VERSION, 4): QT += widgets

TARGET = Camera
TEMPLATE = app

# The following define makes your compiler emit warnings if you use
# any feature of Qt which as been marked as 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


SOURCES += main.cpp\
        camera.cpp

HEADERS  += camera.h

FORMS    += camera.ui

INCLUDEPATH += /usr/include/opencv
INCLUDEPATH += /usr/include/opencv2

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

5、完整demo

(1)下载连接

 

四、效果展示

1、树莓派与传感器(都是自己买的,价格都蛮亲民的)

2、运行双目效果图

 

五、注意事项

1、双目的摄像头要选双目的分辨率

2、此双目的分辨率是1280*480或者是2560*720

 

致谢

1、树莓派实验室

 

 

 

展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 技术黑板 设计师: CSDN官方博客
应支付0元
点击重新获取
扫码支付

支付成功即可阅读