【发布时间】:2019-08-20 07:35:59
【问题描述】:
我裁剪了几张背景为灰色的图像,需要将它们转换为白色背景以与参考图像进行比较。
我实现了以下转换代码:
import cv2
im_gray = cv2.imread('gray_bg.png', cv2.IMREAD_GRAYSCALE)
(thresh, im_bw) = cv2.threshold(im_gray, 255, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
cv2.imwrite('white_bg.png', im_bw)
如果您观察,我的输出图像在原始图像的边缘有一些噪点(我希望我说的没有错)。因此,在将我的输出与参考图像进行比较时,我没有得到所需的输出。有人可以建议我怎么做吗?
这是我们编写的用于比较两个图像的程序:
SourceImagePath = r'white_bg.png'
TemplateImagePath = r'ex_white_bg.png'
#def IconValidation(self,SourceImagePath,TemplateImagePath):
sourceImg=cv.imread(SourceImagePath)
templateImg=cv.imread(TemplateImagePath)
_,tempwidth,tempheight=templateImg.shape[::-1]
srcheight = np.size(sourceImg, 0)
srcwidth = np.size(sourceImg, 1)
if(srcwidth < tempwidth) and (srcheight < tempheight):
print("comparison")
resultImg = cv.matchTemplate(sourceImg,templateImg,cv.TM_CCOEFF_NORMED)
matchVal = resultImg[0][0]
threshold=0.95
if(matchVal>threshold):
print("passed")
else:
print("failed")
【问题讨论】:
-
知道如何将输出与预期的输出进行比较会很有用;考虑到这是引发问题的操作。
-
更新了代码。请看一下
-
在更新您的问题后,为了更好地满足您的需求,如果您还分享模型图像以及您如何获得它将会很有用,以便其他人可以测试比较是否失败。为了进行比较,我使用了图像像素总和的百分比差异。
标签: python opencv edge-detection image-comparison