1

I am new to Python + OpenCV, so this might be a basic question for most of you, as I couldn't find a good/ satisfactory solution for this online.

So I am trying to create an Image by separately creating R-G-B layers
R - Layer of 0s
G - Layer of 255s
B - Layer of 255*Identity matrix

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

Red = np.zeros([6, 6], dtype = np.uint8)
plt.imshow(Red)        # it is just the red layer which is actually all black
plt.show()

Green = np.ones([6, 6], dtype = np.uint8) * 255
plt.imshow(Green)      # it is just the Green layer which is actually all white
plt.show()

Blue = np.eye(6, dtype = int) * 255
plt.imshow(Blue)       # it is just the Blue layer which is actually black with white diag
plt.show()

But I am actually getting a Purple or combination of purplr and yellow. My Output

Can someone explain what's happening and/or how to solve it?

RC0993
  • 898
  • 1
  • 13
  • 44
  • 1
    matplotlib.pyplot produce false color images by default. You will need to use a grayscale color map with it. – fmw42 Aug 08 '19 at 05:29
  • 1
    @fmw42 Can you elaborate? Do you mean `plt.imshow(Green, cmap='gray')`? – RC0993 Aug 08 '19 at 05:31
  • 1
    Why did you expect matplotlib to treat those arrays as red, green, and blue layers? You don't have anything in your code that would make matplotlib do that. – user2357112 Aug 08 '19 at 05:37
  • @user2357112 That is just a naming convention for my understanding. I expect matplotlib to treat them as individual grayscale. – RC0993 Aug 08 '19 at 05:39
  • `plt.imshow(Green, cmap='gray')` Yes, if you want the images to be grayscale and not purple and yellow. – fmw42 Aug 08 '19 at 16:01

1 Answers1

5

Try using

Blue = np.eye(6, dtype = int) * 255
plt.imshow(Blue, cmap='gray', vmin=0, vmax=255)
plt.show()

for more reference this answer

Amit Gupta
  • 2,698
  • 4
  • 24
  • 37