Spaces:
Sleeping
Sleeping
File size: 2,188 Bytes
1d64201 |
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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
import cv2
import numpy as np
import os
# Function to preprocess the image
def preprocessing(image_path):
image = cv2.imread(image_path)
if image is None:
print(f"Warning: Could not read {image_path}")
return None
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
denoisy_img = cv2.GaussianBlur(gray, (5, 5), 0)
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
clahe_img = clahe.apply(denoisy_img)
_, thresholded_img = cv2.threshold(clahe_img, 150, 255, cv2.THRESH_BINARY)
edges = cv2.Canny(thresholded_img, 100, 220, apertureSize=3)
lines = cv2.HoughLinesP(edges, rho=1, theta=np.pi / 180, threshold=50,
minLineLength=35, maxLineGap=5)
output_img = cv2.cvtColor(gray, cv2.COLOR_GRAY2BGR)
if lines is not None:
for line in lines:
x1, y1, x2, y2 = line[0]
cv2.line(output_img, (x1, y1), (x2, y2), (210, 210, 210), 1)
blended_image = cv2.addWeighted(image, 0.7, output_img, 0.3, 0)
return blended_image
# Define paths
source_root = "../cubicasa5k"
output_dir = "dataset/images"
script_dir = os.path.dirname(os.path.abspath(__file__))
os.chdir(script_dir)
print(f"Fixed Working Directory: {os.getcwd()}")
# Create output directories if they don't exist
os.makedirs("dataset", exist_ok=True)
os.makedirs(output_dir, exist_ok=True)
# Iterate over subfolders 1, 2, 3, ..., n
for subfolder in os.listdir(source_root):
subfolder_path = os.path.join(source_root, subfolder)
if os.path.isdir(subfolder_path): # Ensure it's a directory
image_path = os.path.join(subfolder_path, "F1_original.png")
if os.path.exists(image_path):
processed_img = preprocessing(image_path)
if processed_img is not None:
output_filename = f"{subfolder}.png" # Save with subfolder name
output_path = os.path.join(output_dir, output_filename)
cv2.imwrite(output_path, processed_img)
print(f"Processed: {image_path} -> {output_path}")
else:
print(f"Skipping {subfolder}: F1_original.png not found")
print("Processing completed.")
|