- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我有两张图片,我想让它们明显不同。我想为两个图像添加颜色,以便用户可以在一两秒内清楚地发现所有差异。
例如,这里有两张有一些不同的图片:
我目前使差异明显的方法是创建一个蒙版(两个图像之间的差异),将其涂成红色,然后将其添加到图像中。目标是用强烈的红色清楚地标记所有差异。这是我当前的代码:
import cv2
# load images
image1 = cv2.imread("leftImage.jpg")
image2 = cv2.imread("rightImage.jpg")
# compute difference
difference = cv2.subtract(image1, image2)
# color the mask red
Conv_hsv_Gray = cv2.cvtColor(difference, cv2.COLOR_BGR2GRAY)
ret, mask = cv2.threshold(Conv_hsv_Gray, 0, 255,cv2.THRESH_BINARY_INV |cv2.THRESH_OTSU)
difference[mask != 255] = [0, 0, 255]
# add the red mask to the images to make the differences obvious
image1[mask != 255] = [0, 0, 255]
image2[mask != 255] = [0, 0, 255]
# store images
cv2.imwrite('diffOverImage1.png', image1)
cv2.imwrite('diffOverImage2.png', image1)
cv2.imwrite('diff.png', difference)
当前代码的问题:计算出的掩码显示了一些差异,但并非全部(例如,请参见右上角的小块,或蓝色数据包上的绳索)。这些差异在计算出的蒙版中只显示得非常少,但它们应该像其他差异一样清晰地显示为红色。
输入:2 张有一些差异的图像。
预期输出:3 张图像:两张输入图像,但差异突出显示(以可配置的颜色清楚地突出显示),第三张图像仅包含差异( mask )。
最佳答案
方法 #1:结构相似性指数 (SSIM)
为了可视化两幅图像之间的差异,我们可以采用定量方法,使用在 Image Quality Assessment: From Error Visibility to Structural Similarity 中引入的结构相似性指数 (SSIM) 来确定图像之间的确切差异。 .此方法已在 scikit-image 中实现图像处理库。您可以使用 pip install scikit-image
安装 scikit-image
。
使用 skimage.metrics.structural_similarity
来自 scikit-image 的函数,它返回一个 score
和一个差异图像,diff
。 score
表示两个输入图像之间的结构相似性指数,可以落在范围 [-1,1] 之间,值越接近 1 表示相似性越高。但是由于您只对两个图像的不同之处感兴趣,因此我们将重点关注 diff
图像。具体来说,diff
图像包含实际图像差异,较暗区域的差异更大。较大的差异区域以黑色突出显示,而较小的差异区域以灰色突出显示。
所有差异->
显着区域差异
灰色嘈杂区域可能是由于 .jpg 有损压缩造成的。如果我们使用无损压缩图像格式,我们将获得更清晰的结果。比较两幅图像后的 SSIM 分数表明它们非常相似。
Image Similarity: 91.9887%
现在我们过滤 diff
图像,因为我们只想找到图像之间的较大差异。我们遍历每个轮廓,使用最小阈值区域进行过滤以去除灰色噪声,并使用边界框突出显示差异。这是结果。
为了可视化确切的差异,我们将轮廓填充到蒙版和原始图像上。
from skimage.metrics import structural_similarity
import cv2
import numpy as np
# Load images
before = cv2.imread('left.jpg')
after = cv2.imread('right.jpg')
# Convert images to grayscale
before_gray = cv2.cvtColor(before, cv2.COLOR_BGR2GRAY)
after_gray = cv2.cvtColor(after, cv2.COLOR_BGR2GRAY)
# Compute SSIM between the two images
(score, diff) = structural_similarity(before_gray, after_gray, full=True)
print("Image Similarity: {:.4f}%".format(score * 100))
# The diff image contains the actual image differences between the two images
# and is represented as a floating point data type in the range [0,1]
# so we must convert the array to 8-bit unsigned integers in the range
# [0,255] before we can use it with OpenCV
diff = (diff * 255).astype("uint8")
diff_box = cv2.merge([diff, diff, diff])
# 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]
contours = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
contours = contours[0] if len(contours) == 2 else contours[1]
mask = np.zeros(before.shape, dtype='uint8')
filled_after = after.copy()
for c in contours:
area = cv2.contourArea(c)
if area > 40:
x,y,w,h = cv2.boundingRect(c)
cv2.rectangle(before, (x, y), (x + w, y + h), (36,255,12), 2)
cv2.rectangle(after, (x, y), (x + w, y + h), (36,255,12), 2)
cv2.rectangle(diff_box, (x, y), (x + w, y + h), (36,255,12), 2)
cv2.drawContours(mask, [c], 0, (255,255,255), -1)
cv2.drawContours(filled_after, [c], 0, (0,255,0), -1)
cv2.imshow('before', before)
cv2.imshow('after', after)
cv2.imshow('diff', diff)
cv2.imshow('diff_box', diff_box)
cv2.imshow('mask', mask)
cv2.imshow('filled after', filled_after)
cv2.waitKey()
局限性:虽然此方法效果很好,但也有一些重要的局限性。两个输入图像必须具有相同的大小/尺寸,并且还存在一些问题,包括缩放、平移、旋转和扭曲。 SSIM 在模糊或嘈杂的图像上也表现不佳。对于不具有相同尺寸的图像,我们必须使用深度学习特征模型从识别像素相似性切换到识别对象相似性,而不是比较单个像素值。参见 checking images for similarity with OpenCV using Dense Vector Representations用于尺度不变和变换无关图像。
注意使用的scikit-image版本是0.18.1
。
方法 #2:cv2.absdiff
为了完整性,OpenCV 提供了一个非常简单的内置方法,使用 cv2.absdiff
但结果不如 SSIM,也没有计算两幅图像之间的相似度得分。此方法仅生成差异图像。
结果非常洗涤,仍然受到同样的限制。这种方法虽然简单很多,但还是推荐使用SSIM。
import cv2
# Load images as grayscale
image1 = cv2.imread("left.jpg", 0)
image2 = cv2.imread("right.jpg", 0)
# Calculate the per-element absolute difference between
# two arrays or between an array and a scalar
diff = 255 - cv2.absdiff(image1, image2)
cv2.imshow('diff', diff)
cv2.waitKey()
关于python - 使用 OpenCV Python 检测和可视化两个图像之间的差异,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56183201/
我正在尝试学习 Knockout 并尝试创建一个照片 uploader 。我已成功将一些图像存储在数组中。现在我想回帖。在我的 knockout 码(Javascript)中,我这样做: 我在 Jav
我正在使用 php 编写脚本。我的典型问题是如何在 mysql 中添加一个有很多替代文本和图像的问题。想象一下有机化学中具有苯结构的描述。 最有效的方法是什么?据我所知,如果我有一个图像,我可以在数据
我在两个图像之间有一个按钮,我想将按钮居中到图像高度。有人可以帮帮我吗? Entrar
下面的代码示例可以在这里查看 - http://dev.touch-akl.com/celebtrations/ 我一直在尝试做的是在 Canvas 上绘制 2 个图像(发光,然后耀斑。这些图像的链接
请检查此https://jsfiddle.net/rhbwpn19/4/ 图像预览对于第一篇帖子工作正常,但对于其他帖子则不然。 我应该在这里改变什么? function readURL(input)
我对 Canvas 有疑问。我可以用单个图像绘制 Canvas ,但我不能用单独的图像绘制每个 Canvas 。- 如果数据只有一个图像,它工作正常,但数据有多个图像,它不工作你能帮帮我吗? va
我的问题很简单。如何获取 UIImage 的扩展类型?我只能将图像作为 UIImage 而不是它的名称。图像可以是静态的,也可以从手机图库甚至文件路径中获取。如果有人可以为此提供一点帮助,将不胜感激。
我有一个包含 67 个独立路径的 SVG 图像。 是否有任何库/教程可以为每个路径创建单独的光栅图像(例如 PNG),并可能根据路径 ID 命名它们? 最佳答案 谢谢大家。我最终使用了两个答案的组合。
我想将鼠标悬停在一张图片(音乐专辑)上,然后播放一张唱片,所以我希望它向右移动并旋转一点,当它悬停时我希望它恢复正常动画片。它已经可以向右移动,但我无法让它随之旋转。我喜欢让它尽可能简单,因为我不是编
Retina iOS 设备不显示@2X 图像,它显示 1X 图像。 我正在使用 Xcode 4.2.1 Build 4D502,该应用程序的目标是 iOS 5。 我创建了一个测试应用(主/细节)并添加
我正在尝试从头开始以 Angular 实现图像 slider ,并尝试复制 w3school基于图像 slider 。 下面我尝试用 Angular 实现,谁能指导我如何使用 Angular 实现?
我正在尝试获取图像的图像数据,其中 w= 图像宽度,h = 图像高度 for (int i = x; i imageData[pos]>0) //Taking data (here is the pr
我的网页最初通过在 javascript 中动态创建图像填充了大约 1000 个缩略图。由于权限问题,我迁移到 suPHP。现在不用标准 标签本身 我正在通过这个 php 脚本进行检索 $file
我正在尝试将 python opencv 图像转换为 QPixmap。 我按照指示显示Page Link我的代码附在下面 img = cv2.imread('test.png')[:,:,::1]/2
我试图在这个 Repository 中找出语义分割数据集的 NYU-v2 . 我很难理解图像标签是如何存储的。 例如,给定以下图像: 对应的标签图片为: 现在,如果我在 OpenCV 中打开标签图像,
import java.util.Random; class svg{ public static void main(String[] args){ String f="\"
我有一张 8x8 的图片。 (位图 - 可以更改) 我想做的是能够绘制一个形状,给定一个 Path 和 Paint 对象到我的 SurfaceView 上。 目前我所能做的就是用纯色填充形状。我怎样才
要在页面上显示图像,你需要使用源属性(src)。src 指 source 。源属性的值是图像的 URL 地址。 定义图像的语法是: 在浏览器无法载入图像时,替换文本属性告诉读者她们失去的信息。此
**MMEditing是基于PyTorch的图像&视频编辑开源工具箱,支持图像和视频超分辨率(super-resolution)、图像修复(inpainting)、图像抠图(matting)、
我正在尝试通过资源文件将图像插入到我的程序中,如下所示: green.png other files 当我尝试使用 QImage 或 QPixm
我是一名优秀的程序员,十分优秀!