-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
44 lines (37 loc) · 1.3 KB
/
util.py
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
import numpy as np
import cv2
import numba
from numba import int8
SHARPEN = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]])
SMOOTH = np.ones((3,3),np.float32)/9
def zeros(shape):
return np.zeros(shape, dtype=int)
def RGBAWhite(shape):
return np.ones(shape, dtype=int)*255
#Floyd–Steinberg dithering algorithm
@numba.jit(nopython=True)
def Dither(num,derr, thresh = 175):
div = 5
for y in range(num.shape[0]):
for x in range(num.shape[1]):
newval = derr[y,x] + num[y,x]
if newval >= thresh:
errval = newval - 255
num[y,x] = 1.
else:
errval = newval
num[y,x] = 0.
if x + 1 < num.shape[1]:
derr[y, x + 1] += errval / div
if x + 2 < num.shape[1]:
derr[y, x + 2] += errval / div
if y + 1 < num.shape[0]:
derr[y + 1, x - 1] += errval / div
derr[y + 1, x] += errval / div
if y + 2< num.shape[0]:
derr[y + 2, x] += errval / div
if x + 1 < num.shape[1]:
derr[y + 1, x + 1] += errval / div
return (num[::-1,:] * 255)[::-1, ...].astype(np.uint8)
def Filter(frame,filter):
return cv2.filter2D(frame, -1, filter)