28、新手入手樹莓派教程–開啟雙目攝像頭

28、新手入手樹莓派教程–開啟雙目攝像頭

一、介紹

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、樹莓派實驗室