Vita:Python

Az oldal más nyelven nem érhető el.
A Wikiszótárból, a nyitott szótárból
import cv2
import numpy as np
import sys
import os

# Check if the image path is provided as argument
if len(sys.argv) < 2:
    print("Usage: python script.py <image_path>")
    sys.exit(1)

# Load the image
image = cv2.imread(sys.argv[1])
input_file = cv2.imread(sys.argv[1])

# Convert the image to grayscale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Apply Sobel edge detection along the x-axis (vertical edges)
sobel_x = cv2.Sobel(gray, cv2.CV_64F, 1, 0, ksize=3)
edges = cv2.convertScaleAbs(sobel_x)

# Apply binary thresholding to highlight edges
_, edges = cv2.threshold(edges, 50, 255, cv2.THRESH_BINARY)

# Apply Hough Line Transform to detect vertical lines
lines = cv2.HoughLinesP(edges, 1, np.pi/180, threshold=100, minLineLength=520, maxLineGap=9)

# Split the image along the detected line
if lines is not None:
    for line in lines:
        x1, _, x2, _ = line[0]
        midpoint = (x1 + x2) // 2
        left_half = image[:, :midpoint]
        right_half = image[:, midpoint:]
        break  # Considering only the first detected line

# Display the result
cv2.imshow('Left Half', left_half)
cv2.imshow('Right Half', right_half)
 
cv2.destroyAllWindows()

base_name = sys.argv[1]

# Generate output file names based on the input file name
left_output_file = base_name + '_left.jpg'
right_output_file = base_name + '_right.jpg'

# Save the split columns as separate image files
cv2.imwrite(left_output_file, left_half)
cv2.imwrite(right_output_file, right_half)