Home > Software design >  The color of the image changes when it enters the subplot
The color of the image changes when it enters the subplot

Time:09-17

Why do the two examples below produce different colors? How can I show same image on the subplot?

image = cv2.imread('img.jpg')
plt.imshow(image)


fig = plt.figure()
rows = 1
columns = 1
fig.add_subplot(rows, columns, 1)
plt.imshow(image)

CodePudding user response:

OpenCV follows BGR (Blue-Green-Red) colour scheme whereas the dataset images usually are in RGB (Red-Green-Blue) format.

image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

Adding the above line will convert the BGR image read by OpenCV to RGB, and would give the expected output.

CodePudding user response:

Try using opencv's builtin conversion:

image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

Or try reversing the values with NumPy slicing:

image = image[:, :, ::-1]
  • Related