首頁 > 軟體

詳解python中讀取和檢檢視片的6種方法

2022-04-19 13:01:17

本文主要介紹了python中讀取和檢檢視片的6種方法,分享給大家,具體如下:

file_name1='test_imgs/spect/1.png' # 這是彩色圖片
file_name2='test_imgs/mri/1.png' # 這是灰度圖片

1 OpenCV

注:用cv2讀取圖片預設通道順序是B、G、R,而不是通常的RGB順序,所以讀進去的彩色圖直接顯示會出現變色情況,詳情可以看:https://www.jb51.net/article/245048.htm

import cv2
spect= cv2.imread(file_name1) # BGR
spect= spect[:, :, ::-1] # RGB
mri= cv2.imread(file_name2) # 灰度圖
print(spect.shape) # (256, 256, 3)
print(mri.shape) # (256, 256, 3)   cv2讀進來是三通道的圖片
import matplotlib.pyplot as plt
plt.imshow(spect)
plt.show()

import matplotlib.pyplot as plt
fig=plt.figure()
f1 = fig.add_subplot(121)
f2 = fig.add_subplot(122)
f1.imshow(spect)
f2.imshow(mri)
plt.show()

2 imageio

import imageio 
spect = imageio.imread(file_name1) 
mri = imageio.imread(file_name2) 
print(spect.shape) # (256, 256, 3)
print(mri.shape) # (256, 256)
import matplotlib.pyplot as plt
fig=plt.figure()
f1 = fig.add_subplot(121)
f2 = fig.add_subplot(122)
f1.imshow(spect)
f2.imshow(mri,cmap='gray') # 注:單通道灰度圖必須加上cmap='gray'才能正確顯示
plt.show()

3 PIL

from PIL import Image
import numpy as np
spect= Image.open(file_name1) #  <PIL.PngImagePlugin.PngImageFile image mode=RGB size=256x256 at 0x1D9F15FFDC8>
spect.show()

4 scipy.misc

from scipy.misc import imread
spect = imread(file_name1)
mri = imread(file_name2)
import matplotlib.pyplot as plt
fig=plt.figure()
f1 = fig.add_subplot(121)
f2 = fig.add_subplot(122)
f1.imshow(spect)
f2.imshow(mri,cmap='gray') # 注:單通道灰度圖必須加上cmap='gray'才能正確顯示
plt.show()

5 tensorflow

from tensorflow.python.keras.preprocessing.image import load_img
spect = load_img(file_name1) #  <PIL.PngImagePlugin.PngImageFile image mode=RGB size=256x256 at 0x1D9EF188048>
spect.show()

6 skimage

from skimage import io
import matplotlib.pyplot as plt
mri = io.imread(file_name2)#讀取資料
plt.imshow(mri,cmap='gray') # 注:單通道灰度圖必須加上cmap='gray'才能正確顯示
plt.show()

 到此這篇關於詳解python中讀取和檢檢視片的6種方法的文章就介紹到這了,更多相關python讀取和檢檢視片內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com