Suppose have an image like this, that consists of two images, 1 and 6 which are kind of superimposed on each other, if you look closely at it.
So, I need to visualize each of these digits separately by having all positive pixels of original image in one image, and all negative pixels in the second one. Is there any way to achieve that in python using matplotlib.pyplot without messing up with the structure of the image? Basically, I need all the white color pixels to be plotted separately from the black color pixels.
You may set all values above or below some threshold to nan
, such that they won't appear in the final image.
The following code leaves out the range between 0.4 and 0.6 completely. The yellow background is chosen to show that there are no pixels in that area.
import numpy as np
import matplotlib.pyplot as plt
img = plt.imread("grayscaleimage.png")[:,:,0]
white = np.copy(img)
white[white<0.6] = np.nan
dark = np.copy(img)
dark[dark>0.4] = np.nan
fig = plt.figure()
ax0 = fig.add_subplot(211)
ax1 = fig.add_subplot(223)
ax2 = fig.add_subplot(224)
ax0.imshow(img, vmin=0, vmax=1, cmap="Greys")
ax1.imshow(white, vmin=0, vmax=1, cmap="Greys")
ax2.imshow(dark, vmin=0, vmax=1, cmap="Greys")
for ax in (ax1,ax2):
ax.set_facecolor("gold")
plt.show()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.