首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将滑动块重新排列为三维数组的列(IM2COL在3D中)- MATLAB

将滑动块重新排列为三维数组的列(IM2COL在3D中)- MATLAB
EN

Stack Overflow用户
提问于 2016-04-21 06:52:29
回答 1查看 991关注 0票数 2

给定一个矩阵A(mxnxc) (c可以是任意的),我想要对步长d的滑动窗口格式中的补丁进行采样,并将所有的pxpxc补丁重新排列成向量。我可以在嵌套的for-循环中这样做,但这非常耗时。如何快速做到这一点?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-04-21 07:55:20

您可以再次用this solution to Efficient Implementation of im2col and col2im扩展bsxfun,用于三维数组案例来解决您的问题。

现在,对这个问题有两种可能的解释:

  • 提取大小为p x p的块,并将每个块作为向量,对整个第一个2D切片执行此操作,然后对3D中的所有切片重复此操作,从而生成3D输出。
  • 收集大小为p x p x c的块作为向量,并以滑动的方式在整个数组中执行这一操作,从而产生一个2D输出。

这两种解释分别作为im2col_3D_sliding_v1im2col_3D_sliding_v2实现,并随后列出。

im2col_3D_sliding_v1 :

代码语言:javascript
复制
function out = im2col_3D_sliding_v1(A,blocksize,stepsize)

%// Store blocksizes
nrows = blocksize(1);
ncols = blocksize(2);

%// Store stepsizes along rows and cols
d_row = stepsize(1);
d_col = stepsize(2);

%// Get sizes for later usages
[m,n,r] = size(A);

%// Start indices for each block
start_ind = reshape(bsxfun(@plus,[1:d_row:m-nrows+1]',[0:d_col:n-ncols]*m),[],1); %//'

%// Row indices
lin_row = permute(bsxfun(@plus,start_ind,[0:nrows-1])',[1 3 2]);  %//'

%// 2D linear indices
lidx_2D = reshape(bsxfun(@plus,lin_row,[0:ncols-1]*m),nrows*ncols,[]);

%// 3D linear indices
lidx_3D = bsxfun(@plus,lidx_2D,m*n*permute((0:r-1),[1 3 2]));

%// Get linear indices based on row and col indices and get desired output
out = A(lidx_3D);

return;

im2col_3D_sliding_v2 :

代码语言:javascript
复制
function out = im2col_3D_sliding_v2(A,blocksize,stepsize)

%// Store blocksizes
nrows = blocksize(1);
ncols = blocksize(2);

%// Store stepsizes along rows and cols
d_row = stepsize(1);
d_col = stepsize(2);

%// Get sizes for later usages
[m,n,r] = size(A);

%// Start indices for each block
start_ind = reshape(bsxfun(@plus,[1:d_row:m-nrows+1]',[0:d_col:n-ncols]*m),[],1); %//'

%// Row indices
lin_row = permute(bsxfun(@plus,start_ind,[0:nrows-1])',[1 3 2]);  %//'

%// 2D linear indices
lidx_2D = reshape(bsxfun(@plus,lin_row,[0:ncols-1]*m),nrows*ncols,[]);

%// 3D linear indices
lidx_3D = bsxfun(@plus,permute(lidx_2D,[1 3 2]),m*n*(0:r-1));

%// Final 2D linear indices
lidx_2D_final = reshape(lidx_3D,[],size(lidx_2D,2));

%// Get linear indices based on row and col indices and get desired output
out = A(lidx_2D_final);

return;

示例运行

(I)输入阵列:

代码语言:javascript
复制
>> A
A(:,:,1) =
    23   109    63     1    37   153
   110    31   201    57    69   230
    66   127    19     1    45   240
    76   181   101    49    36    57
A(:,:,2) =
   124    18   244     2   141    95
    96   112   110   174    56   228
   134    45   246   181   197   219
    68     7   195   165    59   103

(2)输入参数:

代码语言:javascript
复制
>> blocksize = [2,3]; %// blocksize along rows, cols
>> stepsize = [2,2];  %// stepsize along rows, cols

(3)有两个版本的产出:

代码语言:javascript
复制
>> im2col_3D_sliding_v1(A,blocksize,stepsize)
ans(:,:,1) =
    23    66    63    19
   110    76   201   101
   109   127     1     1
    31   181    57    49
    63    19    37    45
   201   101    69    36
ans(:,:,2) =
   124   134   244   246
    96    68   110   195
    18    45     2   181
   112     7   174   165
   244   246   141   197
   110   195    56    59

   >> im2col_3D_sliding_v2(A,blocksize,stepsize)
ans =
    23    66    63    19
   110    76   201   101
   109   127     1     1
    31   181    57    49
    63    19    37    45
   201   101    69    36
   124   134   244   246
    96    68   110   195
    18    45     2   181
   112     7   174   165
   244   246   141   197
   110   195    56    59
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36761794

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档