average filter in Matlab

조회 수: 3 (최근 30일)
Franzi
Franzi 2020년 6월 8일
댓글: Rena Berman 2020년 10월 12일
Hello! I have an error for my implementation of the average filter on a variable window, can someone help me? That's my code:
function newpic = MeanFilt (pic, M)
[rows, cols] = size(pic);
[a,b]=size(M);
for i = ceil(a/2):rows-floor(a/2)
for j = ceil(b/2):cols-floor(b/2)
for ii = i-ceil(a/2):i+ceil(a/2)
for jj = j-ceil(b/2):j+ceil(b/2)
for k=1:a
for l=1:b
sum = sum + pic(ii,jj)*M(k,l);
endfor
endfor
endfor
endfor
newpic(i,j) = ceil(sum/(a*b));
endfor
endfor
endfunction
  댓글 수: 2
Rik
Rik 2020년 6월 19일
Question recovered from Google cache (as posted by Franzi):
Hello! I have an error for my implementation of the average filter on a variable window, can someone help me? That's my code:
function newpic = MeanFilt (pic, M)
[rows, cols] = size(pic);
[a,b]=size(M);
for i = ceil(a/2):rows-floor(a/2)
for j = ceil(b/2):cols-floor(b/2)
for ii = i-ceil(a/2):i+ceil(a/2)
for jj = j-ceil(b/2):j+ceil(b/2)
for k=1:a
for l=1:b
sum = sum + pic(ii,jj)*M(k,l);
endfor
endfor
endfor
endfor
newpic(i,j) = ceil(sum/(a*b));
endfor
endfor
endfunction
Rena Berman
Rena Berman 2020년 10월 12일
(Answers Dev) Restored edit

댓글을 달려면 로그인하십시오.

답변 (2개)

Bjorn Gustavsson
Bjorn Gustavsson 2020년 6월 8일
Why not use imfilter (if you have the image-processing toolbox) or conv2 - both should do this task in one line.
To your problem. You haven't told us what problem you run into, always do that when asking. The problem I manage to spot is that you don't initialize the variable sum before trying to access its value. Further, you don't re-set it to zero before starting to work on the next pixel. Then you increment the variable newpic, this is time-consuming, pre-allocate that variable first - you know it should have the same size as pic. Then I've never seen endfor in matlab.
HTH
  댓글 수: 1
Bjorn Gustavsson
Bjorn Gustavsson 2020년 6월 8일
When you get to know about a matlab-function (I'll use conv2 as an example here) you've never heard of before do this at the command-line:
help conv2
Then you get to read the matlab help-documentation for the function, and can browse further to the "online documentation". These are typically very good since they are written by Mathworks by people working with help and documentation of the functions with the explicity purpose of being instructive.

댓글을 달려면 로그인하십시오.


Image Analyst
Image Analyst 2020년 6월 8일
See my manual convolution program attached. It does this.
Also, don't use sum for the name of your varaible since sum is the name of a very important built-in function that you don't want to blow away.

카테고리

Help CenterFile Exchange에서 Image Processing and Computer Vision에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by