使用Python和Numpy创建原始图像的图像切片(m * n) [英] Creating image tiles (m*n) of original image using Python and Numpy

查看:487
本文介绍了使用Python和Numpy创建原始图像的图像切片(m * n)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用numpy从我的16位tiff图像(13777 * 16004)创建(224 * 224)的图块。我能够沿行和列裁剪/切片成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, 1:224)

(1:224,112:336)

(1:224, 112:336)

(,224:448)

目标是在移动一半的瓷砖时保留瓷砖尺寸(224 * 224)获取更多图像图块的大小...

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

推荐答案

在计算for循环的范围时你稍微偏了一点。必须使用两个切片之间的偏移量计算切片的数量,在您的情况下, x0 / 2 ,我已经简化了代码并定义了一些有意义的变量您可以配置以从给定图像中获取所需的图块:

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:

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)

作为当前偏移,如果图像尺寸的精确倍数为512x512,那么我们将获得4个相同尺寸的图块:

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

如果偏移量不是图像尺寸的精确倍数,则更改偏移量的值会得到不规则尺寸的切片,如果不需要,您可以稍后通过在<$ c中将 math.ceil 更改为 math.floor 来过滤这些图块$ c> for loop。

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.

这篇关于使用Python和Numpy创建原始图像的图像切片(m * n)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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