CV2, Image, PIL image reading, and mutual conversion and display with Tensor

Through the following learning, you can randomly pick out pictures to display during the deep learning process, and convert them to numpy or PIL through Tensor for viewing.

1 image read

read method read format RGB order HWC sequence
cv2.imread(path) numpy.ndarray BGR H、W、C
plt.imread(path) numpy.ndarray RGB H、W、C
Image.open(path) PIL RGB H、W、C

2 picture show

Just use plt.imshow() to display, it’s perfect, but plt display requires RGB order, so there will be problems displaying cv2 pictures, but you only need to reconstruct the order

Example:
insert image description here

The im_cv here has a color misalignment problem, because it reads in BGR , and plt displays it in RGB . The solution is as follows:

Modify the BGR order of cv2 to re-display:
insert image description here

It is to perform RGB reconstruction on the data read by cv2

3 ndarry and PIL transform into each other

Purpose Way
ndarry -> PIL img = Image.fromarray(img.astype(“uint8”)).convert(“RGB”)
PIL -> ndarry img = np.array(img)

Example:

'''PIL转成numpy数组:'''
img = Image.open(img_path)
print(type(img))  #<class 'PIL.Image.Image'>
img = np.array(img)  
print(type(img))  #<class 'numpy.ndarray'>
print('PIL',img.shape)  #(H,W,C)
'''numpy 转成 PIL'''
#input img is numpy type
img = Image.fromarray(img.astype('uint8')).convert('RGB')
print(type(img))    #<class 'PIL.Image.Image'>

4 Transformation between Tensor, ndarry and PIL

Purpose Way
PIL、ndarry -> Tensor transforms.ToTensor()
Tensor -> PIL transforms.ToPILImage()
Tensor -> ndarry .numpy()

Tensor is actually the ndarry data type plus the function of automatic differentiation, so you only need to use .numpy() to convert Tensor to ndarry

5 examples

5.1 Tensor -> PIL -> Display

insert image description here

Tensor is converted through ToPILImage() , the channel does not need to be changed, and the converted data type can be displayed directly through plt

5.2 Tensor -> ndarry -> display

insert image description here

Convert the cv2 image to Tensor and then to image. If you want to display normally, you need to perform data expansion and astype operation. At the same time, because Tensor is in the form of CHW , so it is converted to ndarry , you also need to convert the dimension through np.transpose .

The remaining one uses plt to read in, capture it as Tensor, and then convert it to ndarry. You can practice it yourself, which is a little easier than 5.2.

Guess you like

Origin blog.csdn.net/qq_44864833/article/details/127337475