I have implemented the algorithm in python. When I run the code, it returns an image with strange patterns ( black spots ). I've reviewed it multiple times, but I cant find what the problem might be. I have also tried changing the direction in which the algorithm runs, but its no use.
import cv2 as cv
from matplotlib import pyplot as plt
import numpy as np
def Dither(img):
output = np.copy(img)
for y in range(1,img.shape[0]-1):
for x in range(1,img.shape[1]-1):
pix = output[y][x]
newpix = round(pix/255) * 255
error = pix - newpix
output[y][x] = newpix
output[y ][x + 1] += error * 7/16
output[y + 1][x - 1] += error * 3/16
output[y + 1][x ] += error * 5/16
output[y + 1][x + 1] += error * 1/16
return(output)
img = np.array(cv.cvtColor(cv.imread("kitten.jpg"), cv.COLOR_BGR2GRAY))
plt.imshow(Dither(img), cmap="gray")
plt.show()
The reason why the x and y variables are assigned in an unconventional order is that I have googled an implementation which had its variables flipped as well.
My result: my result
Expected result: expected result