Python 常用的图像处理库有 OpenCV-Python、PIL(Pillow) 、scikit-image、matplotlib。在这里记录下这四种库的安装过程,常用的读取、显示、保存操作,以及和 Numpy 的交互。
文章目录
1. OpenCV-Python
OpenCV 官网
OpenCV releases
OpenCV Documentation
OpenCV-Python Documentation
列出两个 OpenCV-Python 资源:
1.1 OpenCV-Python 安装
1.1.1 Ubuntu 下安装
Install OpenCV-Python in Ubuntu
Pre-built Binaries
OpenCV-Python 在 Ubuntu 下 Python2.7 / Python3 均支持。
sudo apt-get install python-opencv
import cv2 as cv
print(cv.__version__)
或者从源码编译
1.1.2 Windows 下安装
Install OpenCV-Python in Windows
Python2.7
OpenCV-Python 在 Windows 下只支持 Python2.7.
- 下载 Windows 平台的 OpenCV release 并解压.
- 进入
opencv/build/python/2.7
文件夹. - 将
cv2.pyd
拷贝至/Python27/lib/site-packages
目录下,即可.
验证安装是否成功:
# python2.7
>>> import cv2 as cv
>>> print( cv.__version__ )
Python3
要在 Windows 下的 Python3 使用 OpenCV-Python,可以使用第三方编译的 wheel
包, 点这里。
Unofficial Windows Binaries for Python Extension Packages
by Christoph Gohlke, Laboratory for Fluorescence Dynamics, University of California, Irvine.Ctrl + F 搜索 opencv
1. 以64为平台 OpenCV-Python-3.4.5 和 Python3.6 为例:
下载 opencv_python‑3.4.5‑cp36‑cp36m‑win_amd64.whl
2. 把原来文件名 opencv_python‑3.4.5‑cp36‑cp36m‑win_amd64.whl
中间的cp36m
变为none
,这样文件名改为:
opencv_python‑3.4.5‑cp36‑none‑win_amd64.whl
3. 打开cmd(window键+R,输入cmd并回车),进入 opencv_python‑3.4.5‑cp36‑none‑win_amd64.whl
所在文件夹,在命令行输入:
>pip3 install opencv_python‑3.4.5‑cp36‑none‑win_amd64.whl
验证安装是否成功
# python3.6
>>> import cv2 as cv
>>> print( cv.__version__ )
或者从源码编译
1.2 OpenCV-Python 基本使用
import cv2
1.2.1 读取图像 cv2.imread()
imread(filename[, flags])
. @param filename : Name of file to be loaded.
. @param flags : Flag that can take values of cv::ImreadModes
IMREAD_UNCHANGED ( -1 ) | If set, return the loaded image as is (with alpha channel, otherwise it gets cropped). |
IMREAD_GRAYSCALE ( 0 ) | If set, always convert image to the single channel grayscale image. |
IMREAD_COLOR ( 1 ) | If set, always convert image to the 3 channel BGR color image. |
IMREAD_ANYDEPTH ( 2 ) | If set, return 16-bit/32-bit image when the input has the corresponding depth, otherwise convert it to 8-bit. |
IMREAD_ANYCOLOR ( 4 ) | If set, the image is read in any possible color format. |
IMREAD_LOAD_GDAL | If set, use the gdal driver for loading the image. |
IMREAD_REDUCED_GRAYSCALE_2 | If set, always convert image to the single channel grayscale image and the image size reduced 1/2. |
IMREAD_REDUCED_COLOR_2 | If set, always convert image to the 3 channel BGR color image and the image size reduced 1/2. |
IMREAD_REDUCED_GRAYSCALE_4 | If set, always convert image to the single channel grayscale image and the image size reduced 1/4. |
IMREAD_REDUCED_COLOR_4 | If set, always convert image to the 3 channel BGR color image and the image size reduced 1/4. |
IMREAD_REDUCED_GRAYSCALE_8 | If set, always convert image to the single channel grayscale image and the image size reduced 1/8. |
IMREAD_REDUCED_COLOR_8 | If set, always convert image to the 3 channel BGR color image and the image size reduced 1/8. |
>>> type(cv2.IMREAD_COLOR)
<class 'int'>
>>> cv2.IMREAD_COLOR
1
调用 cv2.imread()
>>> img = cv2.imread('lena.jpg')
>>> img_grey = cv2.imread('lena.jpg', 0)
>>> img_color = cv2.imread('lena.jpg', 1)
cv2.imread() 返回的是 Numpy 数组
>>> type(img)
<class 'numpy.ndarray'>
flag 控制返回的通道数
>>> img.shape
(512, 512, 3)
>>> img_grey.shape
(512, 512)
>>> img_color.shape
(512, 512, 3)
通道,与像素深度深度有关。灰度图通常是8比特的像素深度,则通道数为1。如果是彩色图,且为RGB编码,那么一般为24比特的像素深度,通道数为3。而有的彩色图的像素深度是16或者32比特。16比特可能有多种情况:一是压缩的RGB格式,二是YUV的输出。无论何种,都是只有2通道,需要手动解析分离。32比特(windows *.bmp)的像素深度对应的彩色图,则表示的是4通道,RGBA,多出的A表示的是透明度的索引。
另外读取时需要注意内部像素的编码顺序,这也依赖于imread的flags选项的取值,如果取值决定转成RGB,那么正常的顺序是BGR。如果最后imread输出是四通道,多了Alpha通道,那么顺序是RGBA。
1.2.2 显示图像 cv2.imshow()
imshow(winname, mat)
. @param winname: Name of the window.
. @param mat: Image to be shown.
>>> cv2.imshow('img',img)
... cv2.waitKey(0)
... cv2.destroyAllWindows()
>>> cv2.imshow('img_grey',img_grey)
... cv2.waitKey(0)
... cv2.destroyAllWindows()
1.2.3. 保存图像 cv2.imwrite()
imwrite(filename, img[, params])
. @include snippets/imgcodecs_imwrite.cpp
. @param filename: Name of the file.
. @param img: Image to be saved.
. @param params: Format-specific parameters encoded as pairs (paramId_1, paramValue_1, paramId_2, paramValue_2, ... .) see cv::ImwriteFlags
IMWRITE_JPEG_QUALITY | For JPEG, it can be a quality from 0 to 100 (the higher is the better). Default value is 95. |
IMWRITE_JPEG_PROGRESSIVE | Enable JPEG features, 0 or 1, default is False. |
IMWRITE_JPEG_OPTIMIZE | Enable JPEG features, 0 or 1, default is False. |
IMWRITE_JPEG_RST_INTERVAL | JPEG restart interval, 0 - 65535, default is 0 - no restart. |
IMWRITE_JPEG_LUMA_QUALITY | Separate luma quality level, 0 - 100, default is 0 - don’t use. |
IMWRITE_JPEG_CHROMA_QUALITY | Separate chroma quality level, 0 - 100, default is 0 - don’t use. |
IMWRITE_PNG_COMPRESSION | For PNG, it can be the compression level from 0 to 9. A higher value means a smaller size and longer compression time. If specified, strategy is changed to IMWRITE_PNG_STRATEGY_DEFAULT (Z_DEFAULT_STRATEGY). Default value is 1 (best speed setting). |
IMWRITE_PNG_STRATEGY | One of cv::ImwritePNGFlags, default is IMWRITE_PNG_STRATEGY_RLE. |
IMWRITE_PNG_BILEVEL | Binary level PNG, 0 or 1, default is 0. |
IMWRITE_PXM_BINARY | For PPM, PGM, or PBM, it can be a binary format flag, 0 or 1. Default value is 1. |
IMWRITE_WEBP_QUALITY | For WEBP, it can be a quality from 1 to 100 (the higher is the better). By default (without any parameter) and for quality above 100 the lossless compression is used. |
IMWRITE_PAM_TUPLETYPE | For PAM, sets the TUPLETYPE field to the corresponding string value that is defined for the format. |
>>> cv2.imwrite("lena_grey.png", img_grey)
True
>>> cv2.imwrite("lena_grey2.png", img_grey, [int(cv2.IMWRITE_PNG_COMPRESSION), 5])
True
2. PIL(Pillow)
PIL(Python Imaging Library)是一个强大的图像处理库,事实上成为了 Python 的图形图像标准库。但它只支持到Python 2.7。
PIL官方网站
Pillow原本是PIL的一个分支,如今事实上已经替代PIL本身成为 Python 的图形图像标准库。
Pillow Github
Pillow Handbook
Pillow 中文文档
2.1 Pillow 安装
Pillow Installation
安装 Pillow 即可使用 PIL。Pillow 与 PIL 无法在同一环境下共存,卸载 PIL,安装 Pillow。
2.1.1 Ubuntu下安装
$ pip install Pillow
或者,预编译包
$ sudo apt-get install python-imaging
2.1.2 Windows下安装
> pip install Pillow
2.2 Pillow 基本使用
from PIL import Image
2.2.1 读取图像 Image.open()
open(fp, mode='r')
:param fp: A filename (string), pathlib.Path object or a file object.
:param mode: The mode. If given, this argument must be "r".
:returns: An :py:class:`~PIL.Image.Image` object.
>>> im = Image.open('lena.jpg')
>>> type(im)
<class 'PIL.PngImagePlugin.PngImageFile'>
2.2.2 显示图像 im.show()
| show(self, title=None, command=None)
| Displays this image. This method is mainly intended for
| debugging purposes.
|
| On Unix platforms, this method saves the image to a temporary
| PPM file, and calls the **display**, **eog** or **xv**
| utility, depending on which one can be found.
|
| On macOS, this method saves the image to a temporary PNG file, and
| opens it with the native Preview application.
|
| On Windows, it saves the image to a temporary BMP file, and uses
| the standard BMP display utility to show it (usually Paint).
|
| :param title: Optional title to use for the image window,
| where possible.
| :param command: command used to show the image
对一个 PIL.Image.Image 类的实例 im 调用 show() 方法,调用系统的默认软件显示图像.
>>> im.show()
等同于
>>> Image._show(im)
2.2.3 保存图像 im.save()
| save(self, fp, format=None, **params)
| Saves this image under the given filename. If no format is
| specified, the format to use is determined from the filename
| extension, if possible.
|
| Keyword options can be used to provide additional instructions
| to the writer. If a writer doesn't recognise an option, it is
| silently ignored. The available options are described in the
| :doc:`image format documentation
| <../handbook/image-file-formats>` for each writer.
|
| You can use a file object instead of a filename. In this case,
| you must always specify the format. The file object must
| implement the ``seek``, ``tell``, and ``write``
| methods, and be opened in binary mode.
|
| :param fp: A filename (string), pathlib.Path object or file object.
| :param format: Optional format override. If omitted, the
| format to use is determined from the filename extension.
| If a file object was used instead of a filename, this
| parameter should always be used.
| :param params: Extra parameters to the image writer.
| :returns: None
| :exception ValueError: If the output format could not be determined
| from the file name. Use the format option to solve this.
| :exception IOError: If the file could not be written. The file
| may have been created, and may contain partial data.
对一个 PIL.Image.Image 类的实例 im 调用 save() 方法,保存图像.
>>> im.save('lena.png', 'PNG')
3. scikit-image
scikit-image HomePage
scikit-image Documentation
3.1 安装
3.1.1 Ubuntu下安装
pip install scikit-image
3.1.2 Windows 下安装
pip install scikit-image
3.2 基本使用
import skimage
from skimage import io
3.2.1 读取图像 io.imread()
imread(fname, as_gray=False, plugin=None, flatten=None, **plugin_args)
Load an image from file.
Parameters
----------
fname : string
Image file name, e.g. ``test.jpg`` or URL.
as_gray : bool, optional
If True, convert color images to gray-scale (64-bit floats).
Images that are already in gray-scale format are not converted.
plugin : str, optional
Name of plugin to use. By default, the different plugins are
tried (starting with imageio) until a suitable
candidate is found. If not given and fname is a tiff file, the
tifffile plugin will be used.
Other Parameters
----------------
plugin_args : keywords
Passed to the given plugin.
flatten : bool
Backward compatible keyword, superseded by `as_gray`.
Returns
-------
img_array : ndarray
The different color bands/channels are stored in the
third dimension, such that a gray-image is MxN, an
RGB-image MxNx3 and an RGBA-image MxNx4.
读取图像
>>> img = io.imread('lena.jpg')
>>> img_grey = io.imread('lena.jpg', as_gray=True)
查看 io.imread() 返回数据类型
>>> type(img)
<class 'imageio.core.util.Array'>
>>> type(img_grey)
<class 'numpy.ndarray'>
>>> img.shape
(512, 512, 4)
>>> img_grey.shape
(512, 512)
3.2.2 显示图像 io.imshow()
imshow(arr, plugin=None, **plugin_args)
Display an image.
Parameters
----------
arr : ndarray or str
Image data or name of image file.
plugin : str
Name of plugin to use. By default, the different plugins are
tried (starting with imageio) until a suitable
candidate is found.
Other parameters
----------------
plugin_args : keywords
Passed to the given plugin.
显示图像
>>> io.imshow(img)
<matplotlib.image.AxesImage object at 0x0000007053A9FBA8>
io.imshow(img_grey)
<matplotlib.image.AxesImage object at 0x0000007053A8E400>
3.2.3 保存图像 io.imsave()
imsave(fname, arr, plugin=None, check_contrast=True, **plugin_args)
Save an image to file.
Parameters
----------
fname : str
Target filename.
arr : ndarray of shape (M,N) or (M,N,3) or (M,N,4)
Image data.
plugin : str, optional
Name of plugin to use. By default, the different plugins are
tried (starting with imageio) until a suitable
candidate is found. If not given and fname is a tiff file, the
tifffile plugin will be used.
check_contrast : bool, optional
Check for low contrast and print warning (default: True).
Other parameters
----------------
plugin_args : keywords
Passed to the given plugin.
Notes
-----
When saving a JPEG, the compression ratio may be controlled using the
``quality`` keyword argument which is an integer with values in [1, 100]
where 1 is worst quality and smallest file size, and 100 is best quality
and largest file size (default 75). This is only available when using
the PIL and imageio plugins.
>>> io.imsave('lena.png', img)
4. matplotlib
Matplotlib HomePage
Matplotlib User’s Guide
4.1 安装
Matplotlib installation instructions
4.1.1 Ubuntu下安装
$ pip install matplotlib
或者
$ sudo apt-get install python3-matplotlib
4.1.2 Windows下安装
> pip install matplotlib
4.2 基本使用
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
4.2.1 读取图像 plt.imread()
imread(fname, format=None)
Read an image from a file into an array.
Parameters
----------
fname : str or file-like
The image file to read. This can be a filename, a URL or a Python
file-like object opened in read-binary mode.
format : str, optional
The image file format assumed for reading the data. If not
given, the format is deduced from the filename. If nothing can
be deduced, PNG is tried.
Returns
-------
imagedata : :class:`numpy.array`
The image data. The returned array has shape
- (M, N) for grayscale images.
- (M, N, 3) for RGB images.
- (M, N, 4) for RGBA images.
Notes
-----
Matplotlib can only read PNGs natively. Further image formats are
supported via the optional dependency on Pillow. Note, URL strings
are not compatible with Pillow. Check the `Pillow documentation`_
for more information.
.. _Pillow documentation: http://pillow.readthedocs.io/en/latest/
plt.imread() = mping.imread()
>>> img = mpimg.imread('lena.jpg')
>>> type(img)
<class 'numpy.ndarray'>
或者
>>> img = plt.imread('lena.jpg')
>>> type(img)
<class 'numpy.ndarray'>
4.2.2 显示图像 plt.imshow()
mshow(X, cmap=None, norm=None, aspect=None, interpolation=None, alpha=None, vmin=None, vmax=None, origin=None, extent=None, shape=None, filternorm=1, filterrad=4.0, imlim=None, resample=None, url=None, *, data=None, **kwargs)
Display an image, i.e. data on a 2D regular raster.
Parameters
----------
X : array-like or PIL image
The image data. Supported array shapes are:
- (M, N): an image with scalar data. The data is visualized
using a colormap.
- (M, N, 3): an image with RGB values (float or uint8).
- (M, N, 4): an image with RGBA values (float or uint8), i.e.
including transparency.
The first two dimensions (M, N) define the rows and columns of
the image.
The RGB(A) values should be in the range [0 .. 1] for floats or
[0 .. 255] for integers. Out-of-range values will be clipped to
these bounds.
cmap : str or `~matplotlib.colors.Colormap`, optional
A Colormap instance or registered colormap name. The colormap
maps scalar data to colors. It is ignored for RGB(A) data.
Defaults to :rc:`image.cmap`.
aspect : {'equal', 'auto'} or float, optional
Controls the aspect ratio of the axes. The aspect is of particular
relevance for images since it may distort the image, i.e. pixel
will not be square.
This parameter is a shortcut for explicitly calling
`.Axes.set_aspect`. See there for further details.
- 'equal': Ensures an aspect ratio of 1. Pixels will be square
(unless pixel sizes are explicitly made non-square in data
coordinates using *extent*).
- 'auto': The axes is kept fixed and the aspect is adjusted so
that the data fit in the axes. In general, this will result in
non-square pixels.
If not given, use :rc:`image.aspect` (default: 'equal').
interpolation : str, optional
The interpolation method used. If *None*
:rc:`image.interpolation` is used, which defaults to 'nearest'.
Supported values are 'none', 'nearest', 'bilinear', 'bicubic',
'spline16', 'spline36', 'hanning', 'hamming', 'hermite', 'kaiser',
'quadric', 'catrom', 'gaussian', 'bessel', 'mitchell', 'sinc',
'lanczos'.
If *interpolation* is 'none', then no interpolation is performed
on the Agg, ps and pdf backends. Other backends will fall back to
'nearest'.
See
:doc:`/gallery/images_contours_and_fields/interpolation_methods`
for an overview of the supported interpolation methods.
Some interpolation methods require an additional radius parameter,
which can be set by *filterrad*. Additionally, the antigrain image
resize filter is controlled by the parameter *filternorm*.
norm : `~matplotlib.colors.Normalize`, optional
If scalar data are used, the Normalize instance scales the
data values to the canonical colormap range [0,1] for mapping
to colors. By default, the data range is mapped to the
colorbar range using linear scaling. This parameter is ignored for
RGB(A) data.
vmin, vmax : scalar, optional
When using scalar data and no explicit *norm*, *vmin* and *vmax*
define the data range that the colormap covers. By default,
the colormap covers the complete value range of the supplied
data. *vmin*, *vmax* are ignored if the *norm* parameter is used.
alpha : scalar, optional
The alpha blending value, between 0 (transparent) and 1 (opaque).
This parameter is ignored for RGBA input data.
origin : {'upper', 'lower'}, optional
Place the [0,0] index of the array in the upper left or lower left
corner of the axes. The convention 'upper' is typically used for
matrices and images.
If not given, :rc:`image.origin` is used, defaulting to 'upper'.
Note that the vertical axes points upward for 'lower'
but downward for 'upper'.
extent : scalars (left, right, bottom, top), optional
The bounding box in data coordinates that the image will fill.
The image is stretched individually along x and y to fill the box.
The default extent is determined by the following conditions.
Pixels have unit size in data coordinates. Their centers are on
integer coordinates, and their center coordinates range from 0 to
columns-1 horizontally and from 0 to rows-1 vertically.
Note that the direction of the vertical axis and thus the default
values for top and bottom depend on *origin*:
- For ``origin == 'upper'`` the default is
``(-0.5, numcols-0.5, numrows-0.5, -0.5)``.
- For ``origin == 'lower'`` the default is
``(-0.5, numcols-0.5, -0.5, numrows-0.5)``.
See the example :doc:`/tutorials/intermediate/imshow_extent` for a
more detailed description.
shape : scalars (columns, rows), optional, default: None
For raw buffer images.
filternorm : bool, optional, default: True
A parameter for the antigrain image resize filter (see the
antigrain documentation). If *filternorm* is set, the filter
normalizes integer values and corrects the rounding errors. It
doesn't do anything with the source floating point values, it
corrects only integers according to the rule of 1.0 which means
that any sum of pixel weights must be equal to 1.0. So, the
filter function must produce a graph of the proper shape.
filterrad : float > 0, optional, default: 4.0
The filter radius for filters that have a radius parameter, i.e.
when interpolation is one of: 'sinc', 'lanczos' or 'blackman'.
resample : bool, optional
When *True*, use a full resampling method. When *False*, only
resample when the output image is larger than the input image.
url : str, optional
Set the url of the created `.AxesImage`. See `.Artist.set_url`.
Returns
-------
image : `~matplotlib.image.AxesImage`
Other Parameters
----------------
**kwargs : `~matplotlib.artist.Artist` properties
These parameters are passed on to the constructor of the
`.AxesImage` artist.
See also
--------
matshow : Plot a matrix or an array as an image.
Notes
-----
Unless *extent* is used, pixel centers will be located at integer
coordinates. In other words: the origin will coincide with the center
of pixel (0, 0).
There are two common representations for RGB images with an alpha
channel:
- Straight (unassociated) alpha: R, G, and B channels represent the
color of the pixel, disregarding its opacity.
- Premultiplied (associated) alpha: R, G, and B channels represent
the color of the pixel, adjusted for its opacity by multiplication.
`~matplotlib.pyplot.imshow` expects RGB images adopting the straight
(unassociated) alpha representation.
>>> imgplot = plt.imshow(img)
>>> type(imgplot)
<class 'matplotlib.image.AxesImage'>
4.2.3 保存图像 plt.imsave()
imsave(fname, arr, **kwargs)
Save an array as in image file.
The output formats available depend on the backend being used.
Parameters
----------
fname : str or file-like
The filename or a Python file-like object to store the image in.
The necessary output format is inferred from the filename extension
but may be explicitly overwritten using *format*.
arr : array-like
The image data. The shape can be one of
MxN (luminance), MxNx3 (RGB) or MxNx4 (RGBA).
vmin, vmax : scalar, optional
*vmin* and *vmax* set the color scaling for the image by fixing the
values that map to the colormap color limits. If either *vmin*
or *vmax* is None, that limit is determined from the *arr*
min/max value.
cmap : str or `~matplotlib.colors.Colormap`, optional
A Colormap instance or registered colormap name. The colormap
maps scalar data to colors. It is ignored for RGB(A) data.
Defaults to :rc:`image.cmap` ('viridis').
format : str, optional
The file format, e.g. 'png', 'pdf', 'svg', ... . If not given, the
format is deduced form the filename extension in *fname*.
See `.Figure.savefig` for details.
origin : {'upper', 'lower'}, optional
Indicates whether the ``(0, 0)`` index of the array is in the upper
left or lower left corner of the axes. Defaults to :rc:`image.origin`
('upper').
dpi : int
The DPI to store in the metadata of the file. This does not affect the
resolution of the output image.
>>> plt.imsave('lena.png', img)
5. 与 Numpy 的相互转换
在 Python 中,常用 Numpy.array 表示图像。一般地,grayscale 用形状为 [height, width] 的 Numpy.array 表示,RGB 用形状为 [height, width, 3] 的 Numpy.array表示,RGBA 用形状为 [height, width, 4] 的 Numpy.array 表示(A 表示 Alpha,图像透明度)。
5.1 PIL 与 Numpy
PIL 一般用 PIL.Image.Image 实例对象来表示一幅图像.
1. PIL 转为 Numpy
# PILe => Numpy
img_np = np.array(img_PIL)
2. Numpy 转为 PIL
# Numpy => PIL
img_PIL = Image.fromarray(img_np)
5.2 OpenCV-Python 与 Numpy
OpenCV-Python 使用 Numpy.ndarray 作为底层数据结构,但它的通道顺序默认为 BGR,只用改变通道顺序.
1. cv2 转为 Numpy
# cv2 => Numpy
img_np = cv2.cvtColor(img_cv2, cv2.COLOR_BGR2RGB)
# or
b, g, r = cv2.split(img_cv2)
img_np = cv2.merge([r,g,b])
2. Numpy 转为 cv2
# Numpy => cv2
img_cv2 = cv2.cvtColor(img_np, cv2.COLOR_RGB2BGR)
# or
r, g, b = cv2.split(img_np)
img_np = cv2.merge([b,g,r])
5.3 Scikit-image 与 Numpy
scikit-image 使用 numpy.ndarray 作为底层数据结构,并且 scikit-image 与 Numpy 表示图像的方式一致,故 scikit-image 可与 Numpy 直接交互。
另外,scikit-image 可能会用到 imageio.core.util.Array
类,它是 numpy.ndarray
的子类。
imageio.core.util.Array 转为 numpy.ndarray
>>> type(im)
<class 'imageio.core.util.Array'>
>>> img = np.asarray(im)
>>> type(img)
<class 'numpy.ndarray'>
5.4 Matplotlib 与 Numpy
matplotlib 使用 numpy.ndarray 作为底层数据结构。matplolib 与 numpy 直接交互。