【问题标题】:Problem compare 2 photos and find differences in Python code问题比较 2 张照片并找出 Python 代码中的差异
【发布时间】:2023-02-13 21:18:12
【问题描述】:

有人可以帮助解决代码中的错误。我需要代码比较 2 个 PCB 图像并显示差异在哪里。我是代码新手。结果必须显示图像中的差异。 我已经尝试了一切,但没有找到解决方案。


import cv2
import numpy as np
ref = cv2.imread('Reference/Reference.jpg')
ref_gray = cv2.cvtColor(ref, cv2.COLOR_BGR2GRAY)
cv2.imshow('ref1',ref)
img = cv2.imread('WithDefects/WithDefects.jpg')
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
cv2.imshow('ref2',img)
diff = np.abs(ref_gray-img_gray)
diff_blur= cv2.medianBlur(diff, 3,3)
defects = np.where(diff_blur>50,255,0)
cv2.imshow(np.where(diff_blur>50,255,0))
cv2.imshow(diff_blur)
img_with_defects = np.where(defects == 255, (0,0,255),img)
cv2.imshow(img_with_defects)
cv2.waitKey(0)
cv2.destroyAllWindows()

我收到错误: 第 17 行,在 cv2.imshow(np.where(diff_blur>50,255,0)) cv2.error: OpenCV(4.7.0) :-1: 错误: (-5:Bad argument) in function 'imshow'

重载解析失败:

  • imshow() 缺少必需的参数“mat”(位置 2)
  • imshow() 缺少必需的参数“mat”(位置 2)
  • imshow() 缺少必需的参数“mat”(位置 2)

【问题讨论】:

  • cv2.imshow(diff_blur) 需要两个参数,而你只提供一个(它被视为窗口的名称,实际上不是图像),这同样适用于 cv2.imshow(img_with_defects)

标签: python machine-learning image-processing computer-vision raspberry-pi4


【解决方案1】:

在您的代码中,您将“np.where”的结果直接传递给“cv2.imshow”而不提供窗口名称。

尝试将其更改为此,

cv2.imshow("Defects", np.where(diff_blur>50,255,0))

【讨论】:

    猜你喜欢
    • 2016-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多