Spaces:
Running
Running
Create image_processor.py
Browse files- image_processor.py +38 -0
image_processor.py
ADDED
@@ -0,0 +1,38 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import numpy as np
|
3 |
+
from scipy import ndimage
|
4 |
+
|
5 |
+
class ImageProcessor:
|
6 |
+
def __init__(self):
|
7 |
+
pass
|
8 |
+
|
9 |
+
def preprocess_image(self, image):
|
10 |
+
"""Preprocess the input image"""
|
11 |
+
# Convert to grayscale
|
12 |
+
if len(image.shape) == 3:
|
13 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
14 |
+
else:
|
15 |
+
gray = image
|
16 |
+
|
17 |
+
# Apply Gaussian blur
|
18 |
+
blurred = cv2.GaussianBlur(gray, (5, 5), 0)
|
19 |
+
|
20 |
+
return blurred
|
21 |
+
|
22 |
+
def generate_depth_map(self, image):
|
23 |
+
"""Generate a depth map from the image"""
|
24 |
+
# Preprocess the image
|
25 |
+
processed = self.preprocess_image(image)
|
26 |
+
|
27 |
+
# Use Sobel operators to detect edges
|
28 |
+
sobel_x = cv2.Sobel(processed, cv2.CV_64F, 1, 0, ksize=3)
|
29 |
+
sobel_y = cv2.Sobel(processed, cv2.CV_64F, 0, 1, ksize=3)
|
30 |
+
|
31 |
+
# Combine the gradients
|
32 |
+
gradient_magnitude = np.sqrt(sobel_x**2 + sobel_y**2)
|
33 |
+
|
34 |
+
# Normalize the depth map
|
35 |
+
depth_map = cv2.normalize(gradient_magnitude, None, 0, 1, cv2.NORM_MINMAX)
|
36 |
+
|
37 |
+
return depth_map
|
38 |
+
|