[英]How do I compare between two images in python?
我的代碼是:
import cv2
from PIL import Image
import numpy as np
img=cv2.imread("IMG040.jpg")
img2=cv2.imread("IMG040.jpg")
p1 = np.array(img)
p2 = np.array(img2)
img3=img-img2
p3 = np.array(img3)
if p3==0 :
print "the same"
else:
print"not the same"
but I have this problem
File "part2.py", line 10, in <module>
if p3==0 :
錯誤信息:
**ValueError:** The truth value of an array with more than one element is
ambiguous. Use a.any() or a.all()
表達方式
p3==0
創建一個布爾型numpy數組。 Python if
語句不知道如何將整個數組解釋為true或false。 這就是錯誤消息的含義。 您大概想知道所有元素是否都為零,這就是為什么錯誤消息建議您應該使用all()
。
為此,您最終需要將行更改為
if (p3==0).all():
但是,最好將numpy數組與allclose
方法進行比較,這可以解決數字錯誤。 所以嘗試更換這個
img3=img-img2
p3 = np.array(img3)
if p3==0 :
print "the same"
else:
print"not the same"
同
if np.allclose(img, img2):
print "the same"
else:
print "not the same"
您需要執行以下if語句:
if np.all(p3==0):
print 'The same'
else:
print 'Not the same'
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.