update core.imagelib

This commit is contained in:
Colombo 2020-03-13 19:27:13 +04:00
commit 7c89077321
3 changed files with 70 additions and 16 deletions

View file

@ -2,7 +2,7 @@ import numpy as np
from .blursharpen import LinearMotionBlur
import cv2
def apply_random_hsv_shift(img, rnd_state=None):
def apply_random_hsv_shift(img, mask=None, rnd_state=None):
if rnd_state is None:
rnd_state = np.random
@ -10,44 +10,57 @@ def apply_random_hsv_shift(img, rnd_state=None):
h = ( h + rnd_state.randint(360) ) % 360
s = np.clip ( s + rnd_state.random()-0.5, 0, 1 )
v = np.clip ( v + rnd_state.random()/2-0.25, 0, 1 )
img = np.clip( cv2.cvtColor(cv2.merge([h, s, v]), cv2.COLOR_HSV2BGR) , 0, 1 )
return img
def apply_random_motion_blur( img, chance, mb_max_size, rnd_state=None ):
result = np.clip( cv2.cvtColor(cv2.merge([h, s, v]), cv2.COLOR_HSV2BGR) , 0, 1 )
if mask is not None:
result = img*(1-mask) + result*mask
return result
def apply_random_motion_blur( img, chance, mb_max_size, mask=None, rnd_state=None ):
if rnd_state is None:
rnd_state = np.random
mblur_rnd_kernel = rnd_state.randint(mb_max_size)+1
mblur_rnd_deg = rnd_state.randint(360)
result = img
if rnd_state.randint(100) < np.clip(chance, 0, 100):
img = LinearMotionBlur (img, mblur_rnd_kernel, mblur_rnd_deg )
result = LinearMotionBlur (result, mblur_rnd_kernel, mblur_rnd_deg )
if mask is not None:
result = img*(1-mask) + result*mask
return img
return result
def apply_random_gaussian_blur( img, chance, kernel_max_size, rnd_state=None ):
def apply_random_gaussian_blur( img, chance, kernel_max_size, mask=None, rnd_state=None ):
if rnd_state is None:
rnd_state = np.random
result = img
if rnd_state.randint(100) < np.clip(chance, 0, 100):
gblur_rnd_kernel = rnd_state.randint(kernel_max_size)*2+1
img = cv2.GaussianBlur(img, (gblur_rnd_kernel,)*2 , 0)
return img
result = cv2.GaussianBlur(result, (gblur_rnd_kernel,)*2 , 0)
if mask is not None:
result = img*(1-mask) + result*mask
return result
def apply_random_bilinear_resize( img, chance, max_size_per, rnd_state=None ):
def apply_random_bilinear_resize( img, chance, max_size_per, mask=None, rnd_state=None ):
if rnd_state is None:
rnd_state = np.random
result = img
if rnd_state.randint(100) < np.clip(chance, 0, 100):
h,w,c = img.shape
h,w,c = result.shape
trg = rnd_state.rand()
rw = w - int( trg * int(w*(max_size_per/100.0)) )
rh = h - int( trg * int(h*(max_size_per/100.0)) )
img = cv2.resize (img, (rw,rh), cv2.INTER_LINEAR )
img = cv2.resize (img, (w,h), cv2.INTER_LINEAR )
return img
result = cv2.resize (result, (rw,rh), cv2.INTER_LINEAR )
result = cv2.resize (result, (w,h), cv2.INTER_LINEAR )
if mask is not None:
result = img*(1-mask) + result*mask
return result

View file

@ -0,0 +1 @@
from .draw import *

40
core/imagelib/sd/draw.py Normal file
View file

@ -0,0 +1,40 @@
"""
Signed distance drawing functions using numpy.
"""
import numpy as np
from numpy import linalg as npla
def circle_faded( hw, center, fade_dists ):
"""
returns drawn circle in [h,w,1] output range [0..1.0] float32
hw = [h,w] resolution
center = [y,x] center of circle
fade_dists = [fade_start, fade_end] fade values
"""
h,w = hw
pts = np.empty( (h,w,2), dtype=np.float32 )
pts[...,1] = np.arange(h)[None,:]
pts[...,0] = np.arange(w)[:,None]
pts = pts.reshape ( (h*w, -1) )
pts_dists = np.abs ( npla.norm(pts-center, axis=-1) )
pts_dists = ( pts_dists - fade_dists[0] ) / fade_dists[1]
pts_dists = np.clip( 1-pts_dists, 0, 1)
return pts_dists.reshape ( (h,w,1) ).astype(np.float32)
def random_circle_faded ( hw, rnd_state=None ):
if rnd_state is None:
rnd_state = np.random
h,w = hw
hw_max = max(h,w)
fade_start = rnd_state.randint(hw_max)
fade_end = fade_start + rnd_state.randint(hw_max- fade_start)
return circle_faded (hw, [ rnd_state.randint(h), rnd_state.randint(w) ],
[fade_start, fade_end] )