Python reads and displays pits in the image

Assume a case: cv reads in an image, converts it into a tensor, converts it into an image again and displays it. There are many pitfalls here, and I annotate the pitfalls
in the code

  1. The data read by the cv module is BGR
  2. The numpy data is (H, W, C)
  3. The tensor data is (C, H, W)
  4. plt supports floating-point numbers from 0-1 and integers from 0-255
import matplotlib.pyplot as plt
from PIL import Image
import torchvision.transforms as transforms
import cv2 as cv
import numpy as np

img = cv.imread('./test_image/test.png') # openCV 读入数据为 BGR (坑点一)
print(type(img))
print(img.shape)   # numpy数组格式为(H,W,C)(坑点二)

transf = transforms.ToTensor() # tensor是浮点数
img_tensor = transf(img)  # tensor数据格式是torch(C,H,W) (坑点三)
print(img_tensor.size())
# tensor (c, h, w) 需要转为(h,w,c)才能正常显示
npimg = img_tensor.numpy()
# 转换维度
npimg = npimg.swapaxes(0, 2)
npimg = npimg.swapaxes(0, 1)
print(npimg.shape)
npimg = npimg[:,:,::-1] # 把BGR改成RGP
# npimg.show()
plt.imshow(npimg) # 可以直接渲染0-1浮点数
plt.show()
  • Demonstration effect
    insert image description here

Don't think that this is the end, the real pitfalls are in the following code, pay attention to the comparison and difference

import matplotlib.pyplot as plt
from PIL import Image
import torchvision.transforms as transforms
import cv2 as cv
import numpy as np
from PIL import Image

img = cv.imread('./test_image/test.png') # openCV 读入数据为 BGR (坑点一)
print(type(img))
print(img.shape)   # numpy数组格式为(H,W,C)(坑点二)

transf = transforms.ToTensor()
img_tensor = transf(img)  # tensor数据格式是torch(C,H,W) (坑点三)
print(img_tensor.size())
# tensor (c, h, w) 需要转为(h,w,c)才能正常显示
npimg = img_tensor.numpy()
# 转换维度
npimg = npimg.swapaxes(0, 2)
npimg = npimg.swapaxes(0, 1)
print(npimg.shape)
npimg =  npimg * 255
# 必须转化为int类型,
npimg = npimg[:,:,::-1] # 把BGR改成RGP
# 确实从numpy转化为Image对象的,而且,必须使用int对象
npimg = Image.fromarray(np.uint8(npimg), "RGB") 
# npimg.show()
plt.imshow(npimg) # 可以直接渲染0-1
plt.show()
    

Demonstration of correct effect
insert image description here
If npimg = Image.fromarray(np.uint8(npimg), "RGB") used npimg = Image.fromarray(npimg, "RGB") then a new error is generated
insert image description here

  • The above error comes from float and int. The figure below demonstrates the difference between the two image data

float type
insert image description here
int type, note the dot behind the above figure.
insert image description here

  • Finally, I hope everyone pays attention to the details. Each image processing library in python has its own characteristics and slight differences.

Guess you like

Origin blog.csdn.net/Fly_the_start/article/details/129741817