62

I have an image and I want to extract a region from it. I have coordinates of left upper corner and right lower corner of this region. In gray scale I do it like this:

I = cv2.imread("lena.png")
I = cv2.cvtColor(I, cv2.COLOR_RGB2GRAY)
region = I[248:280,245:288]
tools.show_1_image_pylab(region)

I can't figure it out how to do it in color. I thought of extracting each channel R, G, B; slicing this region from each of the channels and to merge them back together but there is gotta be a shorter way.

8
  • This itself should work for color image also. Commented Feb 25, 2013 at 17:28
  • try it before converting image to grayscale Commented Feb 25, 2013 at 17:32
  • 1
    Maybe you could use GetRectSubPix() to get the region instead of slicing.
    – martineau
    Commented Feb 25, 2013 at 18:10
  • 2
    I just tested it. And I got color image itself. Display image using cv2.imshow() function, not any pylab or matplotlib function, it is just shown in different color map in them. Commented Feb 25, 2013 at 18:49
  • 1
    And if you are doubt that, check region.shape and make sure it is of the format [rows,cols,3]. Commented Feb 25, 2013 at 18:50

3 Answers 3

147

There is a slight difference in pixel ordering in OpenCV and Matplotlib.

OpenCV follows BGR order, while matplotlib likely follows RGB order.

So when you display an image loaded in OpenCV using pylab functions, you may need to convert it into RGB mode. ( I am not sure if any easy method is there). Below method demonstrate it:

import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread('messi4.jpg')
b,g,r = cv2.split(img)
img2 = cv2.merge([r,g,b])
plt.subplot(121);plt.imshow(img) # expects distorted color
plt.subplot(122);plt.imshow(img2) # expect true color
plt.show()

cv2.imshow('bgr image',img) # expects true color
cv2.imshow('rgb image',img2) # expects distorted color
cv2.waitKey(0)
cv2.destroyAllWindows()

NB : Please check @Amro 's comment below for better method of conversion between BGR and RGB. img2 = img[:,:,::-1] . Very simple.

Run this code and see the difference in result yourself. Below is what I got :

Using Matplotlib :

enter image description here

Using OpenCV :

enter image description here

5
  • 89
    you could just do: img2 = img[:,:,::-1] to flip the colors dimension from BGR to RGB (using only NumPy indexing)
    – Amro
    Commented Oct 14, 2013 at 19:23
  • 3
    +1 - yeah, you are right and it would be faster than my answer. Thanks for the comment. (I just wanted to show that flipping of r and b channels specifically.) Commented Oct 15, 2013 at 5:31
  • 16
    Another way to achieve that result is using cv2.cvtColor with cv2.COLOR_BGR2RGB flag.
    – vahid abdi
    Commented Apr 24, 2015 at 13:08
  • 1
    How about this? cv2.imshow = lambda _, y: plt.imshow(y[:,:,::-1]).figure
    – Hatshepsut
    Commented Jul 15, 2017 at 6:35
  • it's img2 = img[::,::-1]
    – bluedroid
    Commented Oct 29, 2017 at 2:00
50

2 more options not mentioned yet:

img[..., ::-1] # same as the mentioned img[:, :, ::-1] but slightly shorter

and the versatile

cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
1

Best way to do this is to use :-
img2 = cv2.cvtColor(img , cv2.COLOR_BGR2RGB)

This will convert the BGR 'img' array to RGB 'img2' array. Now you can use img2 array for imshow() function of matplotlib.

Refer Link:- cvtColor

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.