简体   繁体   English

如何使用 Python 和 OpenCV 计算两个图像之间的差异百分比?

[英]How do I calculate the percentage of difference between two images using Python and OpenCV?

I am trying to write a program in Python (with OpenCV) that compares 2 images, shows the difference between them, and then informs the user of the percentage of difference between the images.我正在尝试用 Python(使用 OpenCV)编写一个程序来比较 2 张图像,显示它们之间的差异,然后通知用户图像之间的差异百分比。 I have already made it so it generates a .jpg showing the difference, but I can't figure out how to make it calculate a percentage.我已经做到了,所以它会生成一个 .jpg 来显示差异,但我不知道如何让它计算百分比。 Does anyone know how to do this?有谁知道如何做到这一点?

Thanks in advance.提前致谢。

Here is a simple idea you can adapt.这是一个您可以适应的简单想法。 But always ensure the images being compared are of the same shape.但始终确保被比较的图像具有相同的形状。

Code:代码:

img1 = cv2.imread('dog.jpg', 0)
img2 = cv2.imread('cat.jpg', 0)

#--- take the absolute difference of the images ---
res = cv2.absdiff(img1, img2)

#--- convert the result to integer type ---
res = res.astype(np.uint8)

#--- find percentage difference based on number of pixels that are not zero ---
percentage = (numpy.count_nonzero(res) * 100)/ res.size
  • If img1 and img2 are similar most of the pixels in res would be 0 resulting in a lower percentage.如果img1img2相似,则res大多数像素将为0从而导致百分比较低。
  • If img1 and img2 are different this percentage would be higher.如果img1img2不同,这个百分比会更高。

Note: I have shown for a single channel image and the same can be extended for multi-channel images.注意:我已经展示了单通道图像,同样可以扩展到多通道图像。

You will need to calculate this on your own.您需要自行计算。 You will need the count of diferent pixels and the size of your original image then a simple math: (diferentPixelsCount / (mainImage.width * mainImage.height))*100您将需要不同像素的数量和原始图像的大小,然后进行简单的数学运算: (diferentPixelsCount / (mainImage.width * mainImage.height))*100

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python; 计算两个 json 之间的差异百分比 - Python; calculate the percentage of difference between two json 我如何使用 pil 在 python 中查看两个图像之间的区别? - how do i see the difference between two images in python using pil? 计算 Python pandas 中两个特定行之间的百分比差异 - Calculate the percentage difference between two specific rows in Python pandas 如何使用 python opencv 计算两个人之间的距离? - How to calculate distance between two person using python opencv? 如何从 2 个图像中检测激光线并使用 python opencv 计算其中心? - How do I detect laser line from 2 images and calculate its center using python opencv? 如何用熊猫计算两个数据框之间的百分比差异? - How to calculate percentage difference between two data frames with Pandas? 如何计算两个dataframe之间的匹配百分比差异 - How to calculate matching percentage difference between two dataframe 我如何计算 python 中两个日期和时间之间的差异 - how can i calculate the difference between two dates and times in python 如何在分钟内计算两次之间的差异 - python)How can I calculate difference between two times in mininute 如何在 python 中使用 365 天格式计算两个日期/时间列表之间的时差? - How can I calculate the time difference between two lists of dates/times using 365 day format in python?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM