如何从python中的3D图像中提取高速缓存? [英] How to extract paches from 3D image in python?

查看:123
本文介绍了如何从python中的3D图像中提取高速缓存?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的3D图像尺寸为:深x重量x高度(例如: 10x20x30 ,表示10图像,每张图片的大小 20x30

I have a 3D image with size: Deep x Weight x Height (for example: 10x20x30, means 10 images, and each image has size 20x30.

鉴于补丁大小为 pd x pw x ph (例如 pd< Deep,pw< Weight,ph< Height ),例如补丁大小: 4x4x4 。路径的中心点位置为: pd / 2 x pw / 2 x ph / 2 。让我们调用之间的距离时间 t 和时间 t + 1 中心点为 stride ,例如 stride = 2

Given a patch size is pd x pw x ph (such as pd <Deep, pw<Weight, ph<Height), for example patch size: 4x4x4. The center point location of the path will be: pd/2 x pw/2 x ph/2. Let's call the distance between time t and time t+1 of the center point be stride, for example stride=2.

我想将原始3D图像提取为大小和上面给出的步幅。我怎么能在python中做到?谢谢

I want to extract the original 3D image into patches with size and stride given above. How can I do it in python? Thank you

推荐答案

使用 np.lib.stride_trick s.as_strided 。该解决方案要求步幅划分输入堆栈的相应尺寸。它甚至允许重叠补丁(在这种情况下,不要写入结果,或者复制。)。因此它比其他方法更灵活:

Use np.lib.stride_tricks.as_strided. This solution does not require the strides to divide the corresponding dimensions of the input stack. It even allows for overlapping patches (Just do not write to the result in this case, or make a copy.). It therefore is more flexible than other approaches:

import numpy as np
from numpy.lib import stride_tricks

def cutup(data, blck, strd):
    sh = np.array(data.shape)
    blck = np.asanyarray(blck)
    strd = np.asanyarray(strd)
    nbl = (sh - blck) // strd + 1
    strides = np.r_[data.strides * strd, data.strides]
    dims = np.r_[nbl, blck]
    data6 = stride_tricks.as_strided(data, strides=strides, shape=dims)
    return data6#.reshape(-1, *blck)

#demo
x = np.zeros((5, 6, 12), int)
y = cutup(x, (2, 2, 3), (3, 3, 5))

y[...] = 1
print(x[..., 0], '\n')
print(x[:, 0, :], '\n')
print(x[0, ...], '\n')

输出:

[[1 1 0 1 1 0]
 [1 1 0 1 1 0]
 [0 0 0 0 0 0]
 [1 1 0 1 1 0]
 [1 1 0 1 1 0]] 

[[1 1 1 0 0 1 1 1 0 0 0 0]
 [1 1 1 0 0 1 1 1 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0 0 0]
 [1 1 1 0 0 1 1 1 0 0 0 0]
 [1 1 1 0 0 1 1 1 0 0 0 0]] 

[[1 1 1 0 0 1 1 1 0 0 0 0]
 [1 1 1 0 0 1 1 1 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0 0 0]
 [1 1 1 0 0 1 1 1 0 0 0 0]
 [1 1 1 0 0 1 1 1 0 0 0 0]
 [0 0 0 0 0 0 0 0 0 0 0 0]] 

说明。 Numpy数组按步幅组织,每个维度一个,数据点[x,y,z]位于内存地址库+ stridex * x + stridey * y + stridez * z。

Explanation. Numpy arrays are organised in terms of strides, one for each dimension, data point [x,y,z] is located in memory at address base + stridex * x + stridey * y + stridez * z.

stride_tricks.as_strided 工厂允许直接操作与给定数组共享其内存的新数组的步幅和形状。只有当你知道自己在做什么时才尝试这个,因为没有进行任何检查,这意味着你可以通过解决越界记忆来射击你的脚。

The stride_tricks.as_strided factory allows to directly manipulate the strides and shape of a new array sharing its memory with a given array. Try this only if you know what you're doing because no checks are performed, meaning you are allowed to shoot your foot by addressing out-of-bounds memory.

代码使用此函数将三个现有维度中的每一个拆分为两个新维度,一个用于块坐标内的对应(这将与原始维度具有相同的步幅,因为块中的相邻点与整个中的相邻点相对应堆栈)和沿该轴的块索引的一个维度;这将有stride =原始步幅x块步幅。

The code uses this function to split up each of the three existing dimensions into two new ones, one for the corresponding within block coordinate (this will have the same stride as the original dimension, because adjacent points in a block corrspond to adjacent points in the whole stack) and one dimension for the block index along this axis; this will have stride = original stride x block stride.

所有代码都是计算正确的步幅和尺寸(=沿三个轴的块尺寸和块计数)。

All the code does is computing the correct strides and dimensions (= block dimensions and block counts along the three axes).

由于数据与原始数组共享,当我们将6d数组的所有点都设置为1时,它们也会在原始数组中设置,从而暴露出块结构演示。请注意,函数最后一行中注释掉的 reshape 会破坏此链接,因为它会强制复制。

Since the data are shared with the original array, when we set all points of the 6d array to 1, they are also set in the original array exposing the block structure in the demo. Note that the commented out reshape in the last line of the function breaks this link, because it forces a copy.

这篇关于如何从python中的3D图像中提取高速缓存?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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