3 x 3 mean and meadian replacement

Never used matlab so it is all new to me. I have to compute the mean and medain of the pixels by 3x3 neighborhoods around all the pixels. I did mean part, not sure if correct. And for the median I have no clue.
I = normrnd(128,8,[256,256]);
imshow(I,[0,255]); % Pick indices to repalce with black and white
Iv = reshape(l,[],1);
histogram(Iv);
---------------
[m,n] = size(I) ;
X = randperm(m*n,round(m*n*10/100)) ;
X1 = randsample(X,length(X)/2) ;
X2 = setdiff(X,X1);
I(X1) = 0 ; % black
I(X2) = 255 ; % white
imshow(I,[0,255])
Xv = reshape(I,[],1);
histogram(Xv); (credit to @KSSV)
-----------------
output1 = 1/3*ones(3,1);
H = output1*output1'; This is my mean attempt, not too sure if correct
imfilt = filter2(H,I);
imshow(imfilt,[0,255]);
histogram(imfilt);
------------------------------
p = medfilt2(outout1); I have tried this for the median, but i keep getting a dimension 2 error
imshow(p,[0,255]);

 Accepted Answer

Matt J
Matt J on 8 Aug 2021
medfilt2 is the correct thing to use. Perhaps the problem is the argument "outout1" which seems to be misspelled.

3 Comments

Missed that but no the message I get is "Undefined function 'medfilt2' for input arguments of type 'double'."
That likely means you do not have the Image Processing Toolbox installed.
wow spent two hours just for something so simple. thank you.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!