plt.imread和PIL.Image.open讀入爲RGB,cv2.imread讀入爲BGR,轉換RGB和BGR方式:
轉換代碼:
https://blog.csdn.net/LEE18254290736/article/details/101598596
PIL與cv2轉換代碼:
def PILImageToCV(path):
# PIL Image轉換成OpenCV格式
img = Image.open(path)
plt.subplot(121)
plt.imshow(img)
img = cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)
plt.subplot(122)
plt.imshow(img)
plt.show()
def CVImageToPIL(path, ):
# OpenCV圖片轉換爲PIL image
img = cv2.imread(path)
img = img[:, :, [2,1,0]]
# cv2.imshow("OpenCV",img)
plt.subplot(121)
plt.imshow(img)
img2 = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
# plt.subplot(122)
#plt.imshow(img2)
plt.show()