Spaces:
Runtime error
Runtime error
File size: 13,836 Bytes
e9d4572 |
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 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 |
import numpy as np
import cv2
from skimage.measure import block_reduce
from linefiller.trappedball_fill import *
from linefiller.thinning import *
from linefiller.third_party import *
def from_png_to_jpg(map):
if map.shape[2] ==3:
return map
color = map[:, :, 0:3].astype(np.float) / 255.0
alpha = map[:, :, 3:4].astype(np.float) / 255.0
reversed_color = 1 - color
final_color = (255.0 - reversed_color * alpha * 255.0).clip(0,255).astype(np.uint8)
return final_color
def mk_resize(x, k):
if x.shape[0] < x.shape[1]:
s0 = k
s1 = int(x.shape[1] * (k / x.shape[0]))
s1 = s1 - s1 % 128
_s0 = 32 * s0
_s1 = int(x.shape[1] * (_s0 / x.shape[0]))
_s1 = (_s1 + 64) - (_s1 + 64) % 128
else:
s1 = k
s0 = int(x.shape[0] * (k / x.shape[1]))
s0 = s0 - s0 % 128
_s1 = 32 * s1
_s0 = int(x.shape[0] * (_s1 / x.shape[1]))
_s0 = (_s0 + 64) - (_s0 + 64) % 128
new_min = min(_s1, _s0)
raw_min = min(x.shape[0], x.shape[1])
if new_min < raw_min:
interpolation = cv2.INTER_AREA
else:
interpolation = cv2.INTER_LANCZOS4
y = cv2.resize(x, (_s1, _s0), interpolation=interpolation)
return y
def k_resize(x, k):
if x.shape[0] < x.shape[1]:
s0 = k
s1 = int(x.shape[1] * (k / x.shape[0]))
s1 = s1 - s1 % 64
_s0 = 16 * s0
_s1 = int(x.shape[1] * (_s0 / x.shape[0]))
_s1 = (_s1 + 32) - (_s1 + 32) % 64
else:
s1 = k
s0 = int(x.shape[0] * (k / x.shape[1]))
s0 = s0 - s0 % 64
_s1 = 16 * s1
_s0 = int(x.shape[0] * (_s1 / x.shape[1]))
_s0 = (_s0 + 32) - (_s0 + 32) % 64
new_min = min(_s1, _s0)
raw_min = min(x.shape[0], x.shape[1])
if new_min < raw_min:
interpolation = cv2.INTER_AREA
else:
interpolation = cv2.INTER_LANCZOS4
y = cv2.resize(x, (_s1, _s0), interpolation=interpolation)
return y
def sk_resize(x, k):
if x.shape[0] < x.shape[1]:
s0 = k
s1 = int(x.shape[1] * (k / x.shape[0]))
s1 = s1 - s1 % 16
_s0 = 4 * s0
_s1 = int(x.shape[1] * (_s0 / x.shape[0]))
_s1 = (_s1 + 8) - (_s1 + 8) % 16
else:
s1 = k
s0 = int(x.shape[0] * (k / x.shape[1]))
s0 = s0 - s0 % 16
_s1 = 4 * s1
_s0 = int(x.shape[0] * (_s1 / x.shape[1]))
_s0 = (_s0 + 8) - (_s0 + 8) % 16
new_min = min(_s1, _s0)
raw_min = min(x.shape[0], x.shape[1])
if new_min < raw_min:
interpolation = cv2.INTER_AREA
else:
interpolation = cv2.INTER_LANCZOS4
y = cv2.resize(x, (_s1, _s0), interpolation=interpolation)
return y
def d_resize(x, d, fac=1.0):
new_min = min(int(d[1] * fac), int(d[0] * fac))
raw_min = min(x.shape[0], x.shape[1])
if new_min < raw_min:
interpolation = cv2.INTER_AREA
else:
interpolation = cv2.INTER_LANCZOS4
y = cv2.resize(x, (int(d[1] * fac), int(d[0] * fac)), interpolation=interpolation)
return y
def n_resize(x, d):
y = cv2.resize(x, (d[1], d[0]), interpolation=cv2.INTER_NEAREST)
return y
def s_resize(x, s):
if x.shape[0] < x.shape[1]:
s0 = x.shape[0]
s1 = int(float(s0) / float(s[0]) * float(s[1]))
else:
s1 = x.shape[1]
s0 = int(float(s1) / float(s[1]) * float(s[0]))
new_max = max(s1, s0)
raw_max = max(x.shape[0], x.shape[1])
if new_max < raw_max:
interpolation = cv2.INTER_AREA
else:
interpolation = cv2.INTER_LANCZOS4
y = cv2.resize(x, (s1, s0), interpolation=interpolation)
return y
def min_resize(x, m):
if x.shape[0] < x.shape[1]:
s0 = m
s1 = int(float(m) / float(x.shape[0]) * float(x.shape[1]))
else:
s0 = int(float(m) / float(x.shape[1]) * float(x.shape[0]))
s1 = m
new_max = min(s1, s0)
raw_max = min(x.shape[0], x.shape[1])
if new_max < raw_max:
interpolation = cv2.INTER_AREA
else:
interpolation = cv2.INTER_LANCZOS4
y = cv2.resize(x, (s1, s0), interpolation=interpolation)
return y
def max_resize(x, m):
if x.shape[0] > x.shape[1]:
s0 = m
s1 = int(float(m) / float(x.shape[0]) * float(x.shape[1]))
else:
s0 = int(float(m) / float(x.shape[1]) * float(x.shape[0]))
s1 = m
new_max = max(s1, s0)
raw_max = max(x.shape[0], x.shape[1])
if new_max < raw_max:
interpolation = cv2.INTER_AREA
else:
interpolation = cv2.INTER_LANCZOS4
y = cv2.resize(x, (s1, s0), interpolation=interpolation)
return y
def s_enhance(x, k=2.0):
p = cv2.cvtColor(x, cv2.COLOR_RGB2HSV).astype(np.float)
p[:, :, 1] *= k
p = p.clip(0, 255).astype(np.uint8)
return cv2.cvtColor(p, cv2.COLOR_HSV2RGB).clip(0, 255)
def sss_enhance(x, k=2.0):
p = cv2.cvtColor(x, cv2.COLOR_RGB2HSV).astype(np.float)
p[:, :, 1] *= k
p[:, :, 2] = 255
p = p.clip(0, 255).astype(np.uint8)
return cv2.cvtColor(p, cv2.COLOR_HSV2RGB).clip(0, 255)
def ini_hint(x):
r = np.zeros(shape=(x.shape[0], x.shape[1], 4), dtype=np.uint8)
return r
def opreate_gird_hint(gird, points, type, length):
h = gird.shape[0]
w = gird.shape[1]
for point in points:
x, y, r, g, b, t = point
if t == type:
x = int(x * w)
y = int(y * h)
l_ = max(0, x - length)
b_ = max(0, y - length)
r_ = min(w, x + length + 1)
t_ = min(h, y + length + 1)
gird[b_:t_, l_:r_, 2] = 1 - r / 255.0
gird[b_:t_, l_:r_, 1] = 1 - g / 255.0
gird[b_:t_, l_:r_, 0] = 1 - b / 255.0
gird[b_:t_, l_:r_, 3] = 1
return gird
def opreate_normal_hint(gird, points, length, skip_sp):
h = gird.shape[0]
w = gird.shape[1]
for point in points:
x, y, r, g, b = point
x = int(x * w)
y = int(y * h)
l_ = max(0, x - length)
b_ = max(0, y - length)
r_ = min(w, x + length + 1)
t_ = min(h, y + length + 1)
if skip_sp:
if r == 1 and g == 233 and b == 0:
continue
elif r == 0 and g == 233 and b == 1:
continue
else:
gird[b_:t_, l_:r_, 2] = r
gird[b_:t_, l_:r_, 1] = g
gird[b_:t_, l_:r_, 0] = b
gird[b_:t_, l_:r_, 3] = 255.0
else:
if r == 1 and g == 233 and b == 0:
gird[b_:t_, l_:r_, 2] = r
gird[b_:t_, l_:r_, 1] = g
gird[b_:t_, l_:r_, 0] = b
gird[b_:t_, l_:r_, 3] = 255.0
elif r == 0 and g == 233 and b == 1:
gird[b_:t_, l_:r_, 2] = r
gird[b_:t_, l_:r_, 1] = g
gird[b_:t_, l_:r_, 0] = b
gird[b_:t_, l_:r_, 3] = 255.0
else:
continue
return gird
def opreate_non_paramic_hints(gird, points, type):
points_r = []
colors_r = []
h = gird.shape[0]
w = gird.shape[1]
for point in points:
x, y, r, g, b, t = point
if t in type:
x = int(x * w)
y = int(y * h)
points_r.append([y, x])
colors_r.append([b, g, r])
return points_r, colors_r
def go_cvline(img):
x = cv2.Sobel(img, cv2.CV_16S, 1, 0)
y = cv2.Sobel(img, cv2.CV_16S, 0, 1)
absX = cv2.convertScaleAbs(x)
absY = cv2.convertScaleAbs(y)
r = 255 - cv2.addWeighted(absX, 0.5, absY, 0.5, 0)
return np.tile(np.min(r, axis=2, keepdims=True).clip(0, 255).astype(np.uint8), [1, 1, 3])
def go_passline(img):
o = img.astype(np.float32)
b = cv2.GaussianBlur(img, (7, 7), 0).astype(np.float32)
r = np.max(b - o, axis=2, keepdims=True)
r /= np.max(cv2.resize(r.clip(0, 255).astype(np.uint8), (64, 64), cv2.INTER_AREA))
r = (1 - r).clip(0, 1)
return np.tile((r * 255.0).clip(0, 255).astype(np.uint8), [1, 1, 3])
def min_k_down(x, k):
y = 255 - x.astype(np.float32)
y = block_reduce(y, (k, k), np.max)
y = 255 - y
return y.clip(0, 255).astype(np.uint8)
def min_k_down_c(x, k):
y = 255 - x.astype(np.float32)
y = block_reduce(y, (k, k, 1), np.max)
y = 255 - y
return y.clip(0, 255).astype(np.uint8)
def mini_norm(x):
y = x.astype(np.float32)
y = 1 - y / 255.0
y -= np.min(y)
y /= np.max(y)
return (255.0 - y * 80.0).astype(np.uint8)
def hard_norm(x):
o = x.astype(np.float32)
b = cv2.GaussianBlur(x, (3, 3), 0).astype(np.float32)
y = (o - b + 255.0).clip(0, 255)
y = 1 - y / 255.0
y -= np.min(y)
y /= np.max(y)
y[y < np.mean(y)] = 0
y[y > 0] = 1
return (255.0 - y * 255.0).astype(np.uint8)
def sensitive(x, s=15.0):
y = x.astype(np.float32)
y -= s
y /= 255.0 - s * 2.0
y *= 255.0
return y.clip(0, 255).astype(np.uint8)
def min_black(x):
return np.tile(np.min(x, axis=2, keepdims=True), [1, 1, 3])
def eye_black(x):
return cv2.cvtColor(cv2.cvtColor(x, cv2.COLOR_RGB2GRAY), cv2.COLOR_GRAY2RGB)
def cal_std(x):
y = (cv2.resize(x, (128, 128), cv2.INTER_AREA)).astype(np.float32)
return np.mean(np.var(y, axis=2))
def emph_line(x, y, c):
a = x.astype(np.float32)
b = y.astype(np.float32)[:, :, None] / 255.0
c = np.tile(c[None, None, ::-1], [a.shape[0], a.shape[1], 1])
return (a * b + c * (1 - b)).clip(0, 255).astype(np.uint8)
def de_line(x, y):
a = x.astype(np.float32)
b = y.astype(np.float32)[:, :, None] / 255.0
c = np.tile(np.array([255, 255, 255])[None, None, ::-1], [a.shape[0], a.shape[1], 1])
return (a * b + c * (1 - b)).clip(0, 255).astype(np.uint8)
def blur_line(x, y):
o = x.astype(np.float32)
b = cv2.GaussianBlur(x, (3, 3), 0).astype(np.float32)
k = y.astype(np.float32)[:, :, None] / 255.0
return (o * k + b * (1 - k)).clip(0, 255).astype(np.uint8)
def clip_15(x, s=15.0):
return ((x - s) / (255.0 - s - s)).clip(0, 1) * 255.0
def cv_denoise(x):
return cv2.fastNlMeansDenoisingColored(x, None, 3, 3, 7, 21)
def norm_sketch(x):
tiny_image = cv2.resize(x, (256, 256), interpolation=cv2.INTER_AREA)
min = np.min(tiny_image)
max = np.max(tiny_image)
y = x.astype(np.float)
y -= min
y /= max - min
y *= 255.0
return y.clip(0, 255).astype(np.uint8)
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(16, 16))
def go_cal(x):
r = clahe.apply(x[:, :, 0])
g = clahe.apply(x[:, :, 1])
b = clahe.apply(x[:, :, 2])
img = np.stack([r, g, b], axis=2)
return img
def shrink(x):
a = cv2.resize(x, (x.shape[1] // 2, x.shape[0] // 2), cv2.INTER_AREA)
b = a[:, ::-1]
c = a[::-1, :]
d = a[::-1, ::-1]
e = np.concatenate([a, b], axis=1)
f = np.concatenate([c, d], axis=1)
g = np.concatenate([e, f], axis=0)
return g
barriersss = np.zeros(shape=(1024, 1024), dtype=np.uint8)
for _x in range(1024):
for _y in range(1024):
if _x % 32 == 0 or _y % 32 == 0 or _x % 32 == 1 or _y % 32 == 1 or _x % 32 == 2 or _y % 32 == 2 or _x % 32 == 3 or _y % 32 == 3 or _x % 32 == 4 or _y % 32 == 4:
barriersss[_x, _y] = 1
def check_filter(x):
kbas = cv2.resize(barriersss, (x.shape[1], x.shape[0]), interpolation=cv2.INTER_NEAREST)
result = np.zeros_like(x)
result[kbas > 0] = x[kbas > 0]
return result
def get_hue_direction(source, target):
h1 = cv2.cvtColor(source, cv2.COLOR_RGB2HSV)[:, :, 0].astype(np.float32)
h2 = cv2.cvtColor(target, cv2.COLOR_RGB2HSV)[:, :, 0].astype(np.float32)
h3 = h2 + 256
h4 = h2 - 256
r1 = h2 - h1
r2 = h3 - h1
r3 = h4 - h1
rs = r1.copy()
rs[np.abs(r2) < np.abs(rs)] = r2[np.abs(r2) < np.abs(rs)]
rs[np.abs(r3) < np.abs(rs)] = r3[np.abs(r3) < np.abs(rs)]
rs[rs < 0] = 0
rs[rs > 0] = 255
return rs.clip(0, 255).astype(np.uint8)
def small_norm(x):
x = cv2.resize(x, (256, 256), cv2.INTER_AREA)
x = np_max_pool(x)
x = np_max_pool(x)
x = np_max_pool(x)
x = cv2.GaussianBlur(x, (0, 0), 3.0)
return x
def cli_norm(sketch):
tiny_sketch = cv2.resize(sketch, (256, 256), interpolation=cv2.INTER_AREA).astype(np.float32)
tiny_min = np.min(tiny_sketch)
tiny_max = np.max(tiny_sketch)
return ((sketch.astype(np.float32) - tiny_min) / (tiny_max - tiny_min) * 255.0).clip(0, 255).astype(np.uint8)
def image_colorfulness(image):
R = image[:, :, 0].astype(np.float32)
G = image[:, :, 1].astype(np.float32)
B = image[:, :, 2].astype(np.float32)
R -= np.mean(R)
G -= np.mean(G)
B -= np.mean(B)
rg = np.absolute(R - G)
yb = np.absolute(0.5 * (R + G) - B)
(rbMean, rbStd) = (np.mean(rg), np.std(rg))
(ybMean, ybStd) = (np.mean(yb), np.std(yb))
stdRoot = np.sqrt((rbStd ** 2) + (ybStd ** 2))
meanRoot = np.sqrt((rbMean ** 2) + (ybMean ** 2))
return stdRoot + (0.3 * meanRoot)
def reason_blending(color, sketch):
color = (color.astype(np.float32) / 255.0).clip(0, 1)
sketch = (sketch.astype(np.float32) / 255.0).clip(0, 1)
sketch_r = sketch.copy()
sketch_r = sketch_r ** 5
color_max = np.max(color, axis=2, keepdims=True)
downs = color ** np.pi
downs = (downs + 1e-10) / (np.max(downs, axis=2, keepdims=True) + 1e-10) * color_max
bleeding = color * sketch_r + downs * (1 - sketch_r)
result_YUV = cv2.cvtColor((bleeding * 255.0).clip(0, 255).astype(np.uint8), cv2.COLOR_RGB2YUV)
sketch_YUV = cv2.cvtColor((sketch * 255.0).clip(0, 255).astype(np.uint8), cv2.COLOR_RGB2YUV)
result_YUV[:, :, 0] = np.minimum(result_YUV[:, :, 0], sketch_YUV[:, :, 0])
return cv2.cvtColor(result_YUV, cv2.COLOR_YUV2RGB)
def absmax(a, axis=None):
amax = a.max(axis)
amin = a.min(axis)
return np.where(-amin > amax, amin, amax)
|