如何检查我的图像是RGB格式还是BGR格式在Python中?如何进行相互转换?

huangapple go评论184阅读模式
英文:

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

huangapple
  • 本文由 发表于 2020年1月3日 23:54:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/59581538.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定