简体   繁体   English

在子图上分别绘制图像的正像素和负像素

[英]Plotting positive and negative pixels of image separately on subplots

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. 假设有一个这样的图像,它由两个图像(1和6)组成,如果仔细观察,它们会彼此叠加。

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? 有什么办法可以在python中使用matplotlib.pyplot来实现,而又不会弄乱图像的结构? 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. 您可以将所有高于或低于某个阈值的值都设置为nan ,这样它们就不会出现在最终图像中。

The following code leaves out the range between 0.4 and 0.6 completely. 以下代码完全超出了0.4到0.6的范围。 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()

在此处输入图片说明


Here is the test image used in the above: 这是上面使用的测试图像: 在此处输入图片说明 (right click, save as...) (右键单击,另存为...)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM