簡體   English   中英

將背面圖像分解和收集成100x100的python

[英]The decomposition and collection of back images to pieces 100x100 python

我有圖像陣列,我需要將其分解為100x100的片段,並對其進行處理后再收集回原始圖像。 問題:我收不到東西,我有這樣的東西,

但實際圖像是800x800

我的代碼:

獲取img作為數組,刪除第三維

path_to_image = './la3.jpg'
image_array = plt.imread(path_to_image)
image_array = image_array[:, :, 0]

寫成新的數組片段100x100(可以正常工作):

main2_array = np.zeros(10000,)
for row in tqdm_notebook(range(0,8)):

    for col in range(0,8):
        main2_array = np.vstack((main2_array, image_array[0 + (100*row):100 + (100*row) ,0 + (100*col):100 + (100*col)].flatten()))

main2_array = np.delete(main2_array, main2_array[0] , axis=0  )

收拾回來(不起作用)

main_array = np.zeros(100,)

for p in tqdm_notebook(range(0,100)):
    for i in range(0,64):
        main_array = np.vstack((main_array, main2_array[0 + (10000*i) + (100*p): 100 + (10000*i) + (100*p)]))

main_array = np.delete(main_array, main_array[0] , axis=0  )     

收集完零件后,我得到了

圖片

假像

x, y = 800,800
img_array = np.arange(x*y).reshape((x,y))

解構后, main2_array.shape為(64,10000); 每行都是扁平的100x100補丁。 解構期間,您從左到右從上到下遍歷圖像並將每個色塊滑動到前一個色塊之下。

要重構相反的過程:

main_array = np.zeros((x,y))
for n, patch in enumerate(main2_array):
    patch = patch.reshape(100,100)
    # eight patches per row
    row, col = divmod(n, 8)
    row_offset, col_offset = row*100, col*100
    row_slice = slice(row_offset, 100 + row_offset)
    col_slice = slice(col_offset, 100 + col_offset)
    #print(np.all(patch == image_array[row_slice,col_slice]))
    main_array[row_slice, col_slice] = patch


>>> np.all(main_array == img_array)
True
>>> 

或者,您也可以將其重塑為原始樣式

>>> b = main2_array.reshape(8,8,100,100)
>>> b[0,1].shape    # row zero column 1? 
(100, 100)
>>> np.all(b[0,1] == a[0:100, 100:200])
True
>>> 
>>> c = np.swapaxes(b, 1,2)
>>> c.shape
(8, 100, 8, 100)
>>> np.all(c[0,:,1,:] == a[0:100, 100:200])    # row zero column 1? 
True
>>> d = c.reshape(800,800)
>>> np.all(d==img_array)
True
>>>

有點晚了,但我認為這個問題值得一個更形象的答案。

您不需要慢循環,只需使用numpy重塑和精美索引就可以完成所有操作。

讓我們從示例圖片開始

import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt  
import skimage.transform
import skimage.data

img = skimage.data.chelsea()
# crop from (300, 451, 3) to (300, 300, 3)
img = img[:,80:380,:]
# resize to (800, 800)
img = skimage.transform.resize(img, (800,800))
plt.imshow(img)

在此處輸入圖片說明

將圖像分解為64 100*100圖塊。 新形狀為(8, 100, 8, 100, 3) ,您可以使用img[i, :, j, :, ...]處理單個圖像。 除了可能更容易閱讀之外,無需將它們存儲在新數組中。

img = img.reshape(8, 100, 8, 100, 3)
gs = mpl.gridspec.GridSpec(8,8)
for i in range(8):
    for j in range(8):
        ax = plt.subplot(gs[i,j])
        ax.imshow(img[i,:,j,:,...])

在此處輸入圖片說明

現在讓我們對磁貼進行一些操作。

清除一些隨機圖塊

cells = np.random.randint(8, size=(20,2))
img[cells[:,0],:,cells[:,1],...] = 1

上下顛倒翻轉

img = img[:,::-1,:,::-1,...]

添加黑色邊框

img[:,:6,...] = 0
img[:,-6:,...] = 0
img[:,:,:,:6,...] = 0
img[:,:,:,-6:,...] = 0

並繪制它們

for i in range(8):
    for j in range(8):
        ax = plt.subplot(gs[i,j])
        ax.imshow(img[i,:,j,:,...])

在此處輸入圖片說明

現在要重建,您可以將其重塑為原始形狀

img = img.reshape(800, 800, 3)
plt.imshow(img)

在此處輸入圖片說明

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM