How to compare images in python: first use [pylab.imread] to read the image; then use [matplotlib.pylab - plt.imshow] to display the image; then convert the grayscale image to the RGB image; and finally save it Pictures are enough.
【Related learning recommendations: python tutorial】
How to compare images in python:
1. Read pictures
pylab.imread and PIL.Image.open read both RBG order,
and cv2.imread reads in BGR order, special attention should be paid when mixed use
1 matplotlib.pylab
import pylab as plt import numpy as np img = plt.imread('examples.png') print(type(img), img.dtype, np.min(img), np.max(img)) [out] (<type 'numpy.ndarray'>, dtype('float32'), 0.0, 1.0) # matplotlib读取进来的图片是float,0-1
2 PIL.image. open
from PIL import Image import numpy as np img = Image.open('examples.png') print(type(img), np.min(img), np.max(img)) img = np.array(img) # 将PIL格式图片转为numpy格式 print(type(img), img.dtype, np.min(img), np.max(img)) [out] (<class 'PIL.PngImagePlugin.PngImageFile'>, 0, 255) # 注意,PIL是有自己的数据结构的,但是可以转换成numpy数组 (<type 'numpy.ndarray'>, dtype('uint8'), 0, 255) # 和用matplotlib读取不同,PIL和matlab相同,读进来图片和其存储在硬盘的样子是一样的,uint8,0-255
3 cv2.imread
import cv2 import numpy as np img = cv2.imread('examples.png') # 默认是读入为彩色图,即使原图是灰度图也会复制成三个相同的通道变成彩色图 img_gray = cv2.imread('examples.png', 0) # 第二个参数为0的时候读入为灰度图,即使原图是彩色图也会转成灰度图 print(type(img), img.dtype, np.min(img), np.max(img)) print(img.shape) print(img_gray.shape) [out] (<type 'numpy.ndarray'>, dtype('uint8'), 0, 255) # opencv读进来的是numpy数组,类型是uint8,0-255 (824, 987, 3) # 彩色图3通道 (824, 987) # 灰度图单通道
import cv2 import pylab as plt from PIL import Image import numpy as np img_plt = plt.imread('examples.png') img_pil = Image.open('examples.png') img_cv = cv2.imread('examples.png') print(img_plt[125, 555, :]) print(np.array(img_pil)[125, 555, :] / 255.0) print(img_cv[125, 555, :] / 255.0) [out] [ 0.61176473 0.3764706 0.29019609] [ 0.61176471 0.37647059 0.29019608] [ 0.29019608 0.37647059 0.61176471] # opencv的是BGR顺序
2. Display pictures
1, matplotlib.pylab - plt.imshow
, this function actually displays an RGB image in numpy array format
import pylab as plt import numpy as np img = plt.imread('examples.png') plt.imshow(img) plt.show()
import pylab as plt from PIL import Image import numpy as np img = Image.open('examples.png') img_gray = img.convert('L') #转换成灰度图像 img = np.array(img) img_gray = np.array(img_gray) plt.imshow(img) # or plt.imshow(img / 255.0),matplotlib和matlab一样,如果是float类型的图像,范围是0-1才能正常imshow,如果是uint8图像,范围则需要是0-255 plt.show() plt.imshow(img_gray, cmap=plt.gray()) # 显示灰度图要设置cmap参数 plt.show() plt.imshow(Image.open('examples.png')) # 实际上plt.imshow可以直接显示PIL格式图像 plt.show()
import pylab as plt import cv2 import numpy as np img = cv2.imread('examples.png') plt.imshow(img[..., -1::-1]) # 因为opencv读取进来的是bgr顺序呢的,而imshow需要的是rgb顺序,因此需要先反过来 plt.show()
2 cv2 display image
import cv2 image2=cv2.imread(r"test/aaa/0002/0002_0_1.jpg") cv2.imshow("1",image2) cv2.waitKey(0)
3. Grayscale image-RGB image conversion
1 PIL.Image
from PIL import Image img = Image.open('examples.png') img_gray = img.convert('L') # RGB转换成灰度图像 img_rgb = img_gray.convert('RGB') # 灰度转RGB print(img) print(img_gray) print(img_rgb) [out] <PIL.PngImagePlugin.PngImageFile image mode=RGB size=987x824 at 0x7FC2CCAE04D0> <PIL.Image.Image image mode=L size=987x824 at 0x7FC2CCAE0990> <PIL.Image.Image image mode=RGB size=987x824 at 0x7FC2CCAE0250>
2 cv2 (note that opencv can convert color channels through parameters when reading images, the following is implemented in other ways)
import cv2 import pylab as plt img = cv2.imread('examples.png') img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # BGR转灰度 img_bgr = cv2.cvtColor(img_gray, cv2.COLOR_GRAY2BGR) # 灰度转BRG img_rgb = cv2.cvtColor(img_gray, cv2.COLOR_GRAY2RGB) # 也可以灰度转RGB
4. Save pictures
1 PIL.image - Save pictures in PIL format
from PIL import Image img = Image.open('examples.png') img.save('examples2.png') img_gray = img.convert('L') img_gray.save('examples_gray.png') # 不管是灰度还是彩色,直接用save函数保存就可以,但注意,只有PIL格式的图片能够用save函数
2 cv2.imwrite - Save pictures in numpy format
import cv2 img = cv2.imread('examples.png') # 这是BGR图片 cv2.imwrite('examples2.png', img) # 这里也应该用BGR图片保存,这里要非常注意,因为用pylab或PIL读入的图片都是RGB的,如果要用opencv存图片就必须做一个转换 img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) cv2.imwrite('examples_gray.png', img_gray)
If you want to know more about related learning, please pay attention to the php training column!
The above is the detailed content of How to compare the differences between images in python. For more information, please follow other related articles on the PHP Chinese website!