近期学习机器视觉,接下来会将将cv整理为一系列教程。
微信公众号:Deepthinkerr
QQ:2454409598(微信公众号回复不及时,有问题可以QQ联系)
微信文章链接:机器视觉 - 图像读取(含代码以及相关图片数据)
图像读取
常见方法主要有以下三种:
1 PIL
from PIL import Image
import matplotlib.pyplot as plt
import numpy as np
img_fp = "./image-data/10815824_2997e03d76.jpg"
plt.figure(figsize=(8, 12))
img = Image.open(img_fp)
plt.imshow(img)
print(type(img)) # <class 'PIL.JpegImagePlugin.JpegImageFile'>
2 cv2
import cv2
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
img_fp = "./image-data/10815824_2997e03d76.jpg"
plt.figure(figsize=(8,12))
plt.subplot(2,1,1)
img = cv2.imread(img_fp) # BGR
plt.imshow(img)
print(type(img), img.shape)
plt.subplot(2,1,2)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
plt.imshow(img)
print(type(img), img.shape)
3 matplotlib
from matplotlib import image as mpimg
import matplotlib.pyplot as plt
import numpy as np
%matplotlib inline
plt.figure(figsize=(8,6))
img = mpimg.imread(img_fp)
plt.imshow(img)
print(type(img)) # <class 'numpy.ndarray'>