I am trying to work on some pixel manipulation on a sequence of images. I encountered an issue when plotting same value of pixel having different color when plotted with different range of pixels.
For example, I have an image with six different pixel values(0, 2, 3, 4, 5, 6):
Code #1 :
import numpy as np
import matplotlib.pyplot as plt
import cv2
image = np.zeros(shape=[256, 256], dtype=np.uint8)
cv2.circle(image, center=(50, 50), radius=10, color=(2, 2), thickness= -1)
cv2.circle(image, center=(100, 100), radius=10, color=(3, 3), thickness= -1)
cv2.circle(image, center=(150, 150), radius=10, color=(4, 4), thickness= -1)
cv2.circle(image, center=(75, 200), radius=10, color=(5, 5), thickness= -1)
cv2.circle(image, center=(200, 89), radius=10, color=(6, 6), thickness= -1)
plt.imshow(image)
plt.show()
Now when I add two more shapes in the image with different pixel value(7, 12), then color for all the existing pixels changes
Code #2 :
image = np.zeros(shape=[256, 256], dtype=np.uint8)
cv2.circle(image, center=(50, 50), radius=10, color=(2, 2), thickness= -1)
cv2.circle(image, center=(100, 100), radius=10, color=(3, 3), thickness= -1)
cv2.circle(image, center=(150, 150), radius=10, color=(4, 4), thickness= -1)
cv2.circle(image, center=(75, 200), radius=10, color=(5, 5), thickness= -1)
cv2.circle(image, center=(200, 89), radius=10, color=(6, 6), thickness= -1)
cv2.circle(image, center=(21, 230), radius=5, color=(7, 7), thickness= -1)
cv2.circle(image, center=(149, 250), radius=5, color=(12, 12), thickness= -1)
plt.imshow(image)
plt.show()
How can I ensure same value of pixel gives me same color irrespective of any other pixel present or not?
>Solution :
This is due to the colormap used. Matplotlib adapts colormap to the plotted values.
If you want to make sure it is the same colormap values/range that is being used, please specify the range explicitly using max and min values:
# Here adapt and choose the values that suits you the best
min_value = -10
max_value = 10
image = np.zeros(shape=[256, 256], dtype=np.uint8)
cv2.circle(image, center=(50, 50), radius=10, color=(2, 2), thickness= -1)
cv2.circle(image, center=(100, 100), radius=10, color=(3, 3), thickness= -1)
cv2.circle(image, center=(150, 150), radius=10, color=(4, 4), thickness= -1)
cv2.circle(image, center=(75, 200), radius=10, color=(5, 5), thickness= -1)
cv2.circle(image, center=(200, 89), radius=10, color=(6, 6), thickness= -1)
cv2.circle(image, center=(21, 230), radius=5, color=(7, 7), thickness= -1)
cv2.circle(image, center=(149, 250), radius=5, color=(12, 12), thickness= -1)
plt.imshow(image)
plt.clim(min_value,max_value)
plt.show()

