Creating Convolution Matrix of 2D Kernel for Different Shapes of Convolution
조회 수: 18 (최근 30일)
이전 댓글 표시
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.
댓글 수: 3
Image Analyst
2019년 1월 15일
I guess I don't understand. If one of the disadvantages is that it requires the Image Processing Toolbox, then how/why are you using imfilter(), which also requires the same toolbox? So if you have the toolbox, why do you care about that?
I have never used convmtx2(). Why would I need to? I don't see any advantage to using that over conv2()m, especially if you say it doesn't have 'same', 'full', and 'valid' options.
If you're trying to come up with a kernel to filter your 2-D array with, why don't you just create it yourself, either with fspecial() or just manually hard-code in all the weights?
채택된 답변
Matt J
2019년 1월 15일
편집: Matt J
2019년 1월 15일
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
댓글 수: 12
추가 답변 (3개)
Matt J
2019년 1월 15일
편집: Matt J
2019년 1월 15일
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
Royi Avital
2019년 1월 19일
편집: Royi Avital
2019년 1월 22일
댓글 수: 3
Matt J
2019년 1월 22일
편집: Matt J
2019년 1월 22일
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.
참고 항목
제품
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!