MATLAB Answers

Finding the average of the neighbors of each pixel in an image

11 views (last 30 days)
Srinand Andey
Srinand Andey on 22 Sep 2021
Commented: Image Analyst on 23 Sep 2021
I want to replace each pixel by the average of 3 x 3 neighbors using 4-connected neighbors (I4) and 8- connected neighbors (I8).

Answers (1)

Matt J
Matt J on 22 Sep 2021
mean4=conv2(image,[0 1 0; 1 1 1; 0 1 0]/5,'same');
mean8=conv2(image,ones(3)/9,'same');
  12 Comments
Image Analyst
Image Analyst on 23 Sep 2021
It's a floating point image so you need to use [] because the range is beyond the 0-1 that imshow() expects for a floating point image.
mean4=conv2(yourImage,[0 1 0; 1 1 1; 0 1 0]/5,'same'); % Don't use image as a variable name because it's a built-in function name.
imshow(mean4, []);

Sign in to comment.

Categories

Community Treasure Hunt

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

Start Hunting!