vikaso001 commited on
Commit
c8bfd94
Β·
verified Β·
1 Parent(s): 2f2a181

Upload 4 files

Browse files
preprocessing/__init__.py ADDED
File without changes
preprocessing/__pycache__/__init__.cpython-312.pyc ADDED
Binary file (177 Bytes). View file
 
preprocessing/__pycache__/process.cpython-312.pyc ADDED
Binary file (5.09 kB). View file
 
preprocessing/process.py ADDED
@@ -0,0 +1,116 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ from pathlib import Path
3
+ import numpy as np
4
+ from matplotlib import pyplot as plt
5
+
6
+ def Advanced_gray(image_path):
7
+ # Define the image you want to preprocess
8
+ IMG_NAME = image_path # πŸ” Change to your image name
9
+ SOURCE_IMG_PATH = Path("./") / IMG_NAME
10
+ DEST_IMG_PATH = Path("test") / IMG_NAME
11
+
12
+ # Ensure output directory exists
13
+ DEST_IMG_PATH.parent.mkdir(parents=True, exist_ok=True)
14
+
15
+ # Load in grayscale
16
+ img = cv2.imread(str(SOURCE_IMG_PATH), cv2.IMREAD_GRAYSCALE)
17
+
18
+ if img is None:
19
+ print(f"❌ Failed to load image: {SOURCE_IMG_PATH}")
20
+ return None
21
+ else:
22
+ print(f"βœ… Loaded image: {SOURCE_IMG_PATH.name}")
23
+
24
+ # Apply Gaussian blur to remove noise
25
+ img = cv2.GaussianBlur(img, (5, 5), 0)
26
+
27
+ # Apply adaptive thresholding
28
+ img = cv2.adaptiveThreshold(
29
+ img, 255,
30
+ cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
31
+ cv2.THRESH_BINARY,
32
+ 11, 2
33
+ )
34
+
35
+ # Save the processed image
36
+ cv2.imwrite(str(DEST_IMG_PATH), img)
37
+ print(f"βœ… Processed image saved to: {DEST_IMG_PATH}")
38
+ return img
39
+
40
+ # Sharpening function
41
+ def sharpen_image(image):
42
+ kernel = np.array([[0, -1, 0],
43
+ [-1, 5,-1],
44
+ [0, -1, 0]])
45
+ sharpened = cv2.filter2D(image, -1, kernel)
46
+ return sharpened
47
+
48
+ def enhance_contrast(image):
49
+ lab = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)
50
+ l, a, b = cv2.split(lab)
51
+
52
+ clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
53
+ cl = clahe.apply(l)
54
+
55
+ limg = cv2.merge((cl, a, b))
56
+ enhanced_img = cv2.cvtColor(limg, cv2.COLOR_LAB2BGR)
57
+ return enhanced_img
58
+
59
+ def modify_image(image_path):
60
+ x = Advanced_gray(image_path)
61
+ if x is None:
62
+ return None
63
+ y = sharpen_image(x)
64
+ # Convert grayscale to BGR before enhancing contrast
65
+ y_bgr = cv2.cvtColor(y, cv2.COLOR_GRAY2BGR)
66
+ z = enhance_contrast(y_bgr)
67
+ output_path = Path("test") / Path(image_path).name
68
+ cv2.imwrite(str(output_path), z)
69
+ return z
70
+
71
+ def batch_process_images(image_dir, limit=10):
72
+ image_dir_path = Path(image_dir)
73
+ output_dir = Path("test")
74
+ output_dir.mkdir(parents=True, exist_ok=True)
75
+
76
+ images_processed = 0
77
+ processed_images = []
78
+ for img_path in image_dir_path.glob("*.jpg"):
79
+ if images_processed >= limit:
80
+ break
81
+ print(f"Processing image: {img_path.name}")
82
+ processed_img = modify_image(str(img_path))
83
+ if processed_img is not None:
84
+ print(f"Saved processed image to: {output_dir / img_path.name}")
85
+ processed_images.append(processed_img)
86
+ images_processed += 1
87
+ else:
88
+ print(f"Failed to process image: {img_path.name}")
89
+
90
+ # Plot all processed images in a single plot
91
+ if processed_images:
92
+ cols = 5
93
+ rows = (len(processed_images) + cols - 1) // cols
94
+ plt.figure(figsize=(15, 3 * rows))
95
+ for i, img in enumerate(processed_images):
96
+ plt.subplot(rows, cols, i + 1)
97
+ if len(img.shape) == 2: # grayscale
98
+ plt.imshow(img, cmap='gray')
99
+ else:
100
+ plt.imshow(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
101
+ plt.axis('off')
102
+ plt.tight_layout()
103
+ plt.show()
104
+
105
+ # if __name__ == "__main__":
106
+ # # Process a single image
107
+ # image_path = "data/10.jpg"
108
+ # processed_img = modify_image(image_path)
109
+ # if processed_img is not None:
110
+ # plt.figure(figsize=(6,6))
111
+ # if len(processed_img.shape) == 2:
112
+ # plt.imshow(processed_img, cmap='gray')
113
+ # else:
114
+ # plt.imshow(cv2.cvtColor(processed_img, cv2.COLOR_BGR2RGB))
115
+ # plt.axis('off')
116
+ # plt.show()