高效实现`im2col`和`col2im` [英] Efficient Implementation of `im2col` and `col2im`

查看:311
本文介绍了高效实现`im2col`和`col2im`的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MATLAB的 im2col col2im <在处理图像时,/ a>是MATLAB中矢量化的非常重要的函数。

但它们需要MATLAB的图像处理工具箱。

MATLAB's im2col and col2im are very important function for vectorization in MATLAB when dealing with images.
Yet they require MATLAB's Image Processing Toolbox.

我的问题是,是否有一种有效的(Vectorzied)方法来实现使用MATLAB的函数(没有工具箱)?

我需要滑动 distinct 模式。

My question is, is there an efficient (Vectorzied) way to implement the using MATLAB's functions (With no toolbox)?
I need both the sliding and distinct mode.

我不需要任何填充。

谢谢。

推荐答案

我只能希望Mathworks的家伙不要起诉你或我或者 Stackoverflow 就此而言,尝试创建其IP工具箱功能的矢量化实现,因为他们已经在该工具箱上定价。但无论如何,忘记这些问题,这里是实现。

I can only hope that Mathworks guys don't sue you or me or Stackoverflow for that matter, trying to create vectorized implementations of their IP toolbox functions, as they have put price on that toolbox. But in any case, forgetting those issues, here are the implementations.

im2col 的替换'滑动'选项

Replacement for im2col with 'sliding' option

在我坐下来写<之前,我无法对此进行矢量化一个href =https://stackoverflow.com/a/25718146/3293881>解决Stackoverflow上的另一个问题。所以,我强烈建议你也考虑一下。

I wasn't able to vectorize this until I sat down to write solution to another problem on Stackoverflow. So, I would strongly encourage to look into it too.

function out = im2col_sliding(A,blocksize)

nrows = blocksize(1);
ncols = blocksize(2);

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

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

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

%// Get linear indices based on row and col indices and get desired output
out = A(reshape(bsxfun(@plus,lin_row,[0:ncols-1]*m),nrows*ncols,[]));

return;

im2col 的替换'distinct'选项

Replacement for im2col with 'distinct' option

function out = im2col_distinct(A,blocksize)

nrows = blocksize(1);
ncols = blocksize(2);
nele = nrows*ncols;

row_ext = mod(size(A,1),nrows);
col_ext = mod(size(A,2),ncols);

padrowlen = (row_ext~=0)*(nrows - row_ext);
padcollen = (col_ext~=0)*(ncols - col_ext);

A1 = zeros(size(A,1)+padrowlen,size(A,2)+padcollen);
A1(1:size(A,1),1:size(A,2)) = A;

t1 = reshape(A1,nrows,size(A1,1)/nrows,[]);
t2 = reshape(permute(t1,[1 3 2]),size(t1,1)*size(t1,3),[]);
t3 =  permute(reshape(t2,nele,size(t2,1)/nele,[]),[1 3 2]);
out = reshape(t3,nele,[]);

return;






一些快速测试显示这两种实现特别 sliding 一个用于小到大小的输入数据, distinct 用于所有数据集的性能比内置的MATLAB函数实现要好得多在运行时性能方面。


Some quick tests show that both these implementations particularly sliding one for small to decent sized input data and distinct for all datasizes perform much better than the in-built MATLAB function implementations in terms of runtime performance.

如何使用

With in-built MATLAB function - 
B = im2col(A,[nrows ncols],'sliding')

With our custom function - 
B = im2col_sliding(A,[nrows ncols])

%// ------------------------------------

With in-built MATLAB function - 
B = im2col(A,[nrows ncols],'distinct')

With our custom function - 
B = im2col_distinct(A,[nrows ncols])

这篇关于高效实现`im2col`和`col2im`的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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