-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenStroke.py
More file actions
156 lines (140 loc) · 4.87 KB
/
genStroke.py
File metadata and controls
156 lines (140 loc) · 4.87 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
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import cv2
import numpy as np
from matplotlib import pyplot as plt
import math
import sys
import os
def rotate_img(img, angle):
row, col = img.shape
M = cv2.getRotationMatrix2D((row / 2 + 1, col / 2 + 1), angle, 1)
res = cv2.warpAffine(img, M, (row, col))
return res
def rotateImg(img, angle):
row, col = img.shape
M = cv2.getRotationMatrix2D((row / 2 + 1, col / 2 + 1), angle, 1)
res = cv2.warpAffine(img, M, (row, col))
return res
def genDirectionLines(halfkernel, verbose = False):
len = halfkernel * 2 + 1
kernel = np.zeros((8, len, len))
kernel [0, halfkernel, :] = 1.0
kernel [2, :, :] = np.rot90(kernel[0,:,:])
kernel [4, :, :] = np.rot90(kernel[2,:,:])
kernel [6, :, :] = np.rot90(kernel[4,:,:])
for i in range(len):
kernel[1,i,i] = 1.0
kernel [3, :, :] = np.rot90(kernel[1,:,:])
kernel [5, :, :] = np.rot90(kernel[3,:,:])
kernel [7, :, :] = np.rot90(kernel[5,:,:])
if verbose == True:
for i in range(8):
title = 'kernel line %d'%i
cv2.imshow(title, kernel[i,:,:])
cv2.waitKey(1)
return kernel;
# compute and get the stroke of the raw img
def genStroke(img, dirNum, verbose = False):
height , width = img.shape[0], img.shape[1]
img = np.float32(img) / 255.0
print "- Images, size %dx%d"%(width, height)
print "- PreProcessing Images, denoising ..."
img = cv2.medianBlur(img, 3)
if verbose == True:
cv2.imshow('blurred image', img)
cv2.waitKey(1)
print "- Generating Gradient Images ..."
imX = np.append(np.absolute(img[:, 0 : width - 1] - img[:, 1 : width]), np.zeros((height, 1)), axis = 1)
imY = np.append(np.absolute(img[0 : height - 1, :] - img[1 : height, :]), np.zeros((1, width)), axis = 0)
#img_gredient = np.sqrt((imX ** 2 + imY ** 2))
img_gredient = imX + imY
if verbose == True:
cv2.imshow('gradient image', img_gredient)
cv2.waitKey(1)
#filter kernel size
tempsize = 0
if height > width:
tempsize = width
else:
tempsize = height
tempsize /= 32
halfKsize = tempsize / 2
if halfKsize < 1:
halfKsize = 3
if halfKsize > 9:
halfKsize = 9
len = halfKsize * 2 + 1
print "- Kernel Line Size=%s" %(len)
kernel = np.zeros((dirNum, len, len))
kernel [0,halfKsize,:] = 1.0
#kernel = genDirectionLines(halfKsize, False);
for i in range(1,dirNum):
kernel[i,:,:] = temp = rotateImg(kernel[0,:,:], i * 180 / dirNum)
if verbose == True:
title = 'line kernel %d'%i
cv2.imshow( title, temp)
cv2.waitKey(1)
#filter gradient map in different directions
print "- Filtering Gradient Images in different directions ..."
response = np.zeros((dirNum, height, width))
for i in range(dirNum):
ker = kernel[i,:,:];
response[i, :, :] = cv2.filter2D(img_gredient, -1, ker)
if verbose == True:
for i in range(dirNum):
title = 'response %d'%i
cv2.imshow( title, response[i,:,:])
cv2.waitKey(1)
#divide gradient map into 8 different sub-map
print "- Caculating Gradient classification ..."
Cs = np.zeros((dirNum, height, width))
for x in range(width):
for y in range(height):
i = np.argmax(response[:,y,x])
Cs[i, y, x] = img_gredient[y,x]
#generate line shape
print "- Generating shape Lines ..."
spn = np.zeros((dirNum, height, width))
for i in range(dirNum):
ker = kernel[i,:,:];
spn[i, :, :] = cv2.filter2D(Cs[i], -1, ker)
sp = np.sum(spn, axis = 0)
sp = (sp - np.min(sp)) / (np.max(sp) - np.min(sp))
S = 1 - sp
if verbose == True:
cv2.imshow('raw stroke', sp)
cv2.waitKey(1)
cv2.imshow('stroke', S)
cv2.waitKey(1)
return S
if __name__ == '__main__':
#if len(sys.argv) < 3:
img_path = '.\\pic\\flower.png'
img_stroke = 'flower-stroke.jpg'
print "- Unit test for stroke generation. image file=%s" %(img_path)
img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
stroke = genStroke(img,12, False)
cv2.imshow('stroke', stroke)
cv2.waitKey(0)
#result = np.uint8(stroke * 255)
#imwrite always write integer value[0,255]
#cv2.imwrite(img_stroke, result)
#print "- Write stroke into file. image file=%s" %(img_stroke)
exit()
# ignore following code, please.
img_rgb = cv2.imread(img_path)
r = img_rgb[:,:,0]
g = img_rgb[:,:,1]
b = img_rgb[:,:,2]
cv2.imshow('r', r)
cv2.imshow('g', g)
cv2.imshow('b', b)
cv2.waitKey(0)
img_rgb = np.float32(img_rgb) / 255.0
img_hls = cv2.cvtColor(img_rgb, cv2.COLOR_BGR2HLS)
l = img_hls[:,:,1]
cv2.imshow('l', l)
cv2.imshow('hls', img_hls)
img_bgr = cv2.cvtColor(img_rgb, cv2.COLOR_HLS2BGR)
cv2.imshow('bgr', img_bgr)
cv2.waitKey(0)
exit()