sudo raspi-config
执行:
raspistill -v -o test.jpg
实际测试openCV在1080P模式下图像异常,原因不明。
方法一(不推荐,openCV版本为2.4,太旧):
执行:
sudo apt-get install libopencv-dev
方法二(推荐):
① 在openCV官网(https://opencv.org/releases/)下载3.4的源码:
② 源代码拷贝到树莓派,解压到opencv-3.4.6
③ 安装openCV相关工具
sudo apt-get install build-essential cmake git pkg-config
④ 安装openCV的图像工具包
sudo apt-get install libjpeg8-dev
sudo apt-get install libtiff5-dev
sudo apt-get install libjasper-dev
sudo apt-get install libpng12-dev
⑤ 安装视频I/O包
sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev
⑥ 安装gtk2.0和优化函数包
sudo apt-get install libgtk2.0-dev
sudo apt-get install libatlas-base-dev gfortran
⑦ 编译并安装openCV
cd opencv-3.4.6
mkdir release
cd release
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr ..
sudo make
sudo make install
sudo ldconfig
之后用rsync同步到上位机。
实测1280*960模式下正常。
① 下载源代码:https://sourceforge.net/projects/raspicam/files/
② 编译源代码,执行:
cd raspicam
mkdir build
cd build
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr ..
sudo make
sudo make install
sudo ldconfig
之后用rsync同步到上位机。
在pro文件中加入:
INCLUDEPATH += /home/raspberry/raspi/sysroot/usr/include \
/home/raspberry/raspi/sysroot/usr/include/opencv \
/home/raspberry/raspi/sysroot/usr/include/opencv2
LIBS += -L /home/raspberry/raspi/sysroot/usr/lib/ -lopencv_highgui -lopencv_core -lopencv_videoio
头文件:
#include "opencv2/opencv.hpp"
cv::Mat *cvImage = nullptr;
cv::VideoCapture *cvCamera = nullptr;
源文件:
void init()
{
cvCamera = new cv::VideoCapture(0);
if (!cvCamera->open(0))
{
QMessageBox::warning(this,QStringLiteral("错误"),QStringLiteral("打开摄像头失败"));
QApplication::quit();
}
//设置720P分辨率
//cvCamera.set(CV_CAP_PROP_FRAME_WIDTH, 1280);
//cvCamera.set(CV_CAP_PROP_FRAME_HEIGHT, 720);
//设置1080P分辨率
cvCamera.set(CV_CAP_PROP_FOURCC, CV_FOURCC('M','J','P','G'));
cvCamera.set(CV_CAP_PROP_FPS, 30);
cvCamera.set(CV_CAP_PROP_FRAME_WIDTH, 1920);
cvCamera.set(CV_CAP_PROP_FRAME_HEIGHT, 1080);
cvImage = new cv::Mat;
}
void capture()
{
if (cvCamera->isOpened())
{
QTime testTime;
//Capture
testTime.start();
*cvCamera >> *cvImage;
qDebug()<<"Capture: "<data, cvImage->cols, cvImage->rows, static_cast(cvImage->step),QImage::Format_RGB888).rgbSwapped().scaled(800,600,Qt::IgnoreAspectRatio, Qt::SmoothTransformation);
qDebug()<<"MatToQImage: "<labelCapture->setPixmap(QPixmap::fromImage(disImage));
qDebug()<<"Begin: "<
附:Windows下openCV的使用(64位)
① 在工程目录中新建目录opencv。
② 安装openCV后,将 openCV安装目录/build/include 目录复制到 工程目录/opencv/ 中。
③ 在工程目录/opencv/中新建lib文件夹,将 openCV安装目录/build/x64/vc14/lib 中的两个lib文件复制到其中。
④ 在 工程目录/opencv/中新建bin文件夹,将 openCV安装目录/build/x64/vc14/bin 中的 opencv_world410.dll , opencv_world410.pdb , opencv_world410d.dll , opencv_world410d.pdb 复制到其中。
⑤ pro文件添加:
INCLUDEPATH += $$PWD/opencv/include \
$$PWD/opencv/include/opencv2
CONFIG(debug, debug|release) {
LIBS += -L$$PWD/opencv/lib/ -lopencv_world410d
}
else {
LIBS += -L$$PWD/opencv/lib/ -lopencv_world410
}
6. 使用RaspiCam截图
pro文件:
INCLUDEPATH += /home/raspberry/raspi/sysroot/usr/include \
/home/raspberry/raspi/sysroot/usr/include/raspicam
LIBS += -L /home/raspberry/raspi/sysroot/usr/lib/ -lraspicam
cpp文件:
cvCamera.setCaptureSize(1280,960);
cvCamera.open();
cvCamera.grab();
unsigned char *data = new unsigned char[cvCamera.getImageTypeSize(raspicam::RASPICAM_FORMAT_RGB)];
cvCamera.retrieve(data);
QImage qImage(data, cvCamera.getWidth(), cvCamera.getHeight(), QImage::Format_RGB888);