树莓派自带摄像头读取视频
以前没有接触的树莓派的时候觉得这货应该和在Ubuntu差不多,就先写完了程序,调试好了放到树莓派上运行的时候,结果死活不能得到视频一帧,在网上找了很多资料,整理如下。
*可以shell脚本,如若不会百度,也可以把命令直接敲进终端(#!/bin/bash是shell脚本的一部分,不必敲到终端)*
opencv 安装
下载你想安装的版本,解压进入目录文件,执行以下命令进行编译(脚本执行更方便)
#!/bin/bash
mkdir release
cd release
cmake -D CMAKE_BUILD_TYPE=RELEASE -DCMAKE_INSTALL_PREFIX=/usr/local ..
make
sudo make install
echo
‘PKG_CONFIG_PATH=$PKG_CONFIG_PATH:/usr/local/lib/pkgconfig
export PKG_CONFIG_PATH ’ >> /etc/bash.bashrc
方法(测试可行)
使用三方库解决
-
安装GTK
-
安装Cmake
-
安装pkg-config
-
安装Numpy
#!/bin/bash
apt-get install libgtk2.0-dev
apt-get install cmake
apt-get install pkg-config
apt-get install Python-numpy
最后编译的时候两种方式
1 cmke方式
2 gcc/g++方式
我比较喜欢后者,现给出后者的编译方式
++ . - - -- --
以上是OpenCV安装过程,接下来就是树莓派自带摄像头三方库的安装
#!/bin/bash
cd raspicam
mkdir build
cd build
cmake ..
make
sudo make install
sudo ldconfig
上面一切顺利的话,接下来就可以对你的环境进行测试了。
测试程序如下:
#include <ctime>
#include <iostream>
#include <raspicam/raspicam_cv.h>
using namespace std;
int main ( int argc,char **argv ) {
time_t timer_begin,timer_end;
raspicam::RaspiCam_Cv Camera;
cv::Mat image;
int nCount=100;
Camera.set( CV_CAP_PROP_FORMAT, CV_8UC1 );
cout<<"Opening Camera..."<<endl;
if (!Camera.open()) {cerr<<"Error opening the camera"<<endl;return -1;}
cout<<"Capturing "<<nCount<<" frames ...."<<endl;
time ( &timer_begin );
for ( int i=0; i<nCount; i++ ) {
Camera.grab();
Camera.retrieve ( image);
if ( i%5==0 ) cout<<"\r captured "<<i<<" images"<<std::flush;
}
cout<<"Stop camera..."<<endl;
Camera.release();
time ( &timer_end );
double secondsElapsed = difftime ( timer_end,timer_begin );
cout<< secondsElapsed<<" seconds for "<< nCount<<" frames : FPS = "<< ( float ) ( ( float ) ( nCount ) /secondsElapsed ) <<endl;
cv::imwrite("raspicam_cv_image.jpg",image);
cout<<"Image saved at raspicam_cv_image.jpg"<<endl;
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
编译:g++ test_cv.cpp -o test_cv -I/usr/local/include/ -lraspicam_cv
pkg-config –cflags –libs opencv