At present, there are several mainstream image databases:
1. OpenCV 2. PIL(Pillow) 3. matplotlib.image 4. skimage 5. scipy.misc
Conclusion: OpenCV is the most powerful and mature image library.
1.1 reading and storage of OpenCV image
import cv2 #Read image directly numpy Matrix format img = cv2.imread('horse.jpg',1) # 0 Read in gray picture, 1 read in color picture cv2.imshow('image',img) # Display image print(img.shape) # (height,width,channel) print(img.size) # Pixel number print(img.dtype) # data type print(img) # Of the printed image numpy Array, 3-dimensional array #Save image # Current directory storage cv2.write('horse1.jpg',img) # Custom storage cv2.write('/path_name/' + str(image_name) + '.jpg',img) cv2.waitKey()
1.2OpenCV image ashing
import cv2 #Method 1 img = cv2.imread('horse.jpg',0) # 0 Read in gray picture,Or use cv2.IMREAD_GRATSCALE Substitute 0 cv2.imshow('gray image',img) #Method two img = cv2.imread('horse.jpg') gray_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) cv2.imshow('gray image',gray_img) print(gray_img.shape) # (height, width) print(gray_img.size) # Pixel number print(gray_img) # Of the printed image numpy Array, 2D cv2.waitKey()
1.3 OpenCV matrix format transformation
Why? : OpenCV's matrix format (height, width, channels) - > > the type of deep learning matrix may be (channels, height, width)
import cv2 import numpy as np img = cv2.imread('horse.jpg',1) cv2.imshow('image',img) # Transformation of matrix format print(img.shape) img = img.transpose(2,0,1) #Transformation function print(img.shape)
# Matrix extension (batch_size, channels, height, width) Operation of forecast single picture # Add a column as the number of pictures img = np.expand_dims(img, axis=0) #Use numpy function print(img.shape)
# Training stage construction batch data_lst = [] loop: img = cv2.imread('xxx.jpg') data_lst.append(img) data_arr = np.array(data_lst)
1.4 OpenCV image normalization
import cv2 # In order to reduce the computation, it is necessary to set the pixel value 0-255 Switch to 0-1 Between img = cv2.imread('horse.jpg') img = img.astype('float') / 255.0 # First convert the data type to float print(img.dtype) print(img)
1.5 OpenCV BRG to RGB
import cv2 img = cv2.imread('horse.jpg') img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB) # Turn to RGB format print(img)
1.6 OpenCV access pixel
import cv2 img = cv2.imread('horse.jpg') gray_img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY) # Turn to Gray image print(img[4,4]) # 3 channels print(gray_img[4,4]) # 1 channel
1.7 OpenCV ROI
import cv2 img = cv2.imread('horse.jpg') print(img.shape) roi = img[0:437,0:400] # [y:height,x:width] cv2.imshow('roi',roi) cv2.waitKey()
2.1 PIL image reading and storage
from PIL import Image import numpy as np #Image reading img = Image.open('horse.jpg') print(img.format) # Picture format print(img.size) # (width,height) print(img.mode) # Picture channel type #Convert image to matrix format arr = np.array(img) print(arr.shape) print(arr.dtype) #Image storage new_img = Image.fromarray(arr) new_img.save('test.jpg') img.show()
2.2 gray processing of PIL image
#Image gray processing gray = Image.open('horse.jpg').convert('L') gray_arr = np.array(gray) print(gray_arr.shape) # (height,width) print(gray_arr.dtype) print(gray_arr) gray.show()
2.3 PIL region of interest shear
# Region of interest cut img = Image.open('horse.jpg') roi = img.crop((0,0,200,200)) # (Left upper x,Left upper y,lower right x,lower right y) roi.show()
2.4 channel operation
# Channel processing r,g,b = img.split() #separate img = Image.merge("RGB",(b,g,r)) #merge img = img.copy() #copy
3.1 Matplotlib reading and storing pictures
import matplotlib.pyplot as plt import numpy as np # Image read as numpy Array format img = plt.imread('horse.jpg') plt.axis('off') # Turn off scale display print(img.shape) # (height, width, channel) print(img.size) # Pixel number print(img.dtype) #Save pictures plt.savefig('./name.jpg') figure = plt.figure(figsize=(20,10)) # Resize display picture plt.imshow(img) plt.show()
3.2 image ashing of Matplotlib
#Image ashing # Average value img_mean = img.mean(axis=2) plt.imshow(img_mean,cmap='gray') plt.show() #Maximum value method img_max = img.max(axis=-1) plt.imshow(img_max,cmap='gray') plt.show() #RGB Trichromatic method gravity = np.array([0.299,0.587,0.114]) img_gravity = np.dot(img,gravity) plt.imshow(img_gravity,cmap="gray") plt.show()
4.1 skimage reading and storing images
from skimage import io #Read image numpy Array format img = io.imread('horse.jpg') print(img.shape) print(img.dtype) print(img.size) #print(img) io.imshow(img) #Save image io.imsave('test.jpg',img)
4.2 ashing of skimage
#Image ashing and normalization img = io.imread('horse.jpg',as_gray=True) print(img.shape) print(img.dtype) # Data type bit float print(img.size) print(img) io.imshow(img) io.show()
5.1 scipy.misc reading and storing images
#At 1.2.0 After unified use imageio Modular import imageio import matplotlib.pyplot as plt #Read picture as numpy array img = imageio.imread('horse.jpg') print(img.dtype) print(img.size) # Pixel number print(img.shape) #(height, width, channels) plt.imshow(img) plt.show() print(img) #Save pictures imageio.imsave('test.jpg',img)
To be continued