python - 根据像素值比较多个图像
问题描述
嗨,我正在尝试比较一组图像,我想在其中生成具有每个单独图像的最大像素的最终图像。假设我有 3 个图像(转换为 10x10 矩阵),其中我知道每个块的像素值> 现在我想逐块比较这些值并生成一个最终的单独图像,其中每个图像的块值最大。
为此,我更改了图像尺寸(250x250),使得每个块都是 25x25 的正方形
我什至尝试比较两个图像并从两个图像中获取最大像素并显示它们
image = cv2.resize(im,(250,250))
hs,ws,c= image.shape
print(hs, ws,c)
hs = round(h/10)
ws = round(w/10)
resized = cv2.resize(image, (ws,hs), interpolation = cv2.INTER_AREA)
cv2.imshow("Resized image", resized)
cv2.waitKey(0)
cv2.destroyAllWindows()
print(list(resized))
#comparing two images
data = np.maximum.reduce([resized,resized1])
from matplotlib import pyplot as plt
plt.imshow(data, interpolation='nearest')
plt.show()
所以这是两个图像,平铺图像是块#comparing两个图像的结果。现在使用这两个图像,我想生成与原始照片类似的最终图像,而不是平铺版本,但最终照片应该具有来自两张原始照片的像素(每张照片的最大值)。我希望它能解决问题
解决方案
为了更好的可视化目的,我跳过了你之前得到的整个调整大小的部分,并坚持调整大小的(250 x 250)
图像。
我的方法如下:将所有调整大小的图像存储在某个具有尺寸的 NumPy 数组中(width x height x numberOfChannels x numberOfImages)
,然后使用 NumPy's max
along axis=3
,这样您就可以在所有图像上获得(width x height x numberOfChannels)
具有最大 BGR 值(或灰度,如果需要)的最终图像。
这是一些示例代码:
import cv2
import numpy as np
# Set up empty images array
width, height, nChannels, nImages = (250, 250, 3, 3)
images = np.zeros((width, height, nChannels, nImages), np.uint8)
# Read and resize exemplary input images
images[:, :, :, 0] = cv2.resize(cv2.imread('WeQow.png'), (width, height))
images[:, :, :, 1] = cv2.resize(cv2.imread('gIHOd.png'), (width, height))
images[:, :, :, 2] = cv2.resize(cv2.imread('lAdfO.jpg'), (width, height))
# Generate maximum image along last axis, i.e. the images.
# For each BGR value you get the maximum over all images.
image = images.max(axis=3)
# Show images
cv2.imshow('image0', images[:, :, :, 0])
cv2.imshow('image1', images[:, :, :, 1])
cv2.imshow('image2', images[:, :, :, 2])
cv2.imshow('image', image)
cv2.waitKey(0)
cv2.destroyAllWindows()
这些是三个输入图像:
最终的输出图像如下所示:
希望有帮助!
推荐阅读
- node.js - 无法从对话流代理调用部署在我的服务器上的 webhook
- .net - 如何在没有 Fiddler 的情况下查看来自 .NET 库的 https 请求?
- asp.net-core - 为什么 ASP.NET MVC 标签助手不能创建正确的链接?
- python - python:从字典中提取多个键
- python - 如果满足条件,则将图形添加到数据表单元格
- java - 为什么lucene不返回结果
- javascript - 无法获取要更新的 ReactJS 组件
- python - 在 csv 中写入函数的结果
- bash - 如何在 Linux 上实时查看文件内容?
- python - 如何在 Twisted Web 服务器中定义具有多个段的路径