import cv2 import numpy as np from PIL import Image import requests # Difference of Gaussians applied to img input def dog(img,size=(0,0),k=1.6,sigma=0.5,gamma=1): img1 = cv2.GaussianBlur(img,size,sigma) img2 = cv2.GaussianBlur(img,size,sigma*k) return (img1-gamma*img2) # Threshold the dog image, with dog(sigma,k) > 0 ? 1(255):0(0) def edge_dog(img,sigma=0.5,k=200,gamma=0.98): aux = dog(img,sigma=sigma,k=k,gamma=0.98) aux[aux>0] = 255 aux[aux<=0] = 0 return aux # garygrossi xdog version def xdog_garygrossi(img,sigma=0.5,k=200, gamma=0.98,epsilon=0.1,phi=10): aux = dog(img,sigma=sigma,k=k,gamma=gamma)/255 aux_copy = aux.copy() aux_copy[aux >= epsilon] = 1 aux_copy[aux < epsilon] = 1 + np.tanh(phi*(aux[aux < epsilon] - epsilon)) return aux_copy*255 def hatchBlend(image): xdogImage = xdog(image,sigma=1,k=200, gamma=0.5,epsilon=-0.5,phi=10) hatchTexture = cv2.imread('./imgs/hatch.jpg', cv2.CV_LOAD_IMAGE_GRAYSCALE) hatchTexture = cv2.resize(hatchTexture,(image.shape[1],image.shape[0])) alpha = 0.120 return (1-alpha)*xdogImage + alpha*hatchTexture # version of xdog inspired by article def xdog(img,sigma=0.5,k=1.6, gamma=1,epsilon=1,phi=1): aux = dog(img,sigma=sigma,k=k,gamma=gamma)/255 aux_copy = aux.copy() aux_copy[aux >= epsilon] = 255*(1 + np.tanh(phi*(aux[aux>=epsilon]))) aux_copy[aux < epsilon] = 255 return aux_copy def to_sketch(img_orig, sigma=0.3, k=4.5, gamma=0.95, epsilon=-1, phi=10e15, area_min=2): img_cnts = [] img = cv2.cvtColor(np.array(img_orig), cv2.COLOR_RGB2GRAY) img_xdog = xdog(img, sigma=sigma, k=k, gamma=gamma, epsilon=epsilon, phi=phi).astype(np.uint8) new_img = np.zeros_like(img_xdog) thresh = cv2.threshold(img_xdog, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1] cnts = cv2.findContours(thresh.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) cnts = cnts[0] if len(cnts) == 2 else cnts[1] for c in cnts: area = cv2.contourArea(c) if area > area_min: img_cnts.append(c) return Image.fromarray(255 - cv2.drawContours(new_img, img_cnts, -1, (255,255,255), -1))