如何从python中的3D图像中提取补丁?

How to extract paches from 3D image in python?(如何从python中的3D图像中提取补丁?)
本文介绍了如何从python中的3D图像中提取补丁?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

问题描述

我有一个尺寸为:Deep x Weight x Height 的 3D 图片(例如: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 ),例如补丁大小:<代码>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_tricks.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], '
')
print(x[:, 0, :], '
')
print(x[0, ...], '
')

输出:

[[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] 位于内存地址 base + 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.

代码使用此函数将三个现有维度中的每一个拆分为两个新维度,一个用于对应的块内坐标(这将与原始维度具有相同的步幅,因为块中的相邻点对应于相邻的整个堆栈中的点)和沿该轴的块索引的一维;这将具有步幅 = 原始步幅 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图像中提取补丁?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

Reading *.mhd/*.raw format in python(在 python 中读取 *.mhd/*.raw 格式)
Count number of cells in the image(计算图像中的单元格数)
How to detect paragraphs in a text document image for a non-consistent text structure in Python OpenCV(如何在 Python OpenCV 中检测文本文档图像中的段落是否存在不一致的文本结构)
How to get the coordinates of the bounding box in YOLO object detection?(YOLO物体检测中如何获取边界框的坐标?)
Divide an image into 5x5 blocks in python and compute histogram for each block(在 python 中将图像划分为 5x5 块并计算每个块的直方图)
Extract cow number from image(从图像中提取奶牛编号)