如何使用CV2以一种格式获得两个图像的差异,以便以后重新使用?

发布于 2025-01-25 12:43:28 字数 242 浏览 1 评论 0 原文

我希望使用Python中的CV2获得IMG1和IMG2之间的区别,以此可以重新申请与IMG1的区别。

本质上:差异= img2 -img1

的格式,我以后可以声明: img2 = img1 +差异

我很清楚CV2功能脓肿,减去和添加。但是,我尚未设法以允许以后添加的格式提取差异。

任何帮助都非常感谢。

PS如果有人想知道为什么?差异将通过插座发送,并重新应用于IMG1,该IMG1先前发送或构造。

I am looking to get the difference between img1 and img2 using cv2 in python, in a way that I can reapply the difference to img1.

Essentially: Difference = img2 - img1

In a format where I can later declare:
img2 = img1 + difference

I am well aware of the cv2 functions absdiff, subtract and add. However I have not managed to extract the diff in a format that would allow addition later on.

Any help is much appreciated.

P.S. In case anybody is wondering why? The difference would be sent over socket and reapplied to img1 which was sent or constructed prior.

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

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

发布评论

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

评论(1

開玄 2025-02-01 12:43:28

原始图像使用dtype uint8 带有值 0..255 ,但是当您提取时,您可以获得value -255..255..255..255 (( 0-255)..(255-0),并且需要转换为 int16

import cv2

img1 = cv2.imread('lenna.png')
print('img1:', img1.shape, img1.dtype)

img2 = cv2.imread('lenna_flip.png')
print('img2:', img2.shape, img2.dtype)

diff = img2.astype('int16') - img1.astype('int16')
print('diff:', diff.shape, diff.dtype)

cv2.imshow('diff', diff)
cv2.waitKey(0)

back = (img1.astype('int16') + diff).astype('uint8')
print('back:', back.shape, back.dtype)

cv2.imshow('back', back)
cv2.waitKey(0)

cv2.destroyAllWindows()

back 给出正确的图像,但 diff 给出灰色图像(因为它具有值-255 ... 255,它在显示时将其转换为 0..255 ),因此显示它是没有用的。

但是,现在您必须使用 int16 发送数组 diff ,因此它可能需要发送更多字节然后使用原始图像,并且发送原始图像可能会更快。


图像 lenna 来自wikipedia

lenna.png

https://i.sstatic.net/hwdhx.png

” href =“ https://i.sstatic.net/wnuwg.png” rel =“ nofollow noreferrer”>

Original image uses dtype uint8 with values 0..255 but when you substract then you can get values -255..255 (0-255)..(255-0) and it needs to convert to int16

import cv2

img1 = cv2.imread('lenna.png')
print('img1:', img1.shape, img1.dtype)

img2 = cv2.imread('lenna_flip.png')
print('img2:', img2.shape, img2.dtype)

diff = img2.astype('int16') - img1.astype('int16')
print('diff:', diff.shape, diff.dtype)

cv2.imshow('diff', diff)
cv2.waitKey(0)

back = (img1.astype('int16') + diff).astype('uint8')
print('back:', back.shape, back.dtype)

cv2.imshow('back', back)
cv2.waitKey(0)

cv2.destroyAllWindows()

back gives correct image but diff gives gray image (because it has values -255...255 which it converts to 0..255 when it displays) so it is useless to display it.

But now you have to send array diff with int16 so it may need to send more bytes then using original image and maybe it would be faster to send original image.


Image Lenna from Wikipedia

lenna.png

enter image description here

lenna_flip.png

enter image description here

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