- GCC 4.4.x or later
- CMake 2.6 or later
- Git
- GTK + 2.x or higher, including headers (libgtk2.0-dev)
- pkg-config
- Python
- Numpy
- ffmpeg or libav development packages: libavcodec-dev, libavformat-dev, libswscale-dev
Installation Environment
sudo apt-get install build-essential
sudo apt-get install cmake git libgtk2.0-dev pkg-config libavcodec-dev libavformat-dev libswscale-dev
sudo pip install numpy
These are the foreplay, next to business.
In the file you want to install opencv folder, enter
git clone https://github.com/opencv/opencv.git
We installed opencv source code from git
We enter opencv
Next steps so divided:
- Create a temporary folder, and enter
- cmake our opencv
- installation
mkdir release #创建一个临时文件夹
cd release #进入它
cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local .. #cmake我们的opencv
make
sudo make install#安装
There step in cmake -D CMAKE_BUILD_TYPE=RELEASE -D CMAKE_INSTALL_PREFIX=/usr/local ..
, we may have something to download for it, this time we can be manually downloaded from.
Here a link to http://blog.csdn.net/yiyuehuan/article/details/52951574 .
This installed the opencv environment, but if we need to use it in python interface, we also need to download something
Interface with python opencv
pip install numpy
pip install Matplotlib
pip install opencv-python
We need to download the three modules on it.
When we downloaded, you can use the opencv cv2 module for python programmed.
Let's make a simple face recognition, I saw http://blog.csdn.net/Marksinoberg/article/details/52443214 this blog to learn.
But some things have not fit now, and I am here to update it.
# coding:utf-8
import cv2
# 待检测的图片路径
img_path = '2.jpg'
Classifier_path = '/media/asahi/数据/所有下载/opencv-3.3.1/data/haarcascades/haarcascade_frontalface_default.xml'
#这个路径表示,我们下载的opencv中自带的haarcascade_frontalface_default特征集,用的时候只需
#把/media/asahi/数据/所有下载/opencv-3.3.1/,换成你自己的路径就行了
face_cascade = cv2.CascadeClassifier(Classifier_path)
# 读取图片
image = cv2.imread(img_path)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# 探测图片中的人脸
faces = face_cascade.detectMultiScale(
gray,
scaleFactor=1.15,
minNeighbors=5,
minSize=(5, 5),
flags=cv2.CASCADE_SCALE_IMAGE #因为现在opencv也升级成了3,所以cv2.cv也不存在了
)
for (x, y, w, h) in faces:
cv2.rectangle(image,(x,y),(x+w,y+w),(0,255,0),2)
# cv2.circle(image, ((x + x + w) / 2,
# (y + y + h) / 2),
# w / 2,
# (0, 255, 0),
# float(2))
cv2.imshow("Find Faces!", image)
cv2.waitKey(0)
There is also a pit, Ubuntu bloggers here are also equipped with ROS system, if run directly to the error.
such as
Traceback (most recent call last):
File "/media/asahi/办公/work/py/learning_opencv/learning_01.py", line 10, in <module>
import cv2
ImportError: /opt/ros/kinetic/lib/python2.7/dist-packages/cv2.so: undefined symbol: PyCObject_Type
Checked, ros should be engaged in something. I have no better way can it /.bashrc the last link to the ros statement to the Notes.
Then restart the terminal, you can run.