python读取图片rgb_opencv python将图像读取为RGB

Is it possible to opencv (using python) as default read an image as order of RGB ? in the opencv documentation imread method return image as order of BGR but in code imread methods return the image as RGB order ? I am not doing any converting process. Just used imread methods and show on the screen. It shows as on windows image viewer.

is it possible ?

EDIT 1:

my code is below. left side cv.imshow() methods and the other one plt.imshow() methods.

cv2.imshow() methods shows image as RGB and plt show it as opencv read (BGR) the image.

image_file = 'image/512-2-1001-18-RGB.jpg'

# img = imp.get_image(image_file)

img = cv2.imread(image_file)

plt.imshow(img)

plt.show()

cv2.imshow('asd', img)

cv2.waitKey(0)

cv2.destroyAllWindows()

EDIT 2 :

Some how opencv imshow methods is showing the image as RGB

below I have attached the first pixel's value of image and next image is photoshop pixel values

EDIT 3 :

below just reading image and with imshow and second image is original RGB image.

after imshow method image looks same as original image and this confused me

Original image in order of RGB.

解决方案

OpenCV is entirely consistent within itself. It reads images into Numpy arrays with the channels in BGR order, keeps the images in BGR order and its cv2.imshow() and cv2.imwrite() also expect images in BGR order. All your JPEG/PNG/BMP/TIFF files remain in their normal RGB order on disk.

Other libraries, such as PIL/Pillow, scikit-image, matplotlib, pyvips store images in conventional RGB order in memory.

So, you will only get colour issues if you mix OpenCV with any other library. If you go from/to OpenCV from any of the others, you will need to reverse the channel order. It is the same process either way, you are swapping the first and third channel:

RGBimage = BGRimage[...,::-1]

or

BGRimage = RGBimage[...,::-1]

Or, you can use OpenCV cvtColor() to do the transform:

RGBimage = cv2.cvtColor(BGRimage, cv2.COLOR_BGR2RGB)

You don't need to necessarily make a whole copy in a new variable every time. Say you read an image with OpenCV, in OpenCV BGR order obviously, and you briefly want to display it with matplotlib, you can just reverse the channels as you pass it:

# Load image with OpenCV and process in BGR order

im = cv2.imread(something)

# Briefly display with matplotlib, which will want RGB order

plt.imshow(img[...,::-1])

plt.show()

# Carry on processing in BGR order with OpenCV

...

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值