-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
151 lines (118 loc) · 3.94 KB
/
Copy pathmain.py
File metadata and controls
151 lines (118 loc) · 3.94 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
import cv2
import numpy as np
import os
import matplotlib.pyplot as plt
def RGBtoGray(imgg, method):
# Resolve absolute path
script_dir = os.path.dirname(os.path.abspath(__file__))
img_path = os.path.join(script_dir, imgg)
# Read the image (BGR in OpenCV)
I = cv2.imread(img_path)
if I is None:
print("Error: Could not read the image.")
return
# Convert to RGB for calculations
I_rgb = cv2.cvtColor(I, cv2.COLOR_BGR2RGB)
M, N, _ = I_rgb.shape
OutputImage = None
if method == 1:
# Weighted sum method
R = I_rgb[:, :, 0].astype(float)
G = I_rgb[:, :, 1].astype(float)
B = I_rgb[:, :, 2].astype(float)
Gray = 0.298936 * R + 0.587043 * G + 0.114021 * B
OutputImage = Gray.astype(np.uint8)
elif method== 2:
# Average method
OutputImage = np.mean(I_rgb, axis=2).astype(np.uint8)
elif method== 3:
# Loop method
OutputImage = np.zeros((M, N), dtype=np.uint8)
for i in range(M):
for j in range(N):
R = float(I_rgb[i, j, 0])
G = float(I_rgb[i, j, 1])
B = float(I_rgb[i, j, 2])
OutputImage[i, j] = int(0.298936 * R + 0.587043 * G + 0.114021 * B)
def ShowREDplane(imgg):
# Resolve absolute path
script_dir = os.path.dirname(os.path.abspath(__file__))
img_path = os.path.join(script_dir, imgg)
# Read the image (BGR in OpenCV)
I = cv2.imread(img_path)
if I is None:
print("Error: Could not read the image.")
return
# Convert to RGB for calculations
I_rgb = cv2.cvtColor(I, cv2.COLOR_BGR2RGB)
M, N, _ = I_rgb.shape
OutputImage = None
Ired = I_rgb.copy()
Ired[:, :, 1] = 0
Ired[:, :, 2] = 0
OutputImage = Ired
def ShowGREENplane(imgg):
# Resolve absolute path
script_dir = os.path.dirname(os.path.abspath(__file__))
img_path = os.path.join(script_dir, imgg)
# Read the image (BGR in OpenCV)
I = cv2.imread(img_path)
if I is None:
print("Error: Could not read the image.")
return
# Convert to RGB for calculations
I_rgb = cv2.cvtColor(I, cv2.COLOR_BGR2RGB)
M, N, _ = I_rgb.shape
OutputImage = None
Ig = I_rgb.copy()
Ig[:, :, 0] = 0
Ig[:, :, 2] = 0
OutputImage = Ig
def ShowBLUEplane(imgg):
# Resolve absolute path
script_dir = os.path.dirname(os.path.abspath(__file__))
img_path = os.path.join(script_dir, imgg)
# Read the image (BGR in OpenCV)
I = cv2.imread(img_path)
if I is None:
print("Error: Could not read the image.")
return
# Convert to RGB for calculations
I_rgb = cv2.cvtColor(I, cv2.COLOR_BGR2RGB)
M, N, _ = I_rgb.shape
OutputImage = None
Ib = I_rgb.copy()
Ib[:, :, 0] = 0
Ib[:, :, 1] = 0
OutputImage = Ib
def GrayToBW(imgg):
# Resolve absolute path
script_dir = os.path.dirname(os.path.abspath(__file__))
img_path = os.path.join(script_dir, imgg)
# Read the image (BGR in OpenCV)
I = cv2.imread(img_path)
if I is None:
print("Error: Could not read the image.")
return
# Convert to RGB for calculations
I_rgb = cv2.cvtColor(I, cv2.COLOR_BGR2RGB)
M, N, _ = I_rgb.shape
OutputImage = None
R = I_rgb[:, :, 0].astype(float)
G = I_rgb[:, :, 1].astype(float)
B = I_rgb[:, :, 2].astype(float)
Gray = 0.298936 * R + 0.587043 * G + 0.114021 * B
Gray = Gray.astype(np.uint8)
BW = np.where(Gray <= 127, 0, 255).astype(np.uint8)
OutputImage = BW
plt.figure(figsize=(6, 6))
plt.imshow(OutputImage)
plt.axis('off')
plt.show()
RGBtoGray("img_process",1)
RGBtoGray("img_process",2)
RGBtoGray("img_process",3)
ShowREDplane("img_process")
ShowBLUEplane("img_process")
ShowGREENplane("img_process")
GrayToBW("img_process")