MATLAB Answers

7x7 arithmetic mean filter

37 ビュー (過去 30 日間)
Atalay Asa
Atalay Asa 2020 年 6 月 7 日
コメント済み: Image Analyst 2020 年 7 月 17 日
How can I apply 7x7 arithmetic mean filter to following image?

  3 件のコメント

faysal firas
faysal firas 2020 年 7 月 17 日
% Chessboard
n = 8;
v = 1:n; % 1 =w; 0=black
c = zeros(n,n); for k=1:n; c(k,:) = mod(v+k-1,2); end; c
% mean filter
h = [1 1 1 1 1 1 1; 1 1 1 1 1 1 1; 1 1 1 1 1 1 1; 1 1 1 1 1 1 1; 1 1 1 1 1 1 1; 1 1 1 1 1 1 1; 1 1 1 1 1 1 1;]/49;
cf = imfilter(c,h) % built-in matlab function
% Now we design our function for filtering.
% zero-pad c to e: (n+2)*(n+2). Note: n1=n+3-1=n+2.
e = zeros(n+2,n+2);
e(2:n+1,2:n+1)=c; % original image
e % print e on screen
% Now perform correlation filtering (not convolution):
% Move the mask across image as follows: put the center of the mask at each
% pixel of the extended (zero-padded) image. Multiply the corresponding
% elements, and add them, then put the result on the center pixel.
g = zeros(size(e));
for i=2:n+1
for j=2:n+1
m = e(i-1:i+5, j-1:j+5); % 7 by 7square, center at (i,j)
g(i,j) = sum(sum(h .* m));
end
end
cg = g(2:n+1,2:n+1)
imshow(cg);
faysal firas
faysal firas 2020 年 7 月 17 日
% Chessboard
n = 8;
v = 1:n; % 1 =w; 0=black
c = zeros(n,n); for k=1:n; c(k,:) = mod(v+k-1,2); end; c
% mean filter
h = [1 1 1; 1 1 1; 1 1 1]/9;
cf = imfilter(c,h) % built-in matlab function
% Now we design our function for filtering.
% zero-pad c to e: (n+2)*(n+2). Note: n1=n+3-1=n+2.
e = zeros(n+2,n+2);
e(2:n+1,2:n+1)=c; %original image
e % print e on screen
% Now perform correlation filtering (not convolution):
% Move the mask across image as follows: put the center of the mask at each
% pixel of the extended (zero-padded) image. Multiply the corresponding
% elements, and add them, then put the result on the center pixel.
g = zeros(size(e));
for i=2:n+1
for j=2:n+1
m = e(i-1:i+1, j-1:j+1); % 3 by 3 square, center at (i,j)
g(i,j) = sum(sum(h .* m));
end
end
cg = g(2:n+1,2:n+1)
imshow(cg);
% error = cg-cf is zero
Image Analyst
Image Analyst 2020 年 7 月 17 日
faysal, are your two comments above "Answers"? If so, put them below in the official Answers area, not up here in comments. That way you can get credit (reputation points) for them. The comments section up here is where people are supposed to ask for clarification of a question, not offer solutions.

サインインしてコメントする。

採用された回答

Image Analyst
Image Analyst 2020 年 6 月 7 日
You can do this:
windowWidth = 7;
kernel = ones(windowWidth) / windowWidth^2;
outputImage = imfilter(grayImage, kernel);
If you use conv2() you'll need to make sure your input image is not of integer class, so cast it to double first:
outputImage = conv2(double(grayImage), kernel, 'same');
imfilter() does not require casting to double in advance. Also imfilter() does not flip the kernel like convolution does, though for a symmetric filter like this box filter, it doesn't matter if it's flipped or not.

  4 件のコメント

表示 1 件の古いコメント
Image Analyst
Image Analyst 2020 年 6 月 8 日
What's up with manual filtering instead of using built-in functions. Is it the latest hot homework question? This is the second one today that I've seen, plus I've seen tons of them in the past month. Just search for them, or see my attached demo.
Atalay Asa
Atalay Asa 2020 年 6 月 9 日
Actually, it is right my lecturer does not let us to use built-in methods. Thank you very much for your effort!
Image Analyst
Image Analyst 2020 年 6 月 9 日
You're welcome. Then since conv2() is a built-in function, it seems mine is the Answer that you should "Accept". Thanks in advance.

サインインしてコメントする。

その他の回答 (1 件)

Thiago Henrique Gomes Lobato
Thiago Henrique Gomes Lobato 2020 年 6 月 7 日
You can colvolve the image with a filter that will perform the average. In your case it will be something like this:
I = randn(50,50); % Substitute this by your image
N = 7;
F = ones(N)/(N.^2); % Each element is scaled so the sum of all will be 1
IFiltered = conv2(I,F,'same');

  1 件のコメント

Atalay Asa
Atalay Asa 2020 年 6 月 9 日
Thanks dude!

サインインしてコメントする。

Community Treasure Hunt

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

Start Hunting!

Translated by