imshow错误地将黑色输出到白色
我想根据字母的宽度和高度绘制一些字母,并通过4个相等的区域将它们分开。碰巧的是,直到区域具有空白像素,例如Zone_0.jpg,Zone_7和Zone_12,如下图中所示。我该如何制作以使图像框对图像2D数组的每个值具有255的框?
到目前为止,这是我的代码:
half_width = int(np.floor(width / 2))
half_height = int(np.floor(height / 2))
z11 = boxed[:half_height, :half_width]
z12 = boxed[:half_height, half_width:]
z21 = boxed[half_height:, :half_width]
z22 = boxed[half_height:, half_width:]
# plotting by zone
plt.figure(figsize=(4, 4), tight_layout=True)
plt.subplot(221)
plt.imshow(z11, cmap='gray')
plt.title('z11')
plt.subplot(222)
plt.imshow(z12, cmap='gray')
plt.title('z12')
plt.subplot(223)
plt.imshow(z21, cmap='gray')
plt.title('z21')
plt.subplot(224)
plt.imshow(z22, cmap='gray')
plt.title('z22')
plt.savefig(f'{zone_path}/zone_{i}.jpg')
plt.close()
因此每个区域变量(Z11
,Z12
,Z21
,Z22
代码>)是2D Numpy阵列,每个单元格可以包含0(将输出黑色)或255(将输出白色)。
例如,关注Zone_0.jpg
,Z22
变量将仅包含具有255的单元格作为值:
/UQNUS.png“ rel =” nofollow noreferrer“> 将包含0或255值的混合:
在逻辑上应该没有问题,因为255应该始终输出到白色,所以我想知道为什么这种行为会发生?
I want to plot some letters and separate them by 4 equal zones according to the letter's width and height. It just so happens that everything looks good until a zone has blank pixels, such as zone_0.jpg, zone_7 and zone_12 as seen in the images below. How can I make it so that the image box that have 255 for every value of the image 2d array be printed white?
Here's my code so far:
half_width = int(np.floor(width / 2))
half_height = int(np.floor(height / 2))
z11 = boxed[:half_height, :half_width]
z12 = boxed[:half_height, half_width:]
z21 = boxed[half_height:, :half_width]
z22 = boxed[half_height:, half_width:]
# plotting by zone
plt.figure(figsize=(4, 4), tight_layout=True)
plt.subplot(221)
plt.imshow(z11, cmap='gray')
plt.title('z11')
plt.subplot(222)
plt.imshow(z12, cmap='gray')
plt.title('z12')
plt.subplot(223)
plt.imshow(z21, cmap='gray')
plt.title('z21')
plt.subplot(224)
plt.imshow(z22, cmap='gray')
plt.title('z22')
plt.savefig(f'{zone_path}/zone_{i}.jpg')
plt.close()
So each zone variable (z11
, z12
, z21
, z22
) are 2d numpy arrays where each cell can contain either 0 (will output black) or 255 (will output white).
For example, focusing on zone_0.jpg
, the z22
variable will only contain cells with 255 as the value:
but z11
will contain a mix of 0 or 255 values:
Logically thinking, there should be no issue since 255 should always output to white, so I wonder why this behaviour happens?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认情况下,matplotlib始终缩放colormap,以使颜色图最小颜色是数据最小的,而colormap max颜色是数据最大。您可以将0和1用于数据值并获得相同的图像。如果您的数据min = max,则显然设置了比例,以使显示器与Colormap最小值匹配。
要用您的当前0、255数据确定colormap量表,请在每个绘图调用中添加以下内容:
vmin = 0,vmax = 255
。这些迫使colormap范围而不是使用传递给绘图调用的数据。请参阅有关详细信息 - 所有具有CMAP能力的情节功能支持此功能。Matplotlib by default always scales the colormap so that the color map min color is the data min and the colormap max color is the data max. You could have just used 0 and 1 for your data values and gotten the same images. If you have the data min = max, the scale is apparently set such that the display will match the colormap min.
To assert the colormap scale with your current 0, 255 data, add the following to each plot call:
vmin=0, vmax=255
. These force the colormap range instead of using the data passed to the plot call. See matplotlib.pyplot.imshow documentation for details - all cmap-capable plot functions support this.