Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
can imfilter be call only one time for the following calculation?

Subject: can imfilter be call only one time for the following calculation?

From: Sébastien Leclaire

Date: 19 Jul, 2010 19:29:04

Message: 1 of 3

Anyway to do this by calling only one time imfilter? I would like to do this with imfilter to use the fast simd instruction. Is it possible to define a multidimensional filter that would do that? Thanks you for your help.

F=rand(128*128,9);
F=reshape(F,128,128,9);
h=[0 0 0; 0 1 0; 0 0 0];
F(:,:,1)=imfilter(F(:,:,1),h,'circular','same');
h=[0 0 0; 1 0 0; 0 0 0];
F(:,:,2)=imfilter(F(:,:,2),h,'circular','same');
h=[1 0 0; 0 0 0; 0 0 0];
F(:,:,3)=imfilter(F(:,:,3),h,'circular','same');
h=[0 1 0; 0 0 0; 0 0 0];
F(:,:,4)=imfilter(F(:,:,4),h,'circular','same');
h=[0 0 1; 0 0 0; 0 0 0];
F(:,:,5)=imfilter(F(:,:,5),h,'circular','same');
h=[0 0 0; 0 0 1; 0 0 0];
F(:,:,6)=imfilter(F(:,:,6),h,'circular','same');
h=[0 0 0; 0 0 0; 0 0 1];
F(:,:,7)=imfilter(F(:,:,7),h,'circular','same');
h=[0 0 0; 0 0 0; 0 1 0];
F(:,:,8)=imfilter(F(:,:,8),h,'circular','same');
h=[0 0 0; 0 0 0; 1 0 0];
F(:,:,9)=imfilter(F(:,:,9),h,'circular','same');
F=reshape(F,128*128,9);

Subject: can imfilter be call only one time for the following calculation?

From: Sean

Date: 19 Jul, 2010 20:00:23

Message: 2 of 3

"Sébastien Leclaire" <sebastienleclaire@gmail.com> wrote in message <i22920$a36$1@fred.mathworks.com>...
> Anyway to do this by calling only one time imfilter? I would like to do this with imfilter to use the fast simd instruction. Is it possible to define a multidimensional filter that would do that? Thanks you for your help.
>
> F=rand(128*128,9);
> F=reshape(F,128,128,9);
> h=[0 0 0; 0 1 0; 0 0 0];
> F(:,:,1)=imfilter(F(:,:,1),h,'circular','same');
> h=[0 0 0; 1 0 0; 0 0 0];
> F(:,:,2)=imfilter(F(:,:,2),h,'circular','same');
> h=[1 0 0; 0 0 0; 0 0 0];
> F(:,:,3)=imfilter(F(:,:,3),h,'circular','same');
> h=[0 1 0; 0 0 0; 0 0 0];
> F(:,:,4)=imfilter(F(:,:,4),h,'circular','same');
> h=[0 0 1; 0 0 0; 0 0 0];
> F(:,:,5)=imfilter(F(:,:,5),h,'circular','same');
> h=[0 0 0; 0 0 1; 0 0 0];
> F(:,:,6)=imfilter(F(:,:,6),h,'circular','same');
> h=[0 0 0; 0 0 0; 0 0 1];
> F(:,:,7)=imfilter(F(:,:,7),h,'circular','same');
> h=[0 0 0; 0 0 0; 0 1 0];
> F(:,:,8)=imfilter(F(:,:,8),h,'circular','same');
> h=[0 0 0; 0 0 0; 1 0 0];
> F(:,:,9)=imfilter(F(:,:,9),h,'circular','same');
> F=reshape(F,128*128,9);

There might be a way but I would just preallocate both your filtered image and your H and it'll be pretty fast.

H = cat(3,[0 0 0; 0 1 0; 0 0 0],[0 0 0; 1 0 0; 0 0 0],...
    [1 0 0; 0 0 0; 0 0 0],[0 1 0; 0 0 0; 0 0 0],[0 0 1; 0 0 0; 0 0 0],...
    [0 0 0; 0 0 1; 0 0 0],[0 0 0; 0 0 0; 0 0 1],[0 0 0; 0 0 0; 0 1 0],...
    [0 0 0; 0 0 0; 1 0 0]);

F_filtered = zeros(size(F));
for ii = 1:9
    F_filtered(:,:,ii) = imfilter(F(:,:,ii),H(:,:,ii),'circular','same');
end

Subject: can imfilter be call only one time for the following calculation?

From: Sébastien Leclaire

Date: 19 Jul, 2010 20:27:06

Message: 3 of 3

Thanks for your anwser, even if the solution is faster or is cleaner, this is not what is was looking for. In your exemple imfilter is still call 9 times. I really want imfilter to be call only one time. This is the thing I want to know if it is possible.

"Sean " <sean.dewolski@nospamplease.umit.maine.edu> wrote in message <i22asn$8p5$1@fred.mathworks.com>...
> "Sébastien Leclaire" <sebastienleclaire@gmail.com> wrote in message <i22920$a36$1@fred.mathworks.com>...
> > Anyway to do this by calling only one time imfilter? I would like to do this with imfilter to use the fast simd instruction. Is it possible to define a multidimensional filter that would do that? Thanks you for your help.
> >
> > F=rand(128*128,9);
> > F=reshape(F,128,128,9);
> > h=[0 0 0; 0 1 0; 0 0 0];
> > F(:,:,1)=imfilter(F(:,:,1),h,'circular','same');
> > h=[0 0 0; 1 0 0; 0 0 0];
> > F(:,:,2)=imfilter(F(:,:,2),h,'circular','same');
> > h=[1 0 0; 0 0 0; 0 0 0];
> > F(:,:,3)=imfilter(F(:,:,3),h,'circular','same');
> > h=[0 1 0; 0 0 0; 0 0 0];
> > F(:,:,4)=imfilter(F(:,:,4),h,'circular','same');
> > h=[0 0 1; 0 0 0; 0 0 0];
> > F(:,:,5)=imfilter(F(:,:,5),h,'circular','same');
> > h=[0 0 0; 0 0 1; 0 0 0];
> > F(:,:,6)=imfilter(F(:,:,6),h,'circular','same');
> > h=[0 0 0; 0 0 0; 0 0 1];
> > F(:,:,7)=imfilter(F(:,:,7),h,'circular','same');
> > h=[0 0 0; 0 0 0; 0 1 0];
> > F(:,:,8)=imfilter(F(:,:,8),h,'circular','same');
> > h=[0 0 0; 0 0 0; 1 0 0];
> > F(:,:,9)=imfilter(F(:,:,9),h,'circular','same');
> > F=reshape(F,128*128,9);
>
> There might be a way but I would just preallocate both your filtered image and your H and it'll be pretty fast.
>
> H = cat(3,[0 0 0; 0 1 0; 0 0 0],[0 0 0; 1 0 0; 0 0 0],...
> [1 0 0; 0 0 0; 0 0 0],[0 1 0; 0 0 0; 0 0 0],[0 0 1; 0 0 0; 0 0 0],...
> [0 0 0; 0 0 1; 0 0 0],[0 0 0; 0 0 0; 0 0 1],[0 0 0; 0 0 0; 0 1 0],...
> [0 0 0; 0 0 0; 1 0 0]);
>
> F_filtered = zeros(size(F));
> for ii = 1:9
> F_filtered(:,:,ii) = imfilter(F(:,:,ii),H(:,:,ii),'circular','same');
> end

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us