user498823
user498823

Reputation: 65

Colors in grayscale image

I am loading an image using cv2 in python (jupyter lab) and trying to display a grayscale image using plt.imshow.

import cv2
import matplotlib.pyplot as plt

img = cv2.imread('my_image.jpg')
new_img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
gray = cv2.cvtColor(new_img, cv2.COLOR_RGB2GRAY)
plt.imshow(img)
plt.imshow(new_img)
plt.imshow(gray)

When the code above is run this happens:

My question is regarding the gray image. Why does this happen and how can I change it?

Upvotes: 2

Views: 2640

Answers (1)

pip1726
pip1726

Reputation: 173

You have two options. Keep using pyplot:

plt.imshow(gray, cmap='gray')

Or instead using the openCV's default imshow.

cv2.imshow('img', img)
cv2.imshow('new_img', new_img)
cv2.imshow('gray', gray)

Upvotes: 4

Related Questions