美文网首页
找出图片不同方案之diffImg 、odiff、(opencv+

找出图片不同方案之diffImg 、odiff、(opencv+

作者: KICHUN | 来源:发表于2023-04-17 18:07 被阅读0次

需求

需要对比两张图片,并识别出图片是否一致,如果不一致,则用矩形框圈出来。没错,这就像大家来找茬的游戏。

思考

  1. 图片是否一致,先用MD5校验,如果文件二进制MD5完全一致,那么两张图片肯定一样。
  2. 假设图片不一样,那则需要计算机视觉范畴解析图片并做出判断,并处理差异部分
  3. 针对差异部分,希望软件可以识别区域内有差距的地方,并用矩形框框选显示,用于客户快速定位。

百度一下

首先我认为这种方案应该是成熟的应用方案,网上一找肯定有,结果不出所料。经过大量的信息筛选,我这边选出了几个优秀的方案。(此刻,辣鸡百度真的费时费力,资料大多是些无用信息或者滥竽充数,此刻感叹需要一个ChatGPT)

准备两张原图

test3.png

第二张图片在第一章的基础上做一个红框 和一个difference文字

test1.png

diffImg

软件下载地址 https://sourceforge.net/projects/diffimg/files/
是一款C端软件

下载,打开exe,打开两张图片,软件截图如下


image.png

差异图可保存,如下


diff.jpeg

总结:

  1. 可以看到差异百分比
  2. 可以看到图像不同的地方
  3. 研究了一下,没法更改差异的颜色,也无法框选

odiff

软件下载地址 https://github.com/dmtrKovalenko/odiff
开源软件,大家先上github看下介绍

参考github相关介绍,执行命令,如图
.\ODiffBin.exe --diff-color=#2f9b82 d:/temp/test1.png d:/temp/test3.png d:/temp/odiff.png
因为不同之处是红色,而这个软件默认的也是红色,所以命令指定了颜色输出

image.png

输出差异图图片


odiff.png

总结

  1. 输出有判断图片是否一致,不同的像素点数量,不同像素点百分比,输出不同点图片
  2. 可以指定输出不同区域像素颜色
  3. 无法框选不同区域,无法单独输出差异图

OPENCV+python

OPENCV 官网 https://opencv.org/
OPENCV github https://github.com/opencv/opencv

opencv百度百科介绍

OpenCV是一个基于Apache2.0许可(开源)发行的跨平台计算机视觉和机器学习软件库,可以运行在LinuxWindowsAndroidMac OS操作系统上。 [1] 它轻量级而且高效——由一系列 C 函数和少量 C++ 类构成,同时提供了Python、RubyMATLAB等语言的接口,实现了图像处理和计算机视觉方面的很多通用算法。

通过百度图片找茬关键字,大部分结果是基于opencv+python实现,当然,opencv的功能很强大,用于处理图片找茬只是一个简单应用,这里,我也做了简单测试

参考文档 https://pyimagesearch.com/2017/06/19/image-difference-with-opencv-and-python/

1. 安装python 及需要的库

  1. 安装 python
  2. 安装 pip install --upgrade scikit-image
  3. 安装 pip install --upgrade imutils
  4. 安装 pip install opencv-python

2. 编写python脚本

# import the necessary packages
from skimage.metrics import structural_similarity as compare_ssim
import argparse
import imutils
import cv2
import numpy as np

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-f", "--first", required=True,
    help="first input image")
ap.add_argument("-s", "--second", required=True,
    help="second")
args = vars(ap.parse_args())


# load the two input images
imageA = cv2.imread(args["first"])
imageB = cv2.imread(args["second"])



# 先判断两张图片是否一致
difference = cv2.subtract(imageA, imageB)
result = not np.any(difference) #if difference is all zeros it will return False
 
if result is True:
     print ("两张图片一样")
else:
     print ("两张图片不一样")


# convert the images to grayscale
grayA = cv2.cvtColor(imageA, cv2.COLOR_BGR2GRAY)
grayB = cv2.cvtColor(imageB, cv2.COLOR_BGR2GRAY)

# compute the Structural Similarity Index (SSIM) between the two
# images, ensuring that the difference image is returned
(score, diff) = compare_ssim(grayA, grayB, full=True)
diff = (diff * 255).astype("uint8")
print("SSIM: {}".format(score))

# threshold the difference image, followed by finding contours to
# obtain the regions of the two input images that differ
thresh = cv2.threshold(diff, 0, 255,
    cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
    cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)


# loop over the contours
for c in cnts:
    # compute the bounding box of the contour and then draw the
    # bounding box on both input images to represent where the two
    # images differ
    (x, y, w, h) = cv2.boundingRect(c)
    cv2.rectangle(imageA, (x, y), (x + w, y + h), (0, 0, 255), 2)
    cv2.rectangle(imageB, (x, y), (x + w, y + h), (0, 0, 255), 2)

# show the output images 这个是打开图片,这里不需要
#cv2.imshow("Original", imageA)
#cv2.imshow("Modified", imageB)
#cv2.imshow("Diff", diff)
#cv2.imshow("Thresh", thresh)


cv2.imwrite("Original.jpg",imageA)
cv2.imwrite("Modified.jpg",imageB)
cv2.imwrite("thresh.jpg",thresh)
cv2.imwrite("diff.jpg",diff)

# 

cv2.destroyAllWindows()

#cv2.waitKey(0)

3. 执行python命令

python image_diff.py --test1.png --second test3.png
如下图,框选的文件是输入和输出

image.png

脚本将输出四张图片,这里选一张输入标记图和差异图


Original.jpg diff.jpg

总结

  1. opencv比较强大,可以做图片像素比较、相似度输出等
  2. 可以针对不同之处,做外框标识

三个软件对比总结

  1. 均可以找出图片不同之处,提供图片相似度、差异像素点数量等功能
  2. diffImg 和 odiff 基本上不用做开发,掌握简单使用方式即可达到效果
  3. opencv+python方案最为强大,可以框选出图片不同之处,但软件安装依赖配置较多,需要编写python脚本

以上,希望可以帮到大家,感谢点赞,比心~

by 王启昌 广州

相关文章

网友评论

      本文标题:找出图片不同方案之diffImg 、odiff、(opencv+

      本文链接:https://www.haomeiwen.com/subject/nnscjdtx.html