Creating a function to calculate median
Show older comments
Hey there,
I have been tasked with creating a function that calculates the median number of an array without using the inbuilt median function.
% Function to calculate the median of an array
function out_val = my_median(in_val);
sorted = sort(in_val); % sort the array
median = numel(sorted); % find the number of elements in the array
if mod(median, 2) == 0
ans = 1
else
ans = 0
end
if ans = 1
% Code to run when median is even, meaning get two numbers and divide by two to find median
else
num = (median + 1) / 2
end
out_val = % either of the above
end
The question I'm asking is probably more maths related by how do I go about calculating the median number if the number of elements is even?
Also, is my code efficient enough or is there something that could be improved?
Thanks in advance
Accepted Answer
More Answers (1)
kokeb Dese
on 31 Jul 2018
0 votes
witching Bilateral Filter Noise Removal matlab code Here we discuss about Switching Bilateral Filter Noise Removal step by step.
1.Read the image pixel by pixel (i,j). 2. Each pixel we construct 4 sub windows.
For a (2N+1) (2N+1) window we divide the window into four (N+1)(N+1) subwindows .
case N = 2
3.Find the Sorted Quadrant Median Vector (SQMV) for all subwindows.
SQM1, SQM2, SQM3 and SQM4 are the medians
4. Then we find the regions from above all SQM.
Uniform Region Diagonal edge in dark side Diagonal edge in dark side Veritical edge Horizontal edge Diagonal line Gradual chage edge Texture
5. Then we find the reference median (SQMR) based on above regions. 6. Then apply Bilateral Filter. 7. The apply noise detection .
Code:
clc clear all close all close all hidden warning off
con = 1; % con1 for
img = imread('lena.jpg');
figure;imshow(img); title('Input Image');
img = imnoise(img,'salt & pepper',0.01); % salt and pepper Tk1 = 30; % for salt and paper noise.. Tk2 = 15; % for salt and paper noise..
% img = imnoise(img,'gaussian',0,0.01); % gaussian noise % Tk1 = 25; % for Gaussian noise.. % Tk2 = 5; % for Gaussian noise..
img = double(img); figure;imshow(uint8(img)); title('Noisy Image');
ext_filt = img; sigma_R = 40; [r c] = size(img); N = 2; pi_val = 25; h = waitbar(0,'Applying Switching bilateral filter..'); L = 1; for i = (N+1):r-(N+1) waitbar(L/length((N+1):r-(N+1))); L = L+1; for j = (N+1):c-(N+1) [m0,m1,m2,m3,m4] = meadian_calc(i,j,img,N); dav = dav_valc(m1,m2,m3,m4,img,i,j); SQMDB = m4-m1; SQMDC = m3-m2; SQMR = SQMR_calc(SQMDC,m1,m2,m3,m4,dav,pi_val); [s1 s2] = find_S1_S2(img,i,j,SQMR,Tk1,Tk2); if(dav~=0) sigma_S = 3; else sigma_S = 1; end f_x_y = f_of_x_y(i,j,N,img,sigma_S,sigma_R,s2,SQMR); ext_filt(i,j) = f_x_y; end end close(h);
figure; imshow(uint8(ext_filt));%output...
Referencce : Switching Bilateral Filter With a Texture/Noise Detector for Universal Noise Removal Chih-Hsing Lin, Jia-Shiuan Tsai, and Ching-Te Chiu Transactions on: Image Processing, IEEE Journals 2010 what is the code for meadian_calc(), dav_valc() and SQMR_calc() source codes. please help me!
Categories
Find more on Object Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!