Python:计算相对于另一幅图像不存在的非零像素的总数

Python: count total no of non-zero pixels which are not not present with respect to another image

我有 2 个二进制图像。

  1. 我想找到图像 'B' 中不存在于图像 'A' 中的非零像素总数。
  2. 还想计算相对于图像 'B'
  3. 从图像 'A' 消失的非零像素总数

这是一个图形示例 (Each white dot represent a pixel)

在我的情况下,问题 1 应该是 2。 谢谢

使用 PIL import image 将您的图像导入 numpy 数组,然后使用 numpy/pandas 逻辑来获取您的答案或将它们转换为列表并使用我下面的逻辑:

import numpy
from PIL import Image
'''
pic1 = Image.open("foo1.jpg")
pic2 = Image.open("foo2.jpg")

a1 = numpy.array(pic1)
a2 = numpy.array(pic2)

pixel1 = a1.tolist()
pixel2 = a2.tolist()
'''
mat1 = [
    [0,0,1],
    [0,1,0],
    [1,0,0]
]
mat2 = [
    [1,0,0],
    [0,1,0],
    [0,0,1]
]

result = []
for rownum,row in enumerate(mat1):
    addrow=[]
    for ind,item in enumerate(row):
        if mat1[rownum][ind]==0 and mat2[rownum][ind]==1:
            addrow.append("question_1")
        elif mat1[rownum][ind]==1 and mat2[rownum][ind]==0:
            addrow.append("question_2")
        else:
            addrow.append("_")
    result.append(addrow)
for r in result:
    print(r)