python 三维图片 任意切片,使用Python和Numpy创建原始图像的图像切片(m * n)

本文围绕使用Python对16位tiff图像进行切片展开。作者最初尝试将图像沿行列切成224*224的相等切片,在尝试以切片大小一半的偏移量创建新切片时遇到问题。随后给出解决方案,简化代码并定义可配置变量以从给定图像获取所需切片。

I am using numpy to create tiles of (224*224) from my 16-bit tiff image (13777*16004). I was able to crop/slice into equal tiles of 224*224 along the rows and columns. 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, 112:336)

( , 224:448)

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

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

解决方案

You went a little off while calculating the range of your for loop. The number of slices to be made, must be calculated using the offset between two slices, which is x0/2 in your case, I have simplified your code and defined some meaningful variables which you can configure to get desired tiles from a given image:

18c06f3a6341075e5381c7231d8496a1.png

import cv2

import math

img = cv2.imread("/path/to/lena.png") # 512x512

img_shape = img.shape

tile_size = (256, 256)

offset = (256, 256)

for i in xrange(int(math.ceil(img_shape[0]/(offset[1] * 1.0)))):

for j in xrange(int(math.ceil(img_shape[1]/(offset[0] * 1.0)))):

cropped_img = img[offset[1]*i:min(offset[1]*i+tile_size[1], img_shape[0]), offset[0]*j:min(offset[0]*j+tile_size[0], img_shape[1])]

# Debugging the tiles

cv2.imwrite("debug_" + str(i) + "_" + str(j) + ".png", cropped_img)

As current offset if exact multiple of image dimensions, which is 512x512, hence we will get 4 tiles of same size:

95e8ec5c03b02eaa8ff43295ea182a0f.png6c5e273c65b730ca3969c5d614e9c9f1.pngdf07d1f0cc5cef05bc16a9028f44344a.png2b5043638a07318a0bcbf5ce53a89e2c.png

Changing the value of offset, would get you tiles of irregular size, if the offset if not exact multiple of the image dimensions, you may later filter those tiles if not required by changing the math.ceil to math.floor in the for loop.

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值