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