Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
|
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
import gradio as gr
|
| 4 |
+
|
| 5 |
+
# Function to process the PSR moon image
|
| 6 |
+
def process_image(input_image):
|
| 7 |
+
# Convert PIL Image to a NumPy array
|
| 8 |
+
image = np.array(input_image)
|
| 9 |
+
|
| 10 |
+
# Convert image to grayscale
|
| 11 |
+
gray_image = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
|
| 12 |
+
|
| 13 |
+
# Apply CLAHE (Contrast Limited Adaptive Histogram Equalization) for enhanced contrast
|
| 14 |
+
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8)) # Increased clipLimit for stronger contrast
|
| 15 |
+
cl1 = clahe.apply(gray_image)
|
| 16 |
+
|
| 17 |
+
# Create a mask for darker regions (e.g., craters) based on pixel intensity
|
| 18 |
+
_, mask = cv2.threshold(gray_image, 80, 255, cv2.THRESH_BINARY_INV) # Adjusted threshold for more precise mask
|
| 19 |
+
|
| 20 |
+
# Apply CLAHE only to the masked regions (darker areas)
|
| 21 |
+
brightened_craters = cv2.bitwise_and(cl1, cl1, mask=mask)
|
| 22 |
+
|
| 23 |
+
# Combine brightened craters with the original image using weighted addition
|
| 24 |
+
combined = cv2.addWeighted(brightened_craters, 1.3, cl1, 0.9, 0) # Adjusted weights for better blending
|
| 25 |
+
|
| 26 |
+
# Further brighten the final image by increasing alpha (contrast) and beta (brightness)
|
| 27 |
+
brightened_image = cv2.convertScaleAbs(combined, alpha=1.2, beta=30) # Increased contrast and moderate brightness
|
| 28 |
+
|
| 29 |
+
# Define a mask for the darker spots that need additional brightening
|
| 30 |
+
dark_spots_mask = cv2.inRange(brightened_image, 0, 110) # Refined range for more precise dark spot targeting
|
| 31 |
+
|
| 32 |
+
# Increase the brightness of only the dark areas by blending with a brighter image
|
| 33 |
+
brightened_dark_spots = cv2.addWeighted(brightened_image, 1.0, dark_spots_mask, 0.4, 40)
|
| 34 |
+
|
| 35 |
+
# Combine the brightened dark spots with the rest of the image
|
| 36 |
+
final_image = cv2.bitwise_or(brightened_image, brightened_dark_spots)
|
| 37 |
+
|
| 38 |
+
return final_image
|
| 39 |
+
|
| 40 |
+
# Create a Gradio app using Blocks
|
| 41 |
+
with gr.Blocks() as app:
|
| 42 |
+
gr.Markdown("## Enhanced Lunar PSR Image")
|
| 43 |
+
|
| 44 |
+
# Image upload component
|
| 45 |
+
img_input = gr.Image(label="Upload PSR Moon Image", type="pil")
|
| 46 |
+
|
| 47 |
+
# Button to trigger processing
|
| 48 |
+
process_button = gr.Button("Process Image")
|
| 49 |
+
|
| 50 |
+
# Output image component to display the processed image
|
| 51 |
+
img_output = gr.Image(label="Enhanced Image")
|
| 52 |
+
|
| 53 |
+
# Define the interaction between components
|
| 54 |
+
process_button.click(process_image, inputs=img_input, outputs=img_output)
|
| 55 |
+
|
| 56 |
+
# Launch the app
|
| 57 |
+
app.launch()
|