在python中使用opencv比较两个图像时出错

时间:2018-03-14 07:59:18

标签: python opencv

我在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

2 个答案:

答案 0 :(得分:0)

@Override
public void serialize(SomeClassImpl someClass, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
    // I need to write all fields except some
}

答案 1 :(得分:0)

OpenCV减法函数要求两个输入数组(如果它们是两个数组,而不是数组和标量)具有相同的通道大小和数量。

检查两者是RGB还是灰色,并将其中一个调整为另一个尺寸。只需要调整大小。