-
Notifications
You must be signed in to change notification settings - Fork 1
/
randomrects.py
143 lines (124 loc) · 4.91 KB
/
randomrects.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
136
137
138
139
140
141
142
143
import cv2
import numpy as np
import math
from numpy import random
import sys
import time
import util
def randomrects(img):
global wr, hr
wr = random.randint(minl, maxl) # rect width
hr = random.randint(minl, maxl) # rect height
x = random.randint(0-minl, w-minl)
y = random.randint(0-minl, h-minl)
x2 = x + wr
y2 = y + hr
if x2 > w:
x2 = w
if y2 > h:
y2 = h
#print('x = %d, y = %d, x2 = %d, y2 = %d' % (x, y, x2, y2))
img = cv2.rectangle(img, (x, y), (x2, y2), (1, 1, 1), thickness=-1)
return img
def randomcircle(img):
global x, y, cr
cr = random.randint(minl, maxl) # circle radius
x = random.randint(0, w)
y = random.randint(0, h)
#print('centre = (%d, %d), r = %d' % (x, y, cr))
img = cv2.circle(img, (x, y), cr, (1, 1, 1), thickness=-1, lineType=cv2.LINE_AA)
return img
def avgcolorslow(src, mask):
_, contours, _ = cv2.findContours(mask, 1, 2)
cnt = contours[0]
lb, ub, wr, hr = cv2.boundingRect(cnt)
rb, bb = lb + wr, ub + hr
start = time.time()
colorlist = None
for i in range(ub, bb):
for j in range(lb, rb):
if mask[i, j] != 0:
if colorlist is None:
colorlist = [src[i, j]]
else:
colorlist = np.append(colorlist, [src[i, j]], axis=0)
#print(colorlist.shape)
avg = np.mean(colorlist, axis=0)
avgcolor = avg.astype(int)
print('color', avgcolor)
end = time.time()
print('time', end - start)
avgcolor = (int(avgcolor[0]), int(avgcolor[1]), int(avgcolor[2]))
return avgcolor
def main(mode='rect', show=True):
global src, mask, canvas, minl, maxl, src400, src200
w2, h2 = 400, int(h * 400 / w)
src400 = cv2.resize(src, (w2, h2))
w2, h2 = 200, int(h * 200 / w)
src200 = cv2.resize(src, (w2, h2))
if mode == 'rect':
list = rectlist
else:
list = circlelist
for k in range(len(list)):
minl = list[k][0]
maxl = list[k][1]
for i in range(list[k][2]):
mask[:] = 0
if mode == 'rect': # generate rectangular
mask = randomrects(mask)
#color = avgcolor(src, mask)
_, contours, hierarchy = cv2.findContours(cv2.resize(mask[:, :, 0], (mask.shape[1], mask.shape[0])), 1, 2)
cnt = contours[0]
#canvas = cv2.fillPoly(canvas, [cnt], color)
if wr < 100 or hr < 100:
color = util.get_avgcolor_crop(src, mask)
canvas = cv2.fillPoly(canvas, [cnt], color)
elif wr >= 100 and wr <= 200 or hr >= 100 and hr < 200:
color = util.get_avgcolor_downsize(src, mask, 400)
canvas = cv2.fillPoly(canvas, [cnt], color)
else:
color = util.get_avgcolor_downsize(src, mask, 200)
canvas = cv2.fillPoly(canvas, [cnt], color)
if show is True:
cv2.imshow('canvas', canvas)
k = cv2.waitKey(1) & 0xFF
if k == ord('q'):
break
else: # generate circle
mask = randomcircle(mask)
if cr < 60:
color = util.get_avgcolor_crop(src, mask)
canvas = cv2.circle(canvas, (x, y), cr, color, thickness=-1, lineType=cv2.LINE_AA)
elif cr >= 60 and cr <= 120:
color = util.get_avgcolor_downsize(src, mask, 400)
canvas = cv2.circle(canvas, (x, y), cr, color, thickness=-1, lineType=cv2.LINE_AA)
else:
color = util.get_avgcolor_downsize(src, mask, 200)
canvas = cv2.circle(canvas, (x, y), cr, color, thickness=-1, lineType=cv2.LINE_AA)
if show is True:
cv2.imshow('canvas', canvas)
cv2.waitKey(1)
return canvas
rectlist = [(200, 250, 25), (100, 150, 35), (60, 80, 60), (40, 60, 80), (10, 30, 80), (5, 20, 80)] # for width 400
circlelist = [(80, 120, 25), (50, 80, 35), (30, 40, 60), (20, 30, 80), (5, 15, 110), (3, 10, 110)] # width 400
# min length/radius, max length/radius, loop times
rectlist = [(200, 400, 50), (100, 200, 300), (80, 160, 600), (20, 100, 600), (10, 50, 600)] # width 800
circlelist = [(100, 120, 50), (40, 60, 600), (10, 30, 600), (6, 20, 600)] # width 800
src = cv2.imread('image/21.jpg')
h, w, _ = src.shape
newsize = 800
w, h = newsize, int(h*newsize/w)
src = cv2.resize(src, (w, h))
mask = np.zeros((h, w, 3), np.uint8)
canvas = np.zeros((h, w, 3), np.uint8)
canvas[:] = 255
canvas = main(mode='circle', show=False)
cv2.imshow('canvas', canvas)
k = cv2.waitKey(0) & 0xFF
if k == ord('s'):
cv2.imwrite('result/randomcircle.png', canvas)
#cv2.imwrite('result/randomcircle.png', canvas)
if k == ord('q'):
#cv2.imwrite('result/randomcircle.png', canvas)
cv2.destroyAllWindows()