[英]Getting error while comparing two images using opencv in python
在Python 2.7中使用OpenCV比較兩個圖像時遇到問題
這是我的代碼:
import cv2
import numpy as np
img1 = cv2.imread("./spot/image1.jpg")
img2 = cv2.imread("./spot/image2.jpg")
diff = cv2.subtract(img1, img2)
result = not np.any(diff)
if result is True:
print "The images are the same"
else:
cv2.imwrite("result.jpg", difference)
print "the images are different"
我收到此錯誤:
Traceback (most recent call last):
File "E:\PrgLang\Python\compareImage.py", line 7, in <module>
diff = cv2.subtract(img1,img2)
error: OpenCV(3.4.1) C:\build\master_winpack-bindings-win64-vc14-static\opencv\modules\core\src\arithm.cpp:659: error: (-209) The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array' in function cv::arithm_op
import cv2
import numpy as np
img1 = cv2.resize(cv2.imread("./spot/image1.jpg"), (224, 224)).astype(np.float32)
img2 = cv2.resize(cv2.imread("./spot/image2.jpg"), (224, 224)).astype(np.float32)
diff = cv2.subtract(img1, img2)
result = not np.any(diff)
if result is True:
print "The images are the same"
else:
cv2.imwrite("result.jpg", diff)
print "the images are different"
OpenCV減法函數要求兩個輸入數組(如果它們是兩個數組,而不是一個數組和一個標量)必須具有相同的通道大小和數量。
檢查兩者均為RGB還是GREY,然后將其中一個調整為另一尺寸。 僅需要調整大小。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.