英文:
How to check whether my image is RGB format or BGR format in python? How do i convert them and viceversa?
问题
如何在Python中检查图像是否为BGR格式或RGB格式?
如果图像是RGB格式,如何将其转换为BGR格式,反之亦然?
英文:
I am doing some preprocessing things on pretrained data in OpenVino model.
It says it only uses the BGR format image.
Here ,
How do i check in python whether my image is in BGR format or RBG format?
my loaded image code is as
import cv2
import numpy as np
from PIL import Image
image = cv2.imread('29101878_988024658021087_5045014614769664000_o.jpg')
print(image.shape)
Gives output of
shape (973,772,3)
How do i check image is RBG or BGR format?
If it is in RBG format How do i convert it to BGR and viceversa?
答案1
得分: 3
使用OpenCV(imread、VideoCapture)时,图像以BGR颜色空间加载。
参考:
“注意:对于彩色图像,解码后的图像将按照BGR顺序存储通道。”
链接: https://docs.opencv.org/2.4/modules/highgui/doc/reading_and_writing_images_and_video.html#imread)
要进行转换,您可以使用以下代码:
rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
反之亦然。
英文:
When you use opencv (imread, VideoCapture), the images are loaded in the BGR color space.
Reference :
Note: In the case of color images, the decoded images will have the channels stored in B G R order.
Link : https://docs.opencv.org/2.4/modules/highgui/doc/reading_and_writing_images_and_video.html#imread)
To convert you can use
rgb_image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
and vice versa.
答案2
得分: 2
To check if the image is in RGB or BGR format we can use:
import cv2
from PIL import Image
image = cv2.imread('image path')
img = Image.fromarray(image)
img.mode
英文:
To check if the image is in RGB or BGR format we can use:
import cv2
from PIL import Image
image = cv2.imread('image path')
img = Image.fromarray(image)
img.mode
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论