Source: https://www.ece.ucsb.edu/Faculty/Rabiner/ece259/image_processing_labs/lab3_solution.pdf