Python OpenCV - imshow doesn't need convert from BGR to RGB
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.
screen = cv2.cvtColor(screen, cv2.COLOR_RGB2BGR)
this one line code changes rgb to bgr
opencv_image_with_bgr_channels = cv2.imread('path/to/color_image.jpg')
matplotlib_compatible_image_with_rgb_channels = opencv_image_with_bgr_channels[:,:, ::-1]
This converts BGR to RGB Channels Image by reversing the channels.
for matplotlib we need to change BGR to RGB:
img = cv2.imread("image_name")
img = img[...,::-1]
plt.imshow(img)