-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtresholding.py
More file actions
47 lines (37 loc) · 1.31 KB
/
tresholding.py
File metadata and controls
47 lines (37 loc) · 1.31 KB
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
import cv2
import numpy as np
import matplotlib.pyplot as plt
# Function to display images
def display_image(title, image):
plt.imshow(image, cmap='gray')
plt.title(title)
plt.axis('off')
plt.show()
# Load Image
image = cv2.imread("image.jpg", cv2.IMREAD_GRAYSCALE)
if image is None:
print("Error: Image not found!")
exit()
# ----- Brightness Improvement -----
brightness_improved = cv2.add(image, 50)
display_image("Brightness Improved", brightness_improved)
# ----- Brightness Reduction -----
brightness_reduced = cv2.subtract(image, 50)
display_image("Brightness Reduced", brightness_reduced)
# ----- Thresholding -----
_, thresholded = cv2.threshold(image, 127, 255, cv2.THRESH_BINARY)
display_image("Thresholded Image", thresholded)
# ----- Negative Image -----
negative = 255 - image
display_image("Negative of Image", negative)
# ----- Log Transformation -----
c_log = 255 / np.log(1 + np.max(image))
log_transformed = c_log * np.log(1 + image.astype(np.float64))
log_transformed = np.array(log_transformed, dtype=np.uint8)
display_image("Log Transformation", log_transformed)
# ----- Power Law (Gamma) Transformation -----
gamma = 1.5
power_law = np.array(255 * (image / 255) ** gamma, dtype=np.uint8)
display_image("Power Law Transformation (Gamma)", power_law)
# Final show (optional)
plt.show()