首页 > 解决方案 > 根据像素值比较多个图像

问题描述

嗨,我正在尝试比较一组图像,我想在其中生成具有每个单独图像的最大像素的最终图像。假设我有 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两个图像的结果。现在使用这两个图像,我想生成与原始照片类似的最终图像,而不是平铺版本,但最终照片应该具有来自两张原始照片的像素(每张照片的最大值)。我希望它能解决问题

标签: pythonopencvimage-processingcomputer-vision

解决方案


为了更好的可视化目的,我跳过了你之前得到的整个调整大小的部分,并坚持调整大小的(250 x 250)图像。

我的方法如下:将所有调整大小的图像存储在某个具有尺寸的 NumPy 数组中(width x height x numberOfChannels x numberOfImages),然后使用 NumPy's maxalong 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()

这些是三个输入图像:

输入 1

输入 2

输入 3

最终的输出图像如下所示:

输出

希望有帮助!


推荐阅读