简体   繁体   English

如何在python中检查我的图像是RGB格式还是BGR格式? 我如何转换它们,反之亦然?

[英]How to check whether my image is RGB format or BGR format in python? How do i convert them and viceversa?

I am doing some preprocessing things on pretrained data in OpenVino model.我正在对 OpenVino 模型中的预训练数据进行一些预处理。 It says it only uses the BGR format image.它说它只使用 BGR 格式的图像。

Here , How do i check in python whether my image is in BGR format or RBG format?在这里,我如何在 python 中检查我的图像是 BGR 格式还是 RBG 格式?

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)给出形状的输出(973,772,3)

How do i check image is RBG or BGR format?如何检查图像是 RBG 还是 BGR 格式? If it is in RBG format How do i convert it to BGR and viceversa?如果是 RBG 格式,我如何将其转换为 BGR,反之亦然?

When you use opencv (imread, VideoCapture), the images are loaded in the BGR color space.当您使用 opencv (imread, VideoCapture) 时,图像会加载到 BGR 色彩空间中。

Reference : Note: In the case of color images, the decoded images will have the channels stored in BGR order.参考: Note: In the case of color images, the decoded images will have the channels stored in BGR order.

Link : https://docs.opencv.org/2.4/modules/highgui/doc/reading_and_writing_images_and_video.html#imread )链接: 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.反之亦然。

To check if the image is in RGB or BGR format we can use:要检查图像是 RGB 还是 BGR 格式,我们可以使用:

import cv2
from PIL import Image

image = cv2.imread('image path')
img = Image.fromarray(image)
img.mode

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM