合并不同图像的块并生成新图像 [英] Combine blocks of different images and produce a new image

查看:68
本文介绍了合并不同图像的块并生成新图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have six photographs. I changed them into block structure. Consider an image of size 200x200. 1. I converted into blocks of 10x10 so now I have 400 blocks in total each of size 10x10. I did this for each image. 2. Then I found the mean value for each block. Again for all images. 3. Found the maximum of mean values of all blocks. So now I have a array of length 400 where each value is the maximum of block values. 4. Also found the indices of these maximum value(blocks number and the image number)

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

Now using this image and block number I want to produce an image such that it should pick blocks from original images and put them into empty images to make the final image. One approach I thought was to pick blocks by row and match it against the original image. Then place that block in the empty image so that same block is placed in the empty image as the original image. This could be wrong but just an idea I had but still I don't know how to proceed with this.

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)]

在此我们具有图像编号和块编号.现在我想从图像0中选择块0,然后放入空白图像,依此类推.我希望这可以清除预期的输出.

In this we have the image number and the block number. Now I want to pick block 0 from image 0 and put in empty image and so on. I hope this clears what is the expected output.

推荐答案

所以,这是我解决您的问题的方法.我重写了部分代码以摆脱所有列表,仅在NumPy数组上工作.因此,我将所有图像存储在4D数组中,并将所有计算出的块均值"存储在3D数组中.最后,我使用发现的image_number数组通过OpenCV的 resize 方法noreferrer> INTER_AREA 插值标志(使用像素面积关系进行重采样" ).这样,您可以使用NumPy的布尔数组索引.

So, here's my approach to your problem. I rewrote parts of your code to get rid of all the lists, and solely working on NumPy arrays. Therefore, I store all images in a 4D array, and store all calculated "block means" in a 3D array. Finally, I use the found image_number array to generate some kind of "index array" by using OpenCV's resize method with INTER_AREA interpolation flag ("resampling using pixel area relation"). In doing so, the generation of your final image can be done very easily using NumPy's boolean array indexing.

让我们看一下以下代码:

Let's have a look at the following code:

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()

这些是使用过的图像:

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]]

然后,最终图像如下所示:

And, the final image looks like this:

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

I hope, I understood your question correctly, and this is what you wanted to achieve.

我的假设是,所有输入图像都具有相同的图像尺寸,此处为(200, 200).否则,我想不出一种方法来管理可能变化的块大小,只要固定了(20, 20)这里的网格"即可.

My assumption is, that all input images have the same image dimensions, (200, 200) here. Otherwise, I couldn't think of a way to manage potentially varying block sizes, if just the "grid", (20, 20) here, is fixed.

希望有帮助!

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

To read all jpg files from the given folder, you might use:

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))

这篇关于合并不同图像的块并生成新图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆