如果 absdiff() 相同图像(只是稍微垂直移动)差异 > 则正确15%?

发布于 2025-01-11 23:10:10 字数 1114 浏览 0 评论 0原文

如果我有 2 个几乎完全相同的图像(除了其中一个图像的所有像素向上移动 2 个像素),并且我使用 cv2.absdiff() 比较它们,则差异 > > 15%。这对我来说听起来不太合适?

我本以为差距会小很多。这是正确的吗?还是我的数学不正确?

以下是我的代码:

def calculate_difference_measure():
    # Load in same image twice
    img1 = cv2.imread('./i1.png')
    img2 = img1.copy()

    # Translate one image slightly up
    img2 = imutils.translate(img2, 0, 2)
    
    # Difference the 2 images and get all values below 15
    diff = cv2.absdiff(img1, img2)
    gray_diff = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
    thresh_diff = cv2.threshold(gray_diff, 15, 255, cv2.THRESH_BINARY)[1]
    
    # Calculate the difference between the 2 images
    total_pixels = img1.shape[0] * img1.shape[1] * 1.0
    diff_on_pixels = cv2.countNonZero(thresh_diff) * 1.0
    difference_measure = diff_on_pixels / total_pixels
    # Outputs 'difference_measure: 0.1768186919702071'
    print('difference_measure: {}'.format(difference_measure))

我的代码用于比较的测试图像。
输入图片此处描述

If I have 2 images that are pretty much exactly the same (except one image has all its pixels shifted 2 pixels up) and I compare them using cv2.absdiff() the difference is > 15%. That doesn't sound right to me?

I was thinking the difference would be much lower. Is this correct? Or is my maths incorrect?

Below is my code:

def calculate_difference_measure():
    # Load in same image twice
    img1 = cv2.imread('./i1.png')
    img2 = img1.copy()

    # Translate one image slightly up
    img2 = imutils.translate(img2, 0, 2)
    
    # Difference the 2 images and get all values below 15
    diff = cv2.absdiff(img1, img2)
    gray_diff = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
    thresh_diff = cv2.threshold(gray_diff, 15, 255, cv2.THRESH_BINARY)[1]
    
    # Calculate the difference between the 2 images
    total_pixels = img1.shape[0] * img1.shape[1] * 1.0
    diff_on_pixels = cv2.countNonZero(thresh_diff) * 1.0
    difference_measure = diff_on_pixels / total_pixels
    # Outputs 'difference_measure: 0.1768186919702071'
    print('difference_measure: {}'.format(difference_measure))

Test image that my code uses to compare.
enter image description here:

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浮华 2025-01-18 23:10:10

是的,据我从 cv2.absdiff 的文档中了解到,它计算像素的差异。为了形象化,我创建了一个棋盘并移动了 1 行 - 这导致了 100% 的差异。

import cv2
import imutils
import numpy as np


def calculate_difference_measure(img1: np.array, img2: np.array) -> float:
    diff = cv2.absdiff(img1, img2)
    thresh_diff = cv2.threshold(diff, 15, 255, cv2.THRESH_BINARY)[1]

    # Calculate the difference between the 2 images
    total_pixels = img1.shape[0] * img1.shape[1] * 1.0
    diff_on_pixels = cv2.countNonZero(thresh_diff) * 1.0
    difference_measure = diff_on_pixels / total_pixels
    print('difference_measure: {}'.format(difference_measure))
    return difference_measure


def main():
    chess1 = np.uint8(([150, 50] * 4 + [50, 150] * 4) * 4).reshape((8, 8))
    chess2 = imutils.translate(chess1.copy(), 0, 1)

    calculate_difference_measure(img1=chess1, img2=chess1)  # 0 diff
    calculate_difference_measure(img1=chess1, img2=chess2)  # 1 diff (100% different)

    chess1 = cv2.resize(chess1, (400, 400), interpolation=cv2.INTER_AREA)
    chess2 = cv2.resize(chess2, (400, 400), interpolation=cv2.INTER_AREA)
    cv2.imshow('original', chess1)
    cv2.imshow('translated', chess2)
    cv2.waitKey(0)
    return


if __name__ == '__main__':
    main()

输入图片此处描述

Yes, as far as I understood from the documentation of cv2.absdiff, it calculates the difference of pixels. to visualize i created a chessboard and shifted 1 row - that resulted in 100% difference.

import cv2
import imutils
import numpy as np


def calculate_difference_measure(img1: np.array, img2: np.array) -> float:
    diff = cv2.absdiff(img1, img2)
    thresh_diff = cv2.threshold(diff, 15, 255, cv2.THRESH_BINARY)[1]

    # Calculate the difference between the 2 images
    total_pixels = img1.shape[0] * img1.shape[1] * 1.0
    diff_on_pixels = cv2.countNonZero(thresh_diff) * 1.0
    difference_measure = diff_on_pixels / total_pixels
    print('difference_measure: {}'.format(difference_measure))
    return difference_measure


def main():
    chess1 = np.uint8(([150, 50] * 4 + [50, 150] * 4) * 4).reshape((8, 8))
    chess2 = imutils.translate(chess1.copy(), 0, 1)

    calculate_difference_measure(img1=chess1, img2=chess1)  # 0 diff
    calculate_difference_measure(img1=chess1, img2=chess2)  # 1 diff (100% different)

    chess1 = cv2.resize(chess1, (400, 400), interpolation=cv2.INTER_AREA)
    chess2 = cv2.resize(chess2, (400, 400), interpolation=cv2.INTER_AREA)
    cv2.imshow('original', chess1)
    cv2.imshow('translated', chess2)
    cv2.waitKey(0)
    return


if __name__ == '__main__':
    main()

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文