gpt4 book ai didi

python - 组合不同图像的 block 并产生新图像

转载 作者:太空宇宙 更新时间:2023-11-03 22:36:06 25 4
gpt4 key购买 nike

我有六张照片。我将它们更改为 block 结构。考虑一张尺寸为 200x200 的图像。1. 我转换成 10x10 的 block ,所以现在我总共有 400 个 block ,每个大小为 10x10。我为每张图片都做了这个。2.然后我找到每个 block 的平均值。再次为所有图像。3.找到所有 block 的平均值的最大值。所以现在我有一个长度为 400 的数组,其中每个值都是 block 值的最大值。4. 还找到了这些最​​大值的索引( block 数和图像数)

现在使用这个图像和 block 号我想生成一个图像,这样它应该从原始图像中挑选 block 并将它们放入空图像中以制作最终图像。我认为的一种方法是逐行挑选 block 并将其与原始图像匹配。然后将该 block 放置在空图像中,以便将相同的 block 放置在空图像中作为原始图像。这可能是错误的,但这只是我的一个想法,但我仍然不知道如何进行。

img = [cv2.imread(file,0) for file in glob.glob("resized/*.jpg")]
X=[]
for im in img:
arr = np.asarray(im)
arr = np.split(arr, 20)
arr = np.array([np.split(x, 20, 1) for x in arr])
mat = [arr[i][j].mean() for i in range(20) for j in range(20)]
X.append(mat)
max_X = list(zip(*X))
result = [max(i) for i in max_X]
print(result)
image_number = np.argmax(X,axis=0)
print(image_number)
result1 = [(row.index(max(row)),index) for index, row in enumerate(max_X)]
print(result1)
[(0, 0), (0, 1), (0, 2), (0, 3), (0, 4), (0, 5), (0, 6), (0, 7), (0, 8), (0, 9), (0, 10), (0, 11), (0, 12), (0, 13), (0, 14), (0, 15), (0, 16), (0, 17), (0, 18), (0, 19), (0, 20), (0, 21), (0, 22), (0, 23), (0, 24), (0, 25), (0, 26), (0, 27), (0, 28), (0, 29), (0, 30), (0, 31), (0, 32), (0, 33), (0, 34), (0, 35), (0, 36), (0, 37), (0, 38), (0, 39), (0, 40), (0, 41), (0, 42), (0, 43), (0, 44), (0, 45), (1, 46), (2, 47), (0, 48), (0, 49), (0, 50), (0, 51), (0, 52), (0, 53), (0, 54), (0, 55), (0, 56), (0, 57), (0, 58), (0, 59), (0, 60), (0, 61), (0, 62), (0, 63), (1, 64), (0, 65), (2, 66), (2, 67), (0, 68), (0, 69), (0, 70), (0, 71), (0, 72), (0, 73), (0, 74), (0, 75), (0, 76), (0, 77), (0, 78), (0, 79), (0, 80), (0, 81), (0, 82), (4, 83), (0, 84), (1, 85), (0, 86), (0, 87), (0, 88), (0, 89), (0, 90), (0, 91), (0, 92), (0, 93), (0, 94), (0, 95), (2, 96), (0, 97), (0, 98), (0, 99), (0, 100), (3, 101), (2, 102), (2, 103), (0, 104), (0, 105), (3, 106), (3, 107), (0, 108), (4, 109), (5, 110), (0, 111), (4, 112), (0, 113), (4, 114), (0, 115), (4, 116), (0, 117), (0, 118), (0, 119), (0, 120), (5, 121), (2, 122), (2, 123), (0, 124), (0, 125), (0, 126), (4, 127), (0, 128), (0, 129), (0, 130), (0, 131), (0, 132), (0, 133), (0, 134), (0, 135), (2, 136), (4, 137), (0, 138), (0, 139), (0, 140), (2, 141), (2, 142), (5, 143), (0, 144), (0, 145), (4, 146), (5, 147), (0, 148), (0, 149), (0, 150), (0, 151), (0, 152), (0, 153), (0, 154), (0, 155), (0, 156), (3, 157), (0, 158), (0, 159), (0, 160), (5, 161), (1, 162), (0, 163), (5, 164), (0, 165), (5, 166), (0, 167), (0, 168), (0, 169), (0, 170), (0, 171), (0, 172), (0, 173), (0, 174), (0, 175), (4, 176), (0, 177), (4, 178), (0, 179), (5, 180), (5, 181), (0, 182), (0, 183), (3, 184), (5, 185), (5, 186), (0, 187), (0, 188), (0, 189), (0, 190), (0, 191), (0, 192), (0, 193), (0, 194), (0, 195), (0, 196), (0, 197), (0, 198), (0, 199), (2, 200), (5, 201), (0, 202), (0, 203), (0, 204), (4, 205), (0, 206), (0, 207), (0, 208), (0, 209), (0, 210), (0, 211), (0, 212), (0, 213), (0, 214), (0, 215), (4, 216), (0, 217), (0, 218), (0, 219), (5, 220), (3, 221), (2, 222), (0, 223), (5, 224), (4, 225), (4, 226), (0, 227), (0, 228), (0, 229), (0, 230), (0, 231), (4, 232), (4, 233), (0, 234), (3, 235), (0, 236), (0, 237), (0, 238), (0, 239), (3, 240), (5, 241), (1, 242), (0, 243), (4, 244), (0, 245), (5, 246), (0, 247), (4, 248), (0, 249), (0, 250), (4, 251), (4, 252), (3, 253), (0, 254), (0, 255), (0, 256), (0, 257), (0, 258), (0, 259), (0, 260), (5, 261), (5, 262), (2, 263), (0, 264), (0, 265), (3, 266), (2, 267), (0, 268), (0, 269), (3, 270), (5, 271), (2, 272), (0, 273), (0, 274), (0, 275), (0, 276), (0, 277), (0, 278), (0, 279), (4, 280), (4, 281), (2, 282), (1, 283), (0, 284), (0, 285), (3, 286), (2, 287), (0, 288), (0, 289), (5, 290), (2, 291), (2, 292), (0, 293), (0, 294), (0, 295), (0, 296), (0, 297), (0, 298), (0, 299), (0, 300), (0, 301), (4, 302), (4, 303), (1, 304), (1, 305), (0, 306), (0, 307), (0, 308), (0, 309), (4, 310), (2, 311), (3, 312), (0, 313), (0, 314), (0, 315), (1, 316), (0, 317), (0, 318), (0, 319), (0, 320), (0, 321), (0, 322), (0, 323), (1, 324), (2, 325), (2, 326), (2, 327), (0, 328), (0, 329), (0, 330), (4, 331), (0, 332), (2, 333), (2, 334), (0, 335), (0, 336), (0, 337), (0, 338), (0, 339), (0, 340), (0, 341), (0, 342), (0, 343), (0, 344), (0, 345), (0, 346), (2, 347), (0, 348), (0, 349), (0, 350), (0, 351), (0, 352), (0, 353), (0, 354), (0, 355), (0, 356), (0, 357), (0, 358), (0, 359), (0, 360), (0, 361), (0, 362), (0, 363), (0, 364), (0, 365), (0, 366), (0, 367), (0, 368), (0, 369), (0, 370), (0, 371), (0, 372), (0, 373), (0, 374), (0, 375), (0, 376), (0, 377), (0, 378), (0, 379), (0, 380), (0, 381), (0, 382), (0, 383), (0, 384), (0, 385), (0, 386), (0, 387), (0, 388), (0, 389), (0, 390), (0, 391), (0, 392), (0, 393), (0, 394), (0, 395), (0, 396), (0, 397), (0, 398), (0, 399)]

在此我们有图像编号和 block 编号。现在我想从图像 0 中选择 block 0 并放入空图像等等。我希望这清除了预期的输出。

最佳答案

那么,这是我解决您的问题的方法。我重写了您的部分代码以删除所有列表,并且只处理 NumPy 数组。因此,我将所有图像存储在一个 4D 数组中,并将所有计算出的“ block 均值”存储在一个 3D 数组中。最后,我使用找到的 image_number 数组通过使用 OpenCV 的 resize 生成某种“索引数组”。使用 INTER_AREA 的方法插值标志(“使用像素区域关系重采样”)。这样做时,可以使用 NumPy 的 boolean array indexing 非常轻松地生成最终图像。 .

让我们看一下下面的代码:

import cv2
import numpy as np

# Read images in one single 4D array; resize to [200, 200]
nImages = 3
images = np.zeros((200, 200, 3, nImages), np.uint8)
images[:, :, :, 0] = cv2.resize(cv2.imread('U2Gmz.png', cv2.IMREAD_COLOR), (200, 200))
images[:, :, :, 1] = cv2.resize(cv2.imread('OZxf3.png', cv2.IMREAD_COLOR), (200, 200))
images[:, :, :, 2] = cv2.resize(cv2.imread('aISEB.png', cv2.IMREAD_COLOR), (200, 200))

# Calculate block means and store in one single 3D array
means = np.zeros((20, 20, nImages), np.uint8)
for im in range(nImages):
arr = np.split(images[:, :, :, im], 20)
arr = np.array([np.split(x, 20, 1) for x in arr])
means[:, :, im] = np.reshape([arr[i][j].mean() for i in range(20) for j in range(20)], (20, 20))

# Determine block mean maximum over all images
result = np.max(means, axis=2)

# Determine index of block mean maximum over all images
image_number = np.argmax(means, axis=2)
print(image_number)

# Resize index array with "resampling using pixel area relation" to final image size
image_number_idx = cv2.resize(np.uint8(image_number), (200, 200), interpolation=cv2.INTER_AREA)

# Generate final image by boolean array indexing
final = np.zeros((200, 200, 3), np.uint8)
for im in range(nImages):
idx = image_number_idx == im
final[idx, :] = images[idx, :, im]

# Show images
cv2.imshow('image1', images[:, :, :, 0])
cv2.imshow('image2', images[:, :, :, 1])
cv2.imshow('image3', images[:, :, :, 2])
cv2.imshow('final', final)

cv2.waitKey(0)
cv2.destroyAllWindows()

这些是使用过的图片:

Input 1

Input 2

Input 3

image_number 输出给出了这个:

[[0 0 0 1 1 1 0 0 0 0 0 0 0 1 0 0 0 0 0 0]
[1 0 0 0 1 0 0 0 0 0 0 0 0 1 1 1 1 0 0 0]
[1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 1 1 0 0 0]
[0 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1]
[0 0 0 1 0 0 0 0 0 1 0 0 0 0 0 0 1 1 1 0]
[0 0 0 0 0 0 0 2 1 1 1 2 0 0 0 0 0 1 0 0]
[0 0 0 0 0 0 0 2 1 0 0 2 2 2 0 0 0 1 1 0]
[0 0 0 0 0 2 2 2 1 0 2 2 2 2 0 0 0 1 1 0]
[0 0 0 0 0 2 2 2 0 0 0 0 2 2 2 0 0 0 0 0]
[0 0 0 0 2 2 2 2 0 0 0 0 2 2 2 2 0 0 0 0]
[0 0 0 0 2 0 2 2 0 0 0 0 2 0 0 0 0 0 0 0]
[1 1 0 0 0 2 2 0 0 2 2 0 0 2 0 0 0 0 0 0]
[1 1 0 0 2 2 2 0 2 2 2 2 1 2 2 2 2 0 2 1]
[1 0 0 0 0 2 2 2 2 0 2 2 2 2 2 2 0 1 1 1]
[1 1 1 0 0 2 2 2 1 1 1 2 2 2 2 0 0 1 1 0]
[1 1 1 1 1 1 1 1 1 1 1 2 0 0 1 0 0 0 0 0]
[1 1 1 1 1 1 0 1 1 1 1 1 0 2 0 0 0 0 0 0]
[1 1 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
[1 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0]
[0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1]]

最后的图像是这样的:

Output

希望我正确理解了您的问题,这就是您想要实现的目标。

我的假设是,所有输入图像都具有相同的图像尺寸,此处为 (200, 200)。否则,我想不出一种方法来管理可能不同的 block 大小,如果这里的“网格”(20, 20) 是固定的。

希望对您有所帮助!

编辑:要读取给定文件夹中的所有 jpg 文件,您可以使用:

files = glob.glob('resized/*.jpg')

# Read images in one single 4D array; resize to [200, 200]
nImages = len(files)
images = np.zeros((200, 200, 3, nImages), np.uint8)
for im in range(nImages):
images[:, :, :, im] = cv2.resize(cv2.imread(files[im], cv2.IMREAD_COLOR), (200, 200))

关于python - 组合不同图像的 block 并产生新图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58369483/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com