Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get same color when plotting numpy image array for same pixel value multiple times

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 :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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()

Output:
image 1

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()

Output:
enter image description here

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()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading