Files
ovt-projekt/main.py
2025-04-23 10:18:58 +02:00

46 lines
1.5 KiB
Python

import sys
import cv2
from pathlib import Path
def laplace_edge_detection(image_path, output_path):
"""
Opens an image, applies Laplace filtering for edge detection,
and saves the resulting grayscale image.
Args:
image_path (str): Path to the input image file.
output_path (str): Path to save the processed image.
"""
try:
# Open the image in grayscale mode
img = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
if img is None:
print(f"Error: Could not open or read image at {image_path}")
return
# Apply Laplace filter for edge detection
laplace_kernel = np.array([[0, 1, 0],
[1, -4, 1],
[0, 1, 0]]) # Standard Laplace kernel
laplace_filtered = cv2.filter2D(img, -1, laplace_kernel)
# Convert to uint8 (grayscale 8-bit) and clip values to the valid range [0, 255]
laplace_filtered = np.uint8(laplace_filtered) # Important for saving correctly
# Save the resulting image
cv2.imwrite(output_path, laplace_filtered)
print(f"Laplace edge detection complete. Image saved to {output_path}")
except Exception as e:
print(f"An error occurred: {e}")
if __name__ == "__main__":
import numpy as np # Import here, only if needed for the main execution
input_path = sys.argv[1]
output_path = sys.argv[2]
laplace_edge_detection(input_path, output_path)