matplotlib 使用位运算符而不是不支持布尔运算符'-'对具有二进制数组图像遮罩

hkmswyz6  于 2022-11-15  发布在  其他
关注(0)|答案(1)|浏览(334)

**两个系统的python版本:第3.10.5节

我试图在我的图像上使用二进制掩码,并制作一个新的二进制图像。让我想知道的是,当我在我的另一个系统上用同样的代码练习同样的事情时,输出生成了,但在另一个系统上,它将返回此错误,并且不会生成二进制映像。(然后我想在主图像上使用40以下的像素掩码)。
在这里,我试图采取像素值低于40。

im = imread('image')
mask = im<40
plt.figure()
imshow(mask)
plt.show()

错误

4 plt.figure()
----> 5 imshow(mask)
      6 plt.show()
      7

TypeError: numpy boolean subtract, the `-` operator, is not supported, use the bitwise_xor, the `^` operator, or the logical_xor function instead.

<Figure size 432x288 with 0 Axes>
  • (如果需要,请相应地编辑查询。)*
0sgqnhkj

0sgqnhkj1#

您需要使用numpy ma masked_where方法:

import matplotlib.pyplot as plt
import numpy as np
im = plt.imread('image')
mask = np.ma.masked_where(im < 40, im)
plt.figure()
plt.imshow(mask)
plt.show()

相关问题