cgDude
cgDude

Reputation: 93

What is x[:,:,::-1] in python

import numpy as np 
import cv2 as cv
from matplotlib import pyplot as plt
img=cv.imread("content.jpg")
x=np.array(img,dtype=np.uint8)
y=x[:,:,::-1]
plt.imshow(x,interpolation='nearest')
plt.show()
plt.imshow(y,interpolation='nearest')
plt.show()

I have some doubts regarding this code

  1. What y=x[:,:,::-1] doing in this code.
  2. The plot of image x is different from the original image, why is it so?
  3. The plot of image y is same as the original image, what is the reason?

Upvotes: 0

Views: 542

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150785

cv2 reads image as BGR, that is, the pixels are in BGR order. On the other hand plt.imshow() uses the more common RBG settings.

x[:,:,::-1] essentially inverts the order of color, so BGR becomes RGB.

So the command

y = x[:,:,::-1]

is equivalent to cv2.cvtColor:

y = cv2.cvtColor(x, cv2.COLOR_BGR2RGB)

Upvotes: 2

Related Questions