Jade Dungeon

树莓派安装OpenCV

安装摄像头

如果你有树莓派官方的摄像头Picamera,需按如下方法正确配置 (如果没有官方摄像头,也不影响安装)。/etc/modules在这个文件末尾添加一行:

bcm2835-v4l2

测试摄像头:

vcgencmd get_camera

如果得到vcgencmd get_camera的结果,则证明摄像头连接成功。

测试拍照:

raspistill -o image.jpg

安装OpenCV

使用老的3.2源里就有:

sudo apt-get install libopencv-contrib-dev libopencv-dev             \
  libopencv-features2d-dev libopencv-ml-dev libopencv-objdetect-dev  \
  python3-opencv libopencv3.2-java libopencv3.2-jni                  \
  opencv-data opencv-doc 

新的版本如3.4.11就需要编译:

安装OpenCV信赖的库:

sudo apt-get install build-essential git cmake pkg-config -y
sudo apt-get install libjpeg8-dev -y
sudo apt-get install libtiff5-dev -y
sudo apt-get install libjasper-dev -y
sudo apt-get install libpng12-dev -y

sudo apt-get install libavcodec-dev libavformat-dev libswscale-dev libv4l-dev -y

sudo apt-get install libgtk2.0-dev -y
sudo apt-get install libatlas-base-dev gfortran -y

下载OpenCV源代码:

wget https://github.com/Itseez/opencv/archive/3.4.11.zip
wget https://github.com/Itseez/opencv_contrib/archive/3.4.11.zip

解压为指定的目录结构编译:

/home/pi/opencv-build/           # 环境起点
	+-> opencv-3.4.11/             # OpenCV 源代码位置
	+-> opencv_contrib-3.4.11/     # OpenCV_Contrib 源代码位置
	`-> build/                     # 执行编译的目录

执行编译

cd /home/pi/opencv-build/build

cmake ../opencv-3.4.11/ -D CMAKE_INSTALL_PREFIX=/usr/local         \
  -D OPENCV_EXTRA_MODULES_PATH=/home/pi/opencv-build/opencv_contrib-3.4.11/modules \
  -D CMAKE_BUILD_TYPE=RELEASE                                      \
  -D WITH_LIBV4L=ON                                                \
  -D PYTHON_LIBRARY=/usr/lib/arm-linux-gnueabihf/libpython3.7m.so  \
  -D PYTHON_INCLUDE_DIR=/usr/include/python3.7                     \
  -D PYTHON3_EXECUTABLE=/usr/bin/python3                           \
  -D PYTHON3_NUMPY_INCLUDE_DIRS=/usr/lib/python3/dist-packages/numpy/core/include \
	-D BUILD_EXAMPLES=ON                                             \
	-D INSTALL_C_EXAMPLES=ON                                         \
	-D INSTALL_PYTHON_EXAMPLES=ON  
INSTALL_C_EXAMPLES
make -j2

如果遇到错误提示:

~/opencv_contrib/modules/xfeatures2d/src/boostdesc.cpp:673:20: fatal error: 
boostdesc_bgm.i: No such file or directory

那应该是遇到了网络问题,临时下载的一文件没有下载下来。

解决方法:

下载别人已经下载好的:boostdesc_bgm.i,vgg_generated_48.i等.rar。 (我已经下载好放在opencv源代码一起了)

下载后,直接拷贝源码并生存同名文件, 放在opencv_contrib/modules/xfeatures2d/src/路径下即可。

cmake成功后,就可以安装了:

sudo make install

测试安装是否成功,在python3交互命令行中输入:

import cv2
cv2.__version__

进一步测试,画一个OpenCV的Logo出来:

import numpy as np
import cv2
import math

i_radius = 30   # 三个圈的内径
o_radius = 70   # 三个圈的外径
ang      = 60   # 三个圈缺口的角度

txt_font = cv2.FONT_HERSHEY_DUPLEX # 字体
thickness = -1                     # 无边框

# 三个圈的圆心所在的位置
cc_red   = (450, 172)
cc_green = (int(cc_red[0] - 170 / 2), cc_red[1] + int(170 / 2 * math.sqrt(3)))
cc_blue  = (int(cc_red[0] + 170 / 2), cc_red[1] + int(170 / 2 * math.sqrt(3)))

# 定义要用到的颜色
red   = (  0,   0, 255)
green = (  0, 255,   0)
blue  = (255,   0,   0)
white = (255, 255, 255)

# 画布大小900x600,RGB三通道8位
img        = np.zeros((600, 900, 3), np.uint8)

img[:,:,0] = np.zeros([600,900]) + 255 # 每个像素B通道的值
img[:,:,1] = np.zeros([600,900]) + 255 # 每个像素G通道的值
img[:,:,2] = np.zeros([600,900]) + 255 # 每个像素R通道的值

# 画红绿蓝三个大圆
cv2.circle(img, cc_red,   o_radius, red,   thickness)
cv2.circle(img, cc_green, o_radius, green, thickness)
cv2.circle(img, cc_blue,  o_radius, blue,  thickness)

# 每个圈中间叠背景色的小圆,变成三个环
cv2.circle(img, cc_red,   i_radius, white, thickness)
cv2.circle(img, cc_green, i_radius, white, thickness)
cv2.circle(img, cc_blue,  i_radius, white, thickness)

# 每个环上加上缺口
cv2.ellipse(img, cc_red,   (o_radius,o_radius),          ang,   0, ang, white, thickness)
cv2.ellipse(img, cc_green, (o_radius,o_radius),    360 - ang,   0, ang, white, thickness)
cv2.ellipse(img, cc_blue,  (o_radius,o_radius), 360 -2 * ang, ang,   0, white, thickness)

# 加上文字
cv2.putText(img, text='OpenCV', org=(209, 494), fontFace=txt_font,
        fontScale=4, color=(0, 0, 0), thickness=10)

cv2.imwrite("opencv_logo.png", img)    # 输出到图片文件
# cv2.imshow("opencv_logo.png", img)   # 显示图片窗口 
# cv2.waitkey(0)                       # 等待按任意键
# cv2.destoryWindow("opencv_logo.png") # 关闭窗口

应该显示的效果:

40 pin

TODO: 树莓派上的编译目录我还没有删除,研究一下怎么打成dpkg包,关联好依赖。