81 views (last 30 days)

Yet there are 2 issues:

- It is only available to those who purchased Image PRocessing Toolbox.
- It creates the matrix for full convolution shape only.

I implemented the matrix form for imfiter() in Generate the Matrix Form of 2D Convolution Kernel. It was written in simple form (No vectorization tricks) for clarity and simplicity for thos who want to learn.

What I'm after is doing somthing similar for Convolution Matrices for the different shapes: full, same, valid.

Namely a function with the following form:

function [ mK ] = CreateImageConvMtx( mH, numRows, numCols, convShape )

%UNTITLED6 Summary of this function goes here

% Detailed explanation goes here

CONVOLUTION_SHAPE_FULL = 1;

CONVOLUTION_SHAPE_SAME = 2;

CONVOLUTION_SHAPE_VALID = 3;

switch(convShape)

case(CONVOLUTION_SHAPE_FULL)

% Code for the 'full' case

case(CONVOLUTION_SHAPE_SAME)

% Code for the 'same' case

case(CONVOLUTION_SHAPE_VALID)

% Code for the 'valid' case

end

end

I would be happy of someone could assist with that.

Again, prefer clarity over performance.

Thank You.

Matt J
on 15 Jan 2019

Edited: Matt J
on 15 Jan 2019

Readability and clarity over performance.

OK, can't get much simpler and more readable then the following:

function [ mK ] = CreateImageConvMtx( mH, nRows, nCols, convShape )

%convShape is 'full', 'same', or 'valid'

impulse=zeros(nRows,nCols);

for i=numel(impulse):-1:1

impulse(i)=1; %Create impulse image corresponding to i-th output matrix column

tmp=sparse( conv2(impulse,mH,convShape) ); %impulse response

Column{i}=tmp(:);

impulse(i)=0;

end

mK=cell2mat(Column);

end

Matt J
on 16 Jan 2019

Yes, there are ways to generate the (i,j,vals) data efficiently in a stand-alone piece of code. However, since we're now shifting priority to efficiency, the code would not look very C-like.

Could I ask who the audience of this code is going to be? Is it for a programming course that you are teaching? If the idea is to show how this would look in C, I'm having trouble seeing why you are pursuing this in Matlab at all.

Sign in to comment.

Matt J
on 15 Jan 2019

Edited: Matt J
on 15 Jan 2019

You can use my func2mat (Download) utility. It will find the matrix form of any linear function and doesn't require any toolboxes. However, there are much better options if your convolution kernels are separable.

function [ mK ] = CreateImageConvMtx( mH, nRows, nCols, convShape )

%convShape is 'full', 'same', or 'valid'

Xtypical=zeros([nRows,nCols]);

fun=@(x) conv2(x,mH,convShape);

mK=func2mat(fun,Xtypical);

end

Matt J
on 15 Jan 2019

Edited: Matt J
on 15 Jan 2019

function [ mK ] = CreateImageConvMtx( mH, nRows, nCols, convShape )

%convShape is 'full', 'same', or 'valid'

E=ndSparse(speye(nRows*nCols), [nRows,nCols,nRows,nCols]);

mK=convn(E,mH, convShape);

mK=sparse2d( reshape(mK, nRows*nCols, [] ) ) ;

end

Royi Avital
on 19 Jan 2019

Edited: Royi Avital
on 22 Jan 2019

Matt J
on 22 Jan 2019

@Matt, any thoughts on the code I posted at -

Overall, I think it's a fine blend of simplicity and efficiency (so +1!), but as I still don't know who this code is for, it's hard for me to say whether it's the best you can do.

I wonder if there is more efficient way to create the matrices while keeping the main idea of creating the Doubly Block Toeplitz Matrix.

I would consider this:

function [ mK ] = CreateConvMtxSparse( vK, numElements, convShape )

%conShape is 'full','same', or'valid'

mK = sparse( conv2(eye(numElements),vk,convShape) );

Matt J
on 22 Jan 2019

that in case numElements is a large number this becomes inefficient both computationaly and memory wise.

Less efficient, yes, but clearer (are you still prioritizing clarity over performance?), and still very fast even for absurdly large image sizes.

N=5000; %image length

K=7; %kernel length

vK=rand(K,1);

E=eye(N);

tic

mK1 = CreateConvMtxSparse( vK, N, 3 );

toc; %Elapsed time is 0.002846 seconds.

tic;

mK2=sparse(conv2(E,vK,'valid'));

toc %Elapsed time is 0.275314 seconds.

Sign in to comment.

Sign in to answer this question.

Opportunities for recent engineering grads.

Apply Today
## 3 Comments

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/439928-creating-convolution-matrix-of-2d-kernel-for-different-shapes-of-convolution#comment_660839

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/439928-creating-convolution-matrix-of-2d-kernel-for-different-shapes-of-convolution#comment_660839

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/439928-creating-convolution-matrix-of-2d-kernel-for-different-shapes-of-convolution#comment_660855

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/439928-creating-convolution-matrix-of-2d-kernel-for-different-shapes-of-convolution#comment_660855

## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/439928-creating-convolution-matrix-of-2d-kernel-for-different-shapes-of-convolution#comment_660928

⋮## Direct link to this comment

https://www.mathworks.com/matlabcentral/answers/439928-creating-convolution-matrix-of-2d-kernel-for-different-shapes-of-convolution#comment_660928

Sign in to comment.