Spaces:
Runtime error
Runtime error
import gradio | |
import cv2 | |
def create_gaborfilter(): | |
# This function is designed to produce a set of GaborFilters | |
# an even distribution of theta values equally distributed amongst pi rad / 180 degree | |
filters = [] | |
num_filters = 16 | |
ksize = 35 # The local area to evaluate | |
sigma = 3.0 # Larger Values produce more edges | |
lambd = 10.0 | |
gamma = 0.5 | |
psi = 0 # Offset value - lower generates cleaner results | |
for theta in np.arange(0, np.pi, np.pi / num_filters): # Theta is the orientation for edge detection | |
kern = cv2.getGaborKernel((ksize, ksize), sigma, theta, lambd, gamma, psi, ktype=cv2.CV_64F) | |
kern /= 1.0 * kern.sum() # Brightness normalization | |
filters.append(kern) | |
return filters | |
def apply_filter(img, filters): | |
# This general function is designed to apply filters to our image | |
# First create a numpy array the same size as our input image | |
newimage = np.zeros_like(img) | |
# Starting with a blank image, we loop through the images and apply our Gabor Filter | |
# On each iteration, we take the highest value (super impose), until we have the max value across all filters | |
# The final image is returned | |
depth = -1 # remain depth same as original image | |
for kern in filters: # Loop through the kernels in our GaborFilter | |
image_filter = cv2.filter2D(img, depth, kern) #Apply filter to image | |
# Using Numpy.maximum to compare our filter and cumulative image, taking the higher value (max) | |
np.maximum(newimage, image_filter, newimage) | |
return newimage | |
def greet(image, in_contrast=1.15, in_brightness=20): | |
in_contrast = float(in_contrast) | |
in_brightness = float(in_brightness) | |
# contrast [1.0-3.0] | |
# brightness [0-100] | |
# https://docs.opencv.org/4.x/d3/dc1/tutorial_basic_linear_transform.html | |
new_image = cv2.convertScaleAbs(image, alpha=in_contrast, beta=in_brightness) | |
# We create our gabor filters, and then apply them to our image | |
# gfilters = create_gaborfilter() | |
# new_image = apply_filter(new_image, gfilters) | |
return new_image | |
iface = gradio.Interface( | |
fn=greet, | |
inputs=['image', gradio.Slider(1,3), gradio.Slider(0, 100)], | |
outputs=['image'], | |
examples=[['detail_with_lines_and_noise.jpg']], | |
) | |
iface.launch() | |