Image.open()读取的通道顺序是RGB,cv2.imread()读取的通道顺序为BGR
Image.save()直接保存RGB的图片
cv2.imwirte()保存图片的时候相当于做了BGR2RGB再去保存
image.open
imagepath='1.png'
im1=Image.open(imagepath).convert("RGB")#.convert("RGB")可不要,默认打开就是RGB
print(type(im1))
#
print(im1.size)
#(600, 400)
im1.save('2.png')#直接保存RGB的图片
im1=np.array(im1)#获得numpy对象,RGB
print(type(im1))
#
print(im1.shape)
#(400, 600, 3) 这里的3是RGB格式
cv2.imread
cv2.imread(filename, flags)
参数:
filepath:读入imge的完整路径
flags:标志位,{cv2.IMREAD_COLOR,cv2.IMREAD_GRAYSCALE,cv2.IMREAD_UNCHANGED}
cv2.IMREAD_COLOR:默认参数,读入一副彩色图片,忽略alpha通道,可用1作为实参替代 BGR
cv2.IMREAD_GRAYSCALE:读入灰度图片,可用0作为实参替代
cv2.IMREAD_UNCHANGED:顾名思义,读入完整图片,包括alpha通道,可用-1作为实参替代
PS:alpha通道,又称A通道,是一个8位的灰度通道,该通道用256级灰度来记录图像中的透明度复信息,定义透明、不透明和半透明区域,其中黑表示全透明,白表示不透明,灰表示半透明
# ----------------------------------------
# get uint8 image of size HxWxn_channles (RGB)
# ----------------------------------------
def imread_uint(path, n_channels=3):
# input: path
# output: HxWx3(RGB or GGG), or HxWx1 (G)
if n_channels == 1:
img = cv2.imread(path, 0) # cv2.IMREAD_GRAYSCALE
img = np.expand_dims(img, axis=2) # HxWx1
elif n_channels == 3:
img = cv2.imread(path, cv2.IMREAD_UNCHANGED) # BGR or G
if img.ndim == 2:
img = cv2.cvtColor(img, cv2.COLOR_GRAY2RGB) # GGG
else:
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # RGB
return img
im2 = cv2.imread(imagepath)#BGR
print(type(im2)) # numpy BGR
#
print(im2.shape) # [width,height,3]
#(400, 600, 3) 这里的3是BGR格式
cv2.imwrite('3.png',im2)
#保存图片的时候相当于做了BGR2RGB再去保存,因为保存的图片是RGB格式
cv2.imshow("OpenCV",im2)
cv2.waitKey()
cv2.imshow()采用BGR模式
plt.imshow() 采用RGB模式
img.show() 采用RGB模式
Image转Opencv
img = Image.open(path)
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()