-
Notifications
You must be signed in to change notification settings - Fork 1
/
crosshatching.py
137 lines (118 loc) · 3.88 KB
/
crosshatching.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
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
import cv2
import numpy as np
import math
from numpy import random as nr
import sys
def lines(code=None, step=12):
l = np.zeros((h, w, 3), np.uint8)
l[:] = 255
if code == 0: # - horizontal
for i in range(0, h, step):
l = cv2.line(l, (0, i), (w, i), black)
elif code == 1: # | horizontal
for i in range(0, w, step):
l = cv2.line(l, (i, 0), (i, h), black)
elif code == 2: # \ 45
l = lines(code=3, step=step)
l = cv2.flip(l, 0)
elif code == 3: # / 45
for i in range(0, 2*w, step):
l = cv2.line(l, (i, 0), (0, i), black)
elif code == 4: # / 22.5
cotheta = 2.4142
tantheta = 0.4142
for i in range(0, int(w+h*cotheta), step):
l = cv2.line(l, (i, 0), (0, int(i*tantheta)), black)
elif code == 5: # / 67.5
cotheta = 0.4142
tantheta = 2.4142
for i in range(0, int(w+h*cotheta), step):
l = cv2.line(l, (i, 0), (0, int(i*tantheta)), black)
else:
pass # empty
return l
def tsh(img, stage=None, Numberoftsh=None, equalizeHist=False):
type = cv2.THRESH_BINARY
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
if equalizeHist == False:
pass
else:
img_gray = cv2.equalizeHist(img_gray, img_gray)
_, th = cv2.threshold(img_gray, 255-int(((stage)/Numberoftsh)*255), 255, type)
th = cv2.cvtColor(th, cv2.COLOR_GRAY2BGR)
return th
def createmasks(img, Numberoftsh=None):
global masks
for i in range(Numberoftsh):
if seqline[i] == 4:
step = 16
elif seqline[i] == 5:
step = 10
else:
step = 8
if masks is not None:
masks = np.append(masks, np.expand_dims(lines(code=seqline[i], step=step), axis=0), axis=0)
else:
masks = lines(code=seqline[i], step=step)
masks = np.expand_dims(masks, axis=0)
#print(masks.shape)
return masks
def crosshatching(img, Numberoftsh=None, equalizeHist=False, color=False):
global frame, flag, w, h
h, w, _ = img.shape
frame = np.zeros((h, w, 3), np.uint8)
frame[:] = 255
if flag is False:
createmasks(img, Numberoftsh=Numberoftsh)
flag = True
for i in range(Numberoftsh):
th = tsh(img, stage=i, Numberoftsh=Numberoftsh, equalizeHist=equalizeHist)
dst = cv2.addWeighted(masks[i], 1, th, 1, 0)
dst = cv2.bitwise_and(dst, frame)
frame = dst
if color is False:
return frame
else:
frame = cv2.bitwise_or(frame, img)
return frame
def showimage(img, Numberoftsh = 7, equalizeHist=False):
global w, h
h, w, _ = img.shape
dst = crosshatching(img, Numberoftsh=Numberoftsh, equalizeHist=equalizeHist, color=True)
#dst = cv2.resize(dst, (int(w/2), int(h/2)))
cv2.imshow('dst', dst)
cv2.waitKey(0)
cv2.destroyAllWindows()
def playvideo(video=None, Numberoftsh=None, color=False):
global w, h
if video is None:
cap = cv2.VideoCapture(0)
else:
cap = video
while True:
_, frame = cap.read()
if video is None:
frame = cv2.flip(frame, 1)
frame = crosshatching(frame, Numberoftsh=Numberoftsh, equalizeHist=False, color=color)
cv2.imshow('main', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
black = (0, 0, 0)
white = (255, 255, 255)
#red = (0, 0, 255)
#green = (0, 255, 0)
#blue = (255, 0, 0)
seqline = (-1, 0, 4, 3, 5, 2, 1)
masks = None
flag = False # existence of line masks
if __name__ == "__main__":
if len(sys.argv) > 1:
pass
else:
#img = cv2.imread('eagle.jpg')
#h, w, _ = img.shape
#img = cv2.resize(img, (int(w/8), int(h/8)))
#showimage(img)
#video = cv2.VideoCapture(0)
#video = cv2.VideoCapture('video/Wildlife.wmv')
playvideo(video=None, Numberoftsh=7, color=True)