import cv2 import numpy as np def sketch_photo(image_path, output_path, scale_factor=0.6): # Read the image image = cv2.imread(image_path) # Resize the image to improve processing speed height, width = image.shape[:2] new_width = int(scale_factor * width) new_height = int(scale_factor * height) resized_image = cv2.resize(image, (new_width, new_height)) # Convert the image to grayscale grayscale_image = cv2.cvtColor(resized_image, cv2.COLOR_BGR2GRAY) # Apply GaussianBlur to reduce noise and improve edge detection blurred_image = cv2.GaussianBlur(grayscale_image, (5, 5), 0) # Use the Canny edge detector to find edges in the image edges = cv2.Canny(blurred_image, 30, 100) # Invert the binary image (black and white) inverted_edges = cv2.bitwise_not(edges) # Create a blank white canvas canvas = np.ones_like(resized_image) * 255 # Combine the original image with the inverted edges sketch = cv2.bitwise_or(canvas, canvas, mask=inverted_edges) # Save the resulting sketch cv2.imwrite(output_path, sketch) # Example usage input_image_path = "path/to/your/photo.jpg" output_sketch_path = "path/to/your/output_sketch.jpg" sketch_photo(input_image_path, output_sketch_path)
No comments:
Post a Comment