Simple-image-to-3d / image_processor.py
Felguk's picture
Create image_processor.py
53fe985 verified
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