15

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()

enter image description here

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

enter image description here

enter image description here

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

enter image description here

Original image in order of RGB.

enter image description here

3
  • 3
    I am not aware about any configuration flag which when passed to cv2.imread() reads image in RGB format. I think the default format is BGR only. You can always use cv2.cvtColor(img, cv2.COLOR_BGR2RGB) after reading the image to convert the image to RGB. Commented Apr 29, 2020 at 11:05
  • 4
    Just use RGB = cv2.imread('image.png')[...,::-1] Commented Apr 29, 2020 at 11:10
  • ` def get_image(self, filePath): return cv.imread(filePath)` code is this and return the image as RGB (when I show it up at screen it's seems like as seems on windows image viewer). Commented Apr 29, 2020 at 11:10

1 Answer 1

25

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
...
Sign up to request clarification or add additional context in comments.

8 Comments

Some thing wrong or I can not still understand how opencv read image and shows it. Added some new code and screen shot
I am using this python package. link.
Try taking a red image, there's one in another of my answers here i.sstatic.net/ZR0sb.png and opening it and displaying it with OpenCV and it should show up as red. Then open it with OpenCV and display with matplotlib and it should appear blue.
I tried it and it happened as you said. I think I am missing something :). well original image is red and opencv reads it as BGR order. so matplot shows it as BGR order right ? should I expect to see opencv imshow methods shows it as blue ? BGR order ?
The image is red. So all the red circles are stored in the first channel in the disk file. When OpenCV reads it, it stores the red data in the third channel, but that doesn't matter because when OpenCV displays it, the OpenCV display function expects the red information to be in the third channel so it looks correct. By contrast, matplotlib and PIL load images with red content in the first channel and their display functions expect the red stuff to be in the first channel. So the problems only occur if you display images with a different package from the one you opened with.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.