Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
278 views
in Technique[技术] by (71.8m points)

python - 使用Python和Numpy创建原始图像的图像图块(m * n)(Creating image tiles (m*n) of original image using Python and Numpy)

I am using numpy to create tiles of (224*224) from my 16-bit tiff image (13777*16004).

(我正在使用numpy从我的16位tiff图像(13777 * 16004)创建(224 * 224)的图块。)

I was able to crop/slice into equal tiles of 224*224 along the rows and columns.

(我能够沿着行和列裁剪/切片为224 * 224相等的图块。)

I ran into problems while trying to create new tiles shifting by half of the tile size... For instance: A rough algorithm of what i am trying to achieve

(我在尝试创建新的,平铺一半大小的图块时遇到问题...例如:我要实现的粗略算法)

(1:224, 1:224)

((1:224,1:224))

(1:224, 112:336)

((1:224,112:336))

( , 224:448)

((224:448))

The goal is to retain tile size (224*224) while shifting by half of tile size to obtain more image tiles...

(目标是保留图块大小(224 * 224),同时移动图块大小的一半以获得更多图像图块...)

Snippet of code written to perform task

(编写用于执行任务的代码片段)

row_x =  img.shape[0]
column_y = img.shape[1]

tile_size_x = 224
tile_size_y = 224


range_x = mpz(ceil(row_x/tile_size_x))
range_y = mpz(ceil(column_y/tile_size_y))

for x in range(range_x, row_x):

    for y in range(range_y, column_y): 

        x0 = x * tile_size_x 

        x1 = int(x0/2) + tile_size_x

        y0 = y * tile_size_y 

        y1 = int(y0/2) + tile_size_y



        z = img[x0:x1, y0:y1]
        print (z.shape,z.dtype)

I keep getting wrong results, can anyone help ???

(我一直得到错误的结果,任何人都可以帮忙???)

  ask by Victor Alhassan translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You can use as_strided for this pretty efficiently I think.

(我认为您可以非常有效地使用as_strided 。)

def window_nd(a, window, steps = None):
    ashp = np.array(a.shape)
    wshp = np.array(window).reshape(-1)
    if steps:
        stp = np.array(steps).reshape(-1)
    else:
        stp = np.ones_like(ashp)
    astr = np.array(a.strides)
    assert np.all(np.r_[ashp.size == wshp.size, wshp.size == stp.size, wshp <= ashp])
    shape = tuple((ashp - wshp) // stp + 1) + tuple(wshp)
    strides = tuple(astr * stp) + tuple(astr)
    as_strided = np.lib.stride_tricks.as_strided
    aview = as_strided(a, shape = shape, strides = strides)
    return aview

EDIT: Generalizing the striding method as much as I can.

(编辑:尽可能广泛地概括跨步方法。)

For your specific question:

(对于您的特定问题:)

aview = window_nd(a, (288, 288), (144, 144))
z = aview.copy().reshape(-1, wx, wy) #to match expected output
print(z.shape, z.dtype) # z.shape should be (num_patches, 288, 288)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...