-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathDither.py
More file actions
88 lines (75 loc) · 3.83 KB
/
Dither.py
File metadata and controls
88 lines (75 loc) · 3.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import cv2
import numpy as np
class ditherModule(object):
def dither(self, img, method='floyd-steinberg', resize = False):
if(resize):
img = cv2.resize(img, (int(0.5*(np.shape(img)[1])), int(0.5*(np.shape(img)[0]))))
if(method == 'simple2D'):
img = cv2.copyMakeBorder(img, 1, 1, 1, 1, cv2.BORDER_REPLICATE)
rows, cols = np.shape(img)
out = cv2.normalize(img.astype('float'), None, 0.0, 1.0, cv2.NORM_MINMAX)
for i in range(1, rows-1):
for j in range(1, cols-1):
# threshold step
if(out[i][j] > 0.5):
err = out[i][j] - 1
out[i][j] = 1
else:
err = out[i][j]
out[i][j] = 0
# error diffusion step
out[i][j + 1] = out[i][j + 1] + (0.5 * err)
out[i + 1][j] = out[i + 1][j] + (0.5 * err)
return(out[1:rows-1, 1:cols-1])
elif(method == 'floyd-steinberg'):
img = cv2.copyMakeBorder(img, 1, 1, 1, 1, cv2.BORDER_REPLICATE)
rows, cols = np.shape(img)
out = cv2.normalize(img.astype('float'), None, 0.0, 1.0, cv2.NORM_MINMAX)
for i in range(1, rows - 1):
for j in range(1, cols - 1):
# threshold step
if (out[i][j] > 0.5):
err = out[i][j] - 1
out[i][j] = 1
else:
err = out[i][j]
out[i][j] = 0
# error diffusion step
out[i][j + 1] = out[i][j + 1] + ((7/16) * err)
out[i + 1][j - 1] = out[i + 1][j - 1] + ((3/16) * err)
out[i + 1][j] = out[i + 1][j] + ((5/16) * err)
out[i + 1][j + 1] = out[i + 1][j + 1] + ((1/16) * err)
return (out[1:rows - 1, 1:cols - 1])
elif (method == 'jarvis-judice-ninke'):
img = cv2.copyMakeBorder(img, 2, 2, 2, 2, cv2.BORDER_REPLICATE)
rows, cols = np.shape(img)
out = cv2.normalize(img.astype('float'), None, 0.0, 1.0, cv2.NORM_MINMAX)
for i in range(2, rows - 2):
for j in range(2, cols - 2):
# threshold step
if (out[i][j] > 0.5):
err = out[i][j] - 1
out[i][j] = 1
else:
err = out[i][j]
out[i][j] = 0
# error diffusion step
out[i][j + 1] = out[i][j + 1] + ((7 / 48) * err)
out[i][j + 2] = out[i][j + 2] + ((5 / 48) * err)
out[i + 1][j - 2] = out[i + 1][j - 2] + ((3 / 48) * err)
out[i + 1][j - 1] = out[i + 1][j - 1] + ((5 / 48) * err)
out[i + 1][j] = out[i + 1][j] + ((7 / 48) * err)
out[i + 1][j + 1] = out[i + 1][j + 1] + ((5 / 48) * err)
out[i + 1][j + 2] = out[i + 1][j + 2] + ((3 / 48) * err)
out[i + 2][j - 2] = out[i + 2][j - 2] + ((1 / 48) * err)
out[i + 2][j - 1] = out[i + 2][j - 1] + ((3 / 48) * err)
out[i + 2][j] = out[i + 2][j] + ((5 / 48) * err)
out[i + 2][j + 1] = out[i + 2][j + 1] + ((3 / 48) * err)
out[i + 2][j + 2] = out[i + 2][j + 2] + ((1 / 48) * err)
return (out[2:rows - 2, 2:cols - 2])
else:
raise TypeError('specified method does not exist. available methods = "simple2D", "floyd-steinberg(default)", "jarvis-judice-ninke"')
def dither(img, method='floyd-steinberg', resize = False):
dither_object = ditherModule()
out = dither_object.dither(img, method, resize)
return(out)