python image processing - summary of reading image methods

The first step in learning digital image processing is to read the image. Here I summarize how to use opencv3, scikit-image, and PIL image processing libraries to read and display images.

Read and display images

opencv3 library

Opencv reads the image and returns matrix data. The shape of the RGB image is (height, weight, channel), and the dtype is uint8.

The sample code is as follows:

import cv2
# 读入一副彩色图像
img_cv2 = cv2.imread('test.jpg',cv2.IMREAD_COLOR)
# 打印图像尺寸,形状,图像元素数据类型
print(type(img_cv2))
print(img_cv2.shape)    # (height, width, channel)
print(img_cv2.dtype)    # uint8
# matplotlib绘制显示图像
plt.figure(1)
plt.imshow(img_PIL)
plt.show()
# cv2绘制显示图像
# cv2.imshow()
# cv2.namedWindow('image', cv2.WINDOW_NORMAL)
# cv2.imshow('image',img_cv2)
# cv2.waitKey(0)
# cv2.destroyAllWindows()

scikit-image library

The sample code is as follows:

from skimage import io
img_skimage = io.imread('test.jpg')
# 打印图像尺寸
print(img_skimage.shape)    #(height, width, channel)
# 绘制显示图像
io.imshow(img_skimage)
# import matplotlib.pyplot as plt
# plt.imshow(img_skimage)

Note: io.imshow(img_skimage), the essence of this line of code is to use the matplotlib package to draw the image. After the drawing is successful, a matplotlib type data is returned. In other words, the scikit-image library actually calls the imshow display function of the matplotlib library to draw the image.

cv2 and skimage read images. The size of the image can be obtained through its shape attribute. shape returns a tuple. The first element represents the height of the image, the second represents the width of the image, and the third represents the pixels. Number of channels.

PIL library

The sample code is as follows:

# PIL库读取绘制显示图像
# plt 用于显示图片
from PIL import Image
import matplotlib.pyplot as plt

import numpy as np
img_PIL = Image.open('test.jpg')
img_PIL = np.array(img_PIL)
# 打印图像类型,尺寸和总像素个数
print(type(img_PIL)) # <class 'numpy.ndarray'>
print(img_PIL.shape) # (height, width, channel), (1200, 1793, 3)
print(img_PIL.size)  # 6454800 = 1200*1793*3
# 绘制显示图像
plt.figure(1)
plt.imshow(img_PIL)
plt.show()

Read image result analysis

The images are read using Opnecv3 and sckit-image respectively, and displayed using the matplotlib library. The sample code is as follows:

import cv2
from skimage import io
import matplotlib.pyplot as plt
img_cv2 = cv2.imread('test.jpg',cv2.IMREAD_COLOR)
img_skimage = io.imread('test.jpg')
# matplotlib显示cv2库读取的图像
plt.figure('imread picture',figsize=(25,25))
plt.subplot(121)
plt.title('cv2 imread picture')
plt.imshow(img_cv2)
# matplotlib显示skimage库读取的图像
plt.subplot(122)
plt.title('skimage imread picture')
plt.imshow(img_skimage)
# 打印图像尺寸,总像素个数,和图像元素数据类型
print(img_cv2.shape)
print(img_cv2.size)
print(img_cv2.dtype)

Insert image description here

Through the comparison of the above output results, we will find that the image read by the cv2 library displayed by matplotlib is different from the original image. This is because the channels of the opencv3 library when reading the image are BGR, while the channels of the normal image are read. RGB, the matplotlib library displays images according to the RGB sequential channels, the explanation is complete.

I'm a little confused. I found out by querying the library function that the first parameter of plt.show() is the object to be displayed (array_like). The literal meaning is an array-like object, but it is obvious that the PIL library returns not 'numpy.ndarray' object, but the 'PIL.JpegImagePlugin.JpegImageFile' object, then why can the plt.show() function still display the result returned by the Image.open() function reading the image?

The program is shown below:

Insert image description here

Print image information

Common image information includes image size, number of pixels, number of channels, etc.

skimage gets image information

NOTE: The scikit-image library reads and scales images nearly 4 times slower than the opencv library.

from skimage import io, data
# create coffee image, return (300, 451, 3) uint8 ndarray
img = data.coffee()
io.imshow(img)      # 显示图片
print(type(img))    # 显示类型
print(img.dtype)    # 显示图像元素数据类型
print(img.shape)    # 显示尺寸
print(img.shape[0]) # 图片高度
print(img.shape[1]) # 图片宽度
print(img.shape[2]) # 图片通道数
print(img.size)     # 显示总像素个数=shape[0]*shape[1]*shape[2]
print(img.max())    # 最大像素值
print(img.min())    # 最小像素值
print(img.mean())   # 像素平均值
print(img[0][0])    # 图像第一行第一列的像素值

The output is as shown below:

Insert image description here

PIL gets image information

# 获取PIL image图片信息
im = Image.open('test.jpg')
print (type(im))
print (im.size) #图片的尺寸
print (im.mode) #图片的模式
print (im.format) #图片的格式
print (im.getpixel((0,0)))#得到像素:
# img读出来的图片获得某点像素用getpixel((w,h))可以直接返回这个点三个通道的像素值

The output is as follows:

Insert image description here

The plt.show function is defined as follows:

Signature: plt.imshow(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, hold=None, data=None, **kwargs)
Docstring:
Display an image on the axes.

Parameters
———-
X : array_like, shape (n, m) or (n, m, 3) or (n, m, 4). Display the image in XX to current axes. XX may be an array or a PIL image. If XX is an array, it can have the following shapes and types:

– MxN — values to be mapped (float or int)
– MxNx3 — RGB (float or uint8)
– MxNx4 — RGBA (float or uint8)

The value for each component of MxNx3 and MxNx4 float arrays should be in the range 0.0 to 1.0. MxN arrays are mapped to colors based on the ∥∥∥∥ (mapping scalar to scalar) and the cmapcmap (mapping the normed scalar to a color).

Summary of methods for reading and displaying images

PIL library to read images

PIL.Image.open + numpy
scipy.misc.imread
scipy.ndimage.imread
these The methods all read image information by calling PIL.Image.open;
PIL.Image.open does not directly return numpy objects, you can use the ones provided by numpy Function to convert, refer to Image and Ndarray conversion;
scipy.ndimage.imread directly returns the numpy.ndarray object, the channel order is RGB, and the default range of channel value is 0-255.

Opencv3 reads image

cv2.imread: Use opencv to read the image and directly return the numpy.ndarray object. The channel order is BGR. Note that BGR, channel The value default range is 0-255.

scikit-image library reads images

skimage.io.imread: directly returns the numpy.ndarray object, the channel order is RGB, and the channel value default range is 0-255.

References

Guess you like

Origin blog.csdn.net/purple_love/article/details/134960447