(1) | Assume the input matrix “*A*” which has *M* rows and *N* columns. |

(2) | Construct a matrix with *M* + 2 rows and *N* + 2 columns by appending zeros to sides of the input matrix. |

(3) | Take a mask of size 3 × 3. |

(4) | Place the mask on the first element, i.e., element on the first row and first column of matrix “*A*”. |

(5) | Select all the elements listed by the mask and find the average |

(6) | Take the mean value from the sorted array and replace the element *A*(1, 1) by the median value. |

(7) | Slide the mask to the next element. |

(8) | Repeat the steps from 4 to 7 until all the elements of matrix “*A*” are replaced by their corresponding median value. |