PIL.Image.open与cv2.imread格式问题
程序员文章站
2022-05-16 11:24:01
...
常用图片读取方式:
PIL.Image.open
scipy.misc.imread
scipy.ndimage.imread
cv2.imread(速度最快)
matplotlib.image.imread
skimage
caffe.io.load_image
imagepath='test1.jpg'
im1=Image.open(imagepath)
im1=np.array(im1)#获得numpy对象,RGB
print(type(im1))
print(im1.shape)
#2 opencv
im2=cv2.imread(imagepath)
print(type(im2))#numpy BGR
print(im2.shape)#[width,height,3]
#3 matplotlib 类似matlab的方式
im3 = mpimg.imread(imagepath)
print(type(im3))#np.array
print(im3.shape)
#4 skimge
#caffe.io.load_iamge()也是调用的skimage实现的,返回的是0-1 float型数据
im4 = io.imread(imagepath)
print(type(im4))#np.array
print(im4.shape)
##########
# <class 'numpy.ndarray'> # (851, 1279, 3)
# <class 'numpy.ndarray'> # (851, 1279, 3)
# <class 'numpy.ndarray'> # (851, 1279, 3)
# <class 'numpy.ndarray'> # (851, 1279, 3)
参考:https://blog.csdn.net/renelian1572/article/details/78761278
Image.open与cv2.imread格式互转
PIL.Image转换为OpenCV格式:
import cv2
from PIL import Image
import numpy
path = '/home/gavin/Desktop/demo-card-1.jpeg'
img = Image.open(path).convert("RGB")#.convert("RGB")可不要,默认打开就是RGB
img.show()
#转opencv
#img = cv2.cvtColor(numpy.asarray(image),cv2.COLOR_RGB2BGR)
img = cv2.cvtColor(np.array(img),cv2.COLOR_RGB2BGR)
cv2.imshow("OpenCV",img)
cv2.waitKey()
OpenCV转换为PIL.Image格式
import cv2
from PIL import Image
import numpy
img = cv2.imread("plane.jpg") # opencv打开的是BRG
cv2.imshow("OpenCV",img)
image = Image.fromarray(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))
image.show()
cv2.waitKey()
判断图像数据是否是OpenCV格式:
isinstance(img, np.ndarray)
上一篇: 网易游戏面试题 如何判断一棵二叉树是AVL(平衡二叉树)
下一篇: 全网最强红黑树的理解和实现