I have a simple code that loads RGB image, converts it to grayscale and then runs Canny edge detector algorithm. The returned image contains only 0 and 255 values, and yet when showing the image using matplotlib it shows the image as grayscale (and not black and white).
How can I fix this?
My code -
import cv2
import matplotlib.pyplot as plt
in_img = cv2.imread('colored_image.jpeg')
gray_in_img = cv2.cvtColor(in_img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(gray_in_img, 100, 155)
fig = plt.figure(1)
ax = fig.add_subplot(1, 3, 1)
ax.imshow(cv2.cvtColor(in_img, cv2.COLOR_BGR2RGB))
ax = fig.add_subplot(1, 3, 2)
ax.imshow(gray_in_img, cmap='gray')
ax = fig.add_subplot(1, 3, 3)
ax.imshow(edges, cmap='gray')
plt.show()
The output figure is:
Zooming in the last image we can see that in contains variety of gray intensities instead of black and white only:
whereas I'd like the last image to be a black and white image, such as:
When I've debugged the code, I've checked that the values of edges
are indeed only 0
and 255
.
The original RGB image: