Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import gradio as gr
|
| 5 |
+
|
| 6 |
+
def retinex(image, sigma_list):
|
| 7 |
+
"""
|
| 8 |
+
Apply Retinex algorithm to enhance image.
|
| 9 |
+
|
| 10 |
+
:param image: Input image (BGR format)
|
| 11 |
+
:param sigma_list: List of sigma values for Gaussian blur
|
| 12 |
+
:return: Retinex enhanced image
|
| 13 |
+
"""
|
| 14 |
+
# Convert image to float32
|
| 15 |
+
image = np.float32(image) + 1.0
|
| 16 |
+
|
| 17 |
+
# Initialize the Retinex result
|
| 18 |
+
retinex_result = np.zeros_like(image)
|
| 19 |
+
|
| 20 |
+
for sigma in sigma_list:
|
| 21 |
+
# Apply Gaussian blur
|
| 22 |
+
blurred = cv2.GaussianBlur(image, (0, 0), sigma)
|
| 23 |
+
# Compute the Retinex result
|
| 24 |
+
retinex_result += np.log(image) - np.log(blurred)
|
| 25 |
+
|
| 26 |
+
# Normalize and convert back to uint8
|
| 27 |
+
retinex_result = retinex_result / len(sigma_list)
|
| 28 |
+
retinex_result = np.exp(retinex_result)
|
| 29 |
+
retinex_result = cv2.normalize(retinex_result, None, 0, 255, cv2.NORM_MINMAX)
|
| 30 |
+
retinex_result = np.uint8(retinex_result)
|
| 31 |
+
|
| 32 |
+
return retinex_result
|
| 33 |
+
|
| 34 |
+
def enhance_feeble_light_signals(image, alpha, beta, clip_limit, gamma, sigma_list):
|
| 35 |
+
# Apply Retinex enhancement
|
| 36 |
+
retinex_image = retinex(image, sigma_list)
|
| 37 |
+
|
| 38 |
+
# Convert to LAB color space
|
| 39 |
+
lab_image = cv2.cvtColor(retinex_image, cv2.COLOR_BGR2LAB)
|
| 40 |
+
|
| 41 |
+
# Split the LAB image into channels
|
| 42 |
+
l, a, b = cv2.split(lab_image)
|
| 43 |
+
|
| 44 |
+
# Apply CLAHE (Contrast Limited Adaptive Histogram Equalization) to the L channel
|
| 45 |
+
clahe = cv2.createCLAHE(clipLimit=clip_limit, tileGridSize=(8,8))
|
| 46 |
+
cl = clahe.apply(l)
|
| 47 |
+
|
| 48 |
+
# Merge the CLAHE enhanced L channel back with a and b channels
|
| 49 |
+
lab_image_clahe = cv2.merge((cl, a, b))
|
| 50 |
+
|
| 51 |
+
# Convert back to BGR color space
|
| 52 |
+
enhanced_image = cv2.cvtColor(lab_image_clahe, cv2.COLOR_LAB2BGR)
|
| 53 |
+
|
| 54 |
+
# Brighten the image by adjusting contrast (alpha) and brightness (beta)
|
| 55 |
+
brightened_image = cv2.convertScaleAbs(enhanced_image, alpha=alpha, beta=beta)
|
| 56 |
+
|
| 57 |
+
# Apply Gamma Correction
|
| 58 |
+
gamma_corrected = np.power(brightened_image / 255.0, gamma)
|
| 59 |
+
gamma_corrected = np.uint8(gamma_corrected * 255)
|
| 60 |
+
|
| 61 |
+
return gamma_corrected
|
| 62 |
+
|
| 63 |
+
def process_image(input_image, alpha, beta, clip_limit, gamma):
|
| 64 |
+
# Convert image to the format compatible with OpenCV
|
| 65 |
+
input_image = cv2.cvtColor(input_image, cv2.COLOR_RGB2BGR)
|
| 66 |
+
|
| 67 |
+
# Define sigma values for Retinex algorithm
|
| 68 |
+
sigma_list = [15, 80, 250] # You can adjust this as needed
|
| 69 |
+
|
| 70 |
+
# Enhance the image using Retinex and other adjustments
|
| 71 |
+
output_image = enhance_feeble_light_signals(input_image, alpha, beta, clip_limit, gamma, sigma_list)
|
| 72 |
+
|
| 73 |
+
# Convert output image back to RGB for displaying
|
| 74 |
+
output_image = cv2.cvtColor(output_image, cv2.COLOR_BGR2RGB)
|
| 75 |
+
|
| 76 |
+
return input_image, output_image
|
| 77 |
+
|
| 78 |
+
# Define the Gradio interface
|
| 79 |
+
interface = gr.Interface(
|
| 80 |
+
fn=process_image,
|
| 81 |
+
inputs=[
|
| 82 |
+
gr.Image(type="numpy", label="Input Image"),
|
| 83 |
+
gr.Slider(minimum=1.0, maximum=10.0, value=3.0, label="Alpha (Contrast)"),
|
| 84 |
+
gr.Slider(minimum=0, maximum=100, value=20, label="Beta (Brightness)"),
|
| 85 |
+
gr.Slider(minimum=1.0, maximum=15.0, value=10.0, label="CLAHE Clip Limit"),
|
| 86 |
+
gr.Slider(minimum=0.1, maximum=10.0, value=1.5, label="Gamma Correction"),
|
| 87 |
+
],
|
| 88 |
+
outputs=gr.Image(type="numpy", label="Enhanced Image"), # Only the enhanced image is shown
|
| 89 |
+
title="Feeble Light Signal Image Enhancer",
|
| 90 |
+
description="Upload a dark image, and enhance it using Retinex, CLAHE, contrast, brightness, and gamma correction."
|
| 91 |
+
)
|
| 92 |
+
|
| 93 |
+
# Launch the Gradio app
|
| 94 |
+
interface.launch()
|