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

13 ビュー (過去 30 日間)
Srinand Andey
Srinand Andey 2021 年 9 月 22 日
コメント済み: Image Analyst 2022 年 7 月 18 日
I want to replace each pixel by the average of 3 x 3 neighbors using 4-connected neighbors (I4) and 8- connected neighbors (I8).

回答 (1 件)

Matt J
Matt J 2021 年 9 月 22 日
mean4=conv2(image,[0 1 0; 1 1 1; 0 1 0]/5,'same');
mean8=conv2(image,ones(3)/9,'same');
  16 件のコメント
Isabella
Isabella 2022 年 7 月 18 日
@Image Analyst Can I replace each pixel by the avg of its 3x3 neighbors, without I4 or I8?
Image Analyst
Image Analyst 2022 年 7 月 18 日
You have to specify which pixels to use in the average. That kernel basically specifies which of the 9 pixels in the 3x3 window to use in computing the average. You can specify whatever you want in your kernel. Like if you wanted to replace the center pixel by the average of the 4 on the corners, you'd do
kernel = [1,0,1; 0,0,0; 1,0,1] / 4; % Diagonals only.
blurredImage = conv2(double(grayImage), kernel, 'same');
% or, including the middle pixel:
kernel = [1,0,1; 0,1,0; 1,0,1] / 4; % Diagonals plus center pixel.
blurredImage = conv2(double(grayImage), kernel, 'same');

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

Community Treasure Hunt

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

Start Hunting!

Translated by