如何找到 numpy 数组中图像的差异?
How to find the difference of images in numpy arrays?
我正在尝试计算 2 张图像之间的差异。我希望得到一个整数作为我的结果,但我没有得到我期望的结果。
from imageio import imread
#https://raw.githubusercontent.com/glennford49/sampleImages/main/cat1.png
#https://raw.githubusercontent.com/glennford49/sampleImages/main/cat2.png
img1="cat1.png" # 183X276
img2="cat2.png" # 183x276
numpyImg1=[]
numpyImg2=[]
img1=imread(img1)
img2=imread(img2)
numpyImg1.append(img1)
numpyImg2.append(img2)
diff = numpyImg1[0] - numpyImg2[0]
result = sum(abs(diff))
print("difference:",result)
打印:
# it prints an array of images rather than printing an interger only
目标:
difference: <int>
一张(彩色)图像是一个 3D 矩阵,所以您可以做的是使用 numpy.array(image)
将这些图像转换为 numpy
数组,然后您可以获得这两个 numpy
数组。
最终答案将是一个 3 维数组
我相信numpy数组的维数不是1,你需要对数组的维数进行求和的次数才能有一个和值。
[1,2,3]
sum gives : 6
[[1,2,3],[1,2,3]]
sum gives : [2,4,6]
doing a second sum opertion gives
: 12 (single value)
您可能需要在打印数据之前再添加一个“sum(result)”(如果图像是二维的)。
例如:
numpyImg2.append(img2)
diff = numpyImg1[0] - numpyImg2[0]
result = sum(abs(diff))
result = sum(result) >> Repeat
print("difference:",result)
这是我在 rgb 通道中找到 2 个图像差异的答案。
如果要减去2张相同的图像,
印刷:
每像素差异:0
from numpy import sum
from imageio import imread
#https://github.com/glennford49/sampleImages/blob/main/cat2.png
#https://github.com/glennford49/sampleImages/blob/main/cat2.png
img1="cat1.png"
img2="cat2.png"
numpyImg1=[]
numpyImg2=[]
img1=imread(img1)
img2=imread(img2)
numpyImg1.append(img1)
numpyImg2.append(img2)
diff = numpyImg1[0] - numpyImg2[0]
result = sum(diff/numpyImg1[0].size)
result = sum(abs(result.reshape(-1)))
print("difference per pixel:",result)
您正在使用 Python 的内置 sum
函数,它也只对绝对计算执行求和 along the first dimension of a NumPy array. This is the reason why you are getting a 2D array as the output instead of the single integer you expect. Please use numpy.sum
on your result instead which will internally flatten a multi-dimensional NumPy array then sum over the results. In addition, you might as well use numpy.abs
:
import numpy as np
result = np.sum(np.abs(diff))
使用 numpy.sum
意味着您不再需要在使用答案中的内置 sum
函数之前将数组重塑为扁平化表示。对于未来的开发,始终对要对 NumPy 数组执行的任何算术运算使用 NumPy 方法。它可以防止意外行为,例如您刚才看到的。
我正在尝试计算 2 张图像之间的差异。我希望得到一个整数作为我的结果,但我没有得到我期望的结果。
from imageio import imread
#https://raw.githubusercontent.com/glennford49/sampleImages/main/cat1.png
#https://raw.githubusercontent.com/glennford49/sampleImages/main/cat2.png
img1="cat1.png" # 183X276
img2="cat2.png" # 183x276
numpyImg1=[]
numpyImg2=[]
img1=imread(img1)
img2=imread(img2)
numpyImg1.append(img1)
numpyImg2.append(img2)
diff = numpyImg1[0] - numpyImg2[0]
result = sum(abs(diff))
print("difference:",result)
打印:
# it prints an array of images rather than printing an interger only
目标:
difference: <int>
一张(彩色)图像是一个 3D 矩阵,所以您可以做的是使用 numpy.array(image)
将这些图像转换为 numpy
数组,然后您可以获得这两个 numpy
数组。
最终答案将是一个 3 维数组
我相信numpy数组的维数不是1,你需要对数组的维数进行求和的次数才能有一个和值。
[1,2,3]
sum gives : 6
[[1,2,3],[1,2,3]]
sum gives : [2,4,6]
doing a second sum opertion gives
: 12 (single value)
您可能需要在打印数据之前再添加一个“sum(result)”(如果图像是二维的)。
例如:
numpyImg2.append(img2)
diff = numpyImg1[0] - numpyImg2[0]
result = sum(abs(diff))
result = sum(result) >> Repeat
print("difference:",result)
这是我在 rgb 通道中找到 2 个图像差异的答案。
如果要减去2张相同的图像, 印刷: 每像素差异:0
from numpy import sum
from imageio import imread
#https://github.com/glennford49/sampleImages/blob/main/cat2.png
#https://github.com/glennford49/sampleImages/blob/main/cat2.png
img1="cat1.png"
img2="cat2.png"
numpyImg1=[]
numpyImg2=[]
img1=imread(img1)
img2=imread(img2)
numpyImg1.append(img1)
numpyImg2.append(img2)
diff = numpyImg1[0] - numpyImg2[0]
result = sum(diff/numpyImg1[0].size)
result = sum(abs(result.reshape(-1)))
print("difference per pixel:",result)
您正在使用 Python 的内置 sum
函数,它也只对绝对计算执行求和 along the first dimension of a NumPy array. This is the reason why you are getting a 2D array as the output instead of the single integer you expect. Please use numpy.sum
on your result instead which will internally flatten a multi-dimensional NumPy array then sum over the results. In addition, you might as well use numpy.abs
:
import numpy as np
result = np.sum(np.abs(diff))
使用 numpy.sum
意味着您不再需要在使用答案中的内置 sum
函数之前将数组重塑为扁平化表示。对于未来的开发,始终对要对 NumPy 数组执行的任何算术运算使用 NumPy 方法。它可以防止意外行为,例如您刚才看到的。