Skip to content


In short,

$ pip install opencv-python


For example, the workflow of Cell-Video uses, which includes opencv-python.

OpenCV 2

installed with python 2

OpenCV 3

installed with python 3, but the imshow doesn’t work, solve by

conda install -c menpo opencv3

refer to OpenCV not working properly with python on Linux with anaconda. Getting error that cv2.imshow() is not implemented Ask Question

If necessary, install imutils: A series of convenience functions to make basic image processing operations such as translation, rotation, resizing, skeletonization, and displaying Matplotlib images easier with OpenCV and Python.

conda install -c conda-forge imutils

Currently I am mainly work with

(py37) $ conda list
opencv                    3.4.2            py37h6fd60c2_1    defaults
opencv-python                    pypi_0    pypi

which can be easily installed with

$ pip install opencv-python

pay attention to that import cv2; cv2.__version__ returns the version of opencv-python. The contrib packages can be installed with (see,

(py37) $ pip install opencv-contrib-python

Some bugs

error: OpenCV(3.4.2) /tmp/build/80754af9/opencv-suite_1535558553474/work/modules/imgproc/src/color.hpp:253: error: (-215:Assertion failed) VScn::contains(scn) && VDcn::contains(dcn) && VDepth::contains(depth) in function 'CvtHelper'

the image might be not read properly, such as wrong path.

OpenCV 4

build from source according to the official documentation

curious how the python distinguish different versions of opencv, does it affect the conda environment?

build from source by specifying something, refer to Buidling OpenCV with Conda on Linux and How to install OpenCV 4 on Ubuntu

  1. 这并不意味着只安装在具体 conda envs 中,而是 /usr/local 中,最后再 ln -s 过去。换句话说,安装时使用的 python 环境仅用于 build。
  2. ln -s 时python site-packages 下的 .so 文件必须重命名为,否则找不到!!
  3. 所有其实一开始指定 cmake,只是为了针对具体版本进行编译,最后还需要自己 link,参照 Compile OpenCV with Cmake to integrate it within a Conda env 设置 BUILD_opencv_python2=OFF

Currently (2020-07-27 21:32:18), I am not using this version, and the previous built conda env also has been deleted due to the upgrade of system. But the build folders (in ~/github/opencv4_build) and resulting .so (in /usr/local/lib/python3.7/site-packages/cv2/python-3.7) should still be OK since the beginning python is just for building.

PIL and Pillow

PIL is the Python Imaging Library, and Pillow is the friendly PIL fork. - 由于PIL仅支持到Python 2.7,加上年久失修,于是一群志愿者在PIL的基础上创建了兼容的版本,名字叫Pillow,支持最新Python 3.x,又加入了许多新特性 🔗


An example.

Read Image


Adopted from my project.

For a 16-bit TIFF images, the mode should be set as IMREAD_ANYDEPTH instead of IMREAD_GRAYSCALE.

>>> im1 = cv2.imread("example/three_cells.tif", cv2.IMREAD_ANYDEPTH)
>>> np.unique(im1)
array([ 0, 46, 51, 52, 53, 54, 55, 56], dtype=uint16)
>>> im2 = cv2.imread("../test/example/three_cells.tif", cv2.IMREAD_GRAYSCALE)
>>> np.unique(im2)
array([0], dtype=uint8)

while avoids such specification, which might be more convenience,

>>> im3 = io.imread("../test/example/three_cells.tif")
>>> np.unique(im3)
array([ 0, 46, 51, 52, 53, 54, 55, 56], dtype=uint16)

As a comparison, JuliaImages uses a special type NOf16 (standing for Normalized, with 0 integer bits and 16 fractional bits), that interprets an 16-bit integer as if it had been scaled by 1/(2^16-1), thus encoding values from 0 to 1 in 2^16 steps.

julia> im = load("../test/example/three_cells.tif")
# in jupyter notebook, direct `unique(im)` will automatically shows the image pixel
julia> display(MIME("text/plain"), sort(unique(im)))
8-element Array{Gray{N0f16},1} with eltype Gray{Normed{UInt16,16}}:


The result returned by np.unique has been sorted, while Julia’s unique does not.