How to implement a function to apply the 3x3 average filter to a gray-scale image.
Show older comments
Here is my code
function AverageFilter(image)
I = imread(image);
[x,y] = size(I);
for i = 2:x-1
for j = 2:y-1
sum = 0;
for ii = i-1:i+1
for jj = j-1:j+1
sum = sum + I(ii,jj);
end
end
I2(i,j) = ceil(sum/9);
end
end
imshow(I2);
it keeps giving me the following error: Warning: Image is too big to fit on screen; displaying at 67% > In imuitools\private\initSize at 73 In imshow at 264 In AverageFilter at 18
and the resulting image is very dark..
I want to know what did i do wrong in my code and to fix this error?!!!
7 Comments
sateesh kumar
on 16 Apr 2020
function AverageFilter(image)
I = imread(image);
[x,y] = size(I);
for i = 2:x-1
for j = 2:y-1
sum = 0;
sum=int32(sum);
for ii = i-1:i+1
for jj = j-1:j+1
tmp=I(ii,jj);
tmp=int32(tmp);
sum = sum + tmp;
sum=int32(sum);
end
end
I2(i,j) = ceil(sum/9);
end
end
imshow(uint8(I2)
Logically, what you have posted is correct but technically not. In MATLAB each variable is defined as 8 bit of size by default, So, sum is rounded to 255 while adding pixel values. That is the reason you got dark image at the output i.e., 255/9 =28 after roundoff. Now, to get exact "average" value sum is defined as 32 bit size. tmp is also defined as temporary variable. Then you will get the exact ouput by converting I2 into 8 bit.
justin gat
on 30 May 2021
Edited: justin gat
on 30 May 2021
sir it is giving the output gray image can u please send me code for the color image
Image Analyst
on 30 May 2021
@abhishek KP, try this:
rgbImage = imread('peppers.png');
nexttile
imshow(rgbImage);
drawnow;
windowWidth = 21;
kernel = ones(windowWidth) / windowWidth^2;
blurryImage = imfilter(rgbImage, kernel);
nexttile
imshow(blurryImage);

justin gat
on 30 May 2021
sir actually what i need is this can u help me with the code sir
sir can u please send me code for this
- i am considering 3x3 size window
- i am taking the average of the pixels conditons are
- if the pixel values in the window is greater than 255 then i am averaging the pixels expect 255 value pixels
- if the pixel values are less than 255 value pixels then i am taking 255 as my answer
example lets take in a 3x3 window i have 5 number of 255 pixel value and 4 number less than 255 values then 255 > 254 or any other pixel value so i am keeping 255 as same if 255< 254 or any other pixel values then i am averaging all the pixels except 255 pixel value
3. after this i have to replace the pixel value by resultent value
anyone can write code for this please as soon as possible
sir I need to ignore high intensity pixel 255 and have to average all other pixels in the 3x3 window If all the pixels are white that is high intensity pixels 255 then I have keep it as 255 - sir here is the condition 1. While sliding a 3x3 window on image 2.if there are white(255) pixels are more than other pixels then I have to keep 255 and replace center pixel value by 255 3.if there are below (255) pixels are more inside that 3x3 window... Then I have to take average of below(255)pixels then have to take average of only below(255) pixels and replace as the center pixel value I have to take 255(white pixels) as center during sliding a 3x3 window I need a code for it please do help with loop I need because I have to take all 255(white)pixels in a image and have to change the value of each and every white pixel
Sir I have found out the pixel values using coordinates (x, y) = find(I==255) I got the each and every coorodintes and pixels of 255(white) from this now I need to apply averaging as I explained above for every pixel Please do help me with the code
Image Analyst
on 30 May 2021
@abhishek KP, since you are not asking @amy elmossly to clarify anything on her 9 year old post, you should start your own question. In that new question I will show you how to do it with two convolutions (one to sum and one to count) and an assignment of the 255s.
justin gat
on 30 May 2021
ok sir now i will tag u and will write a question
thank you i need it as soon as possible
justin gat
on 30 May 2021
sir here the question please do help sir
Accepted Answer
More Answers (2)
sateesh kumar
on 16 Apr 2020
0 votes
Dont worry about Warning/Display Warning of too big size, you can simply write warning off at the starting of program.
Nitishselvakumar Nadar
on 22 Apr 2021
img = imread("Tulip.jpg");
[row ,col] = size(img);
for i = 2:1:row-1
for j = 2:1:col-1
x= img(i+1:i-1,j+1:j-1);
C=x(:)';
C=sort(C);
avg = sum(C)/9;
img(i,j) = avg;
end
end
imshow(img);
4 Comments
Nitishselvakumar Nadar
on 22 Apr 2021
Edited: Nitishselvakumar Nadar
on 22 Apr 2021
i have a problem to run this code.Output is showing black screen
mohammad sheikholmolok
on 27 Nov 2022
Moved: DGM
on 27 Nov 2022
clc
clear
close all
img = imread('E:\matlab project\image processing\Fig0121(b)(blown_ic_hr).tif');
[row ,col] = size(img);
for i = 2:1:row-1
for j = 2:1:col-1
x= img(i-1:i+1,j-1:j+1);
C=x(:);
%C=sort(C);
avg = sum(C)/9;
I2(i,j) =ceil(avg);
I2=uint8(I2);
end
end
figure(1);
subplot(1,2,1);
imshow(img);
subplot(1,2,2);
imshow(I2);
imtool(I2);
imtool(img);
mohammad sheikholmolok
on 27 Nov 2022
Moved: DGM
on 27 Nov 2022
I write for you the correct code
If you're going to offer an improved bit of code, explain why it's better.
This is still a lot of room for improvement.
% read an image
inpict = imread('cameraman.tif'); % use unambiguous variable names
% what stops this from breaking if the image is RGB?
[row,col,~] = size(inpict); % always discard the last output from size()
outpict = zeros(row,col,class(inpict)); % preallocate to appropriate class
for i = 2:1:row-1 % avoiding the edges is a simple strategy, but not very good
for j = 2:1:col-1 % a better way would be edge padding/replication
% get a sample
sampleblock = inpict(i-1:i+1,j-1:j+1);
% don't need a bunch of temporary variables or unused lines
% RHS is uint8-scale double
% LHS is uint8
% the assignment implicitly uses round() when casting
outpict(i,j) = mean(sampleblock(:));
end
end
% don't need a bunch of redundant images
montage({inpict outpict})

Communication is the purpose of a forum, and comments (code comments and external commentary) are part of that process.
Categories
Find more on Image Arithmetic in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!