python bgr转rgb_Python OpenCV-imshow不需要从BGR转换为RGB

As I'm lead to believe, OpenCV reads images in the BGR colorspace and we usually have to convert it back to RGB like this:

img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)

But when I try to simply read an image and show it, the coloring seems fine (without the need to convert BGR to RGB):

img_bgr = cv2.imread(image_path)

cv2.imshow('BGR Image',img_bgr)

img_rgb = cv2.cvtColor(img_bgr, cv2.COLOR_BGR2RGB)

cv2.imshow('RGB Image',img_rgb )

cv2.waitkey(0)

So is imshow() changing the colorspace within the function automatically (from BGR to RGB) or the colorspace has been BGR all along?

解决方案

BGR and RGB are not color spaces, they are just conventions for the order of the different color channels. cv2.cvtColor(img, cv2.COLOR_BGR2RGB) doesn't do any computations (like a conversion to say HSV would), it just switches around the order. Any ordering would be valid - in reality, the three values (red, green and blue) are stacked to form one pixel. You can arrange them any way you like, as long as you tell the display what order you gave it.

OpenCV imread, imwrite and imshow indeed all work with the BGR order, so there is no need to change the order when you read an image with cv2.imread and then want to show it with cv2.imshow.

While BGR is used consistently throughout OpenCV, most other image processing libraries use the RGB ordering. If you want to use matplotlib's imshow but read the image with OpenCV, you would need to convert from BGR to RGB.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值