Skip to content

Commit 8c88c87

Browse files
committed
Apply python black style 🎱
1 parent 8a99528 commit 8c88c87

File tree

4 files changed

+57
-39
lines changed

4 files changed

+57
-39
lines changed

code-example/test_image_im2dhisteq.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
from im2dhisteq import im2dhisteq
2-
import numpy as np
2+
import numpy as np
33
import cv2
44
import os
55

6-
filename = 'assets/cloudy-day-500.jpg'
6+
filename = "assets/cloudy-day-500.jpg"
77

88
name, ext = os.path.splitext(filename)
99
image = cv2.imread(filename)
@@ -19,6 +19,5 @@
1919
image_hsv[:, :, 2] = image_v_heq.copy()
2020
image_heq = cv2.cvtColor(image_hsv, cv2.COLOR_HSV2BGR)
2121

22-
cv2.imwrite(f'{name}-im2dhisteq{ext}', image_2dheq)
23-
cv2.imwrite(f'{name}-imhisteq{ext}', image_heq)
24-
22+
cv2.imwrite(f"{name}-im2dhisteq{ext}", image_2dheq)
23+
cv2.imwrite(f"{name}-imhisteq{ext}", image_heq)

code-example/test_video_im2dhisteq.py

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,25 @@
22
import cv2
33
from im2dhisteq import vid2dhisteq
44

5-
cap = cv2.VideoCapture('assets/Arctic-Convoy-With-Giant-Mack-Trucks.mp4')
5+
cap = cv2.VideoCapture("assets/Arctic-Convoy-With-Giant-Mack-Trucks.mp4")
66

77
# video without sound
8-
video_out_name = 'assets/Arctic-Convoy-With-Giant-Mack-Trucks-imWeightedThresholdedheq.mp4'
8+
video_out_name = (
9+
"assets/Arctic-Convoy-With-Giant-Mack-Trucks-imWeightedThresholdedheq.mp4"
10+
)
911

1012
i = 0
1113
j = 0
1214
length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
1315
Wout_list = np.zeros((10))
14-
while(cap.isOpened()):
16+
while cap.isOpened():
1517
ret, frame = cap.read()
1618
if ret == False:
1719
break
18-
print("\r", 100*i//length, "%", end='')
20+
print("\r", 100 * i // length, "%", end="")
1921

2022
frame_hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
21-
frame_v = frame_hsv[:, :, 2].copy()
23+
frame_v = frame_hsv[:, :, 2].copy()
2224
image_heq, Wout = vid2dhisteq(frame_v, Wout_list=Wout_list)
2325
Wout_list[j] = Wout
2426
j += 1
@@ -28,19 +30,32 @@
2830
frame_eq = cv2.cvtColor(frame_hsv, cv2.COLOR_HSV2BGR)
2931

3032
fps = cap.get(cv2.CAP_PROP_FPS)
31-
frame = cv2.putText(frame, 'Original', (10, 20), cv2.FONT_HERSHEY_COMPLEX,
32-
0.5, (255, 255, 255), 1, cv2.LINE_AA)
33-
frame_eq = cv2.putText(frame_eq, 'Enhanced', (10, 20), cv2.FONT_HERSHEY_COMPLEX,
34-
0.5, (255, 255, 255), 1, cv2.LINE_AA)
35-
frame_frame_eq = np.concatenate(
36-
( frame, frame_eq ),
37-
axis= 1
38-
)
39-
if i==0:
33+
frame = cv2.putText(
34+
frame,
35+
"Original",
36+
(10, 20),
37+
cv2.FONT_HERSHEY_COMPLEX,
38+
0.5,
39+
(255, 255, 255),
40+
1,
41+
cv2.LINE_AA,
42+
)
43+
frame_eq = cv2.putText(
44+
frame_eq,
45+
"Enhanced",
46+
(10, 20),
47+
cv2.FONT_HERSHEY_COMPLEX,
48+
0.5,
49+
(255, 255, 255),
50+
1,
51+
cv2.LINE_AA,
52+
)
53+
frame_frame_eq = np.concatenate((frame, frame_eq), axis=1)
54+
if i == 0:
4055
h, w, d = frame_frame_eq.shape
41-
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
56+
fourcc = cv2.VideoWriter_fourcc(*"mp4v")
4257
video_out = cv2.VideoWriter(video_out_name, fourcc, fps, (w, h))
4358
video_out.write(frame_frame_eq)
4459

45-
i+=1
60+
i += 1
4661
cv2.destroyAllWindows()

src/im2dhisteq.py

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
from im2dhist import im2dhist as im2dhist
22
from im2dhist import imhist
3-
import numba
3+
import numba
44
import numpy as np
55

6+
67
@numba.njit()
78
def im2dhisteq(image, w_neighboring=6):
89
V = image.copy()
910
[h, w] = V.shape
1011
V_hist = imhist(V)
1112
H_in = im2dhist(V, w_neighboring=w_neighboring)
12-
CDFx = np.cumsum(np.sum(H_in, axis=0)) # Kx1
13+
CDFx = np.cumsum(np.sum(H_in, axis=0)) # Kx1
1314

1415
# normalizes CDFx
15-
CDFxn = (255*CDFx/CDFx[-1])
16+
CDFxn = 255 * CDFx / CDFx[-1]
1617

1718
PDFxn = np.zeros_like(CDFxn)
1819
PDFxn[0] = CDFxn[0]
@@ -22,10 +23,11 @@ def im2dhisteq(image, w_neighboring=6):
2223
X_transform[np.where(V_hist > 0)] = PDFxn.copy()
2324
CDFxn_transform = np.cumsum(X_transform)
2425

25-
2626
bins = np.arange(256)
2727
# use linear interpolation of cdf to find new pixel values
28-
image_equalized = np.floor(np.interp(V.flatten(), bins, CDFxn_transform).reshape(h, w)).astype(np.uint8)
28+
image_equalized = np.floor(
29+
np.interp(V.flatten(), bins, CDFxn_transform).reshape(h, w)
30+
).astype(np.uint8)
2931

3032
return image_equalized
3133

@@ -38,24 +40,24 @@ def vid2dhisteq(image, w_neighboring=6, Wout_list=np.zeros((10), dtype=np.uint16
3840

3941
H_in = im2dhist(V, w_neighboring=6)
4042

41-
CDFx = np.cumsum(np.sum(H_in, axis=0)) # Kx1
43+
CDFx = np.cumsum(np.sum(H_in, axis=0)) # Kx1
4244

4345
# normalizes CDFx
44-
CDFxn = (CDFx/CDFx[-1])
46+
CDFxn = CDFx / CDFx[-1]
4547

4648
PDFxn = np.zeros_like(CDFxn)
4749
PDFxn[0] = CDFxn[0]
4850
PDFxn[1:] = np.diff(CDFxn)
4951

50-
X_transform = np.zeros((256), dtype=np.float64)
52+
X_transform = np.zeros((256), dtype=np.float64)
5153
X_transform[V_hist > 0] = PDFxn.copy()
5254
CDFxn_transform = np.cumsum(X_transform).astype(np.float64)
5355

5456
Win = H_in.shape[0]
55-
Gmax = 1.5 # 1.5 .. 2
56-
Wout = min(255, Gmax*Win)
57-
if np.where(Wout_list>0)[0].size==Wout_list.size:
58-
Wout = (np.sum(Wout_list)+Wout) / (1+Wout_list.size)
57+
Gmax = 1.5 # 1.5 .. 2
58+
Wout = min(255, Gmax * Win)
59+
if np.where(Wout_list > 0)[0].size == Wout_list.size:
60+
Wout = (np.sum(Wout_list) + Wout) / (1 + Wout_list.size)
5961

6062
F = V.ravel().astype(np.uint16)
6163
Ftilde = Wout * CDFxn_transform[F]

tests/test_im2dhisteq.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,18 @@
22
import numpy as np
33
import cv2
44

5+
56
def test_im2dhisteq_with_param():
6-
image_name = '../assets/Plane.jpg'
7+
image_name = "../assets/Plane.jpg"
78
image = cv2.imread(image_name)
89
# convert rgb image to gray
910
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
10-
11+
1112
gray_image_2DHisteq = im2dhisteq(gray_image, w_neighboring=6)
12-
1313

14-
#np.save(f'{image_name}-2D-Histogram-Equalized', gray_image_2DHisteq)
15-
gray_image_2DHisteq_cmpr = np.load(f'{image_name}-2D-Histogram-Equalized.npy')
14+
# np.save(f'{image_name}-2D-Histogram-Equalized', gray_image_2DHisteq)
15+
gray_image_2DHisteq_cmpr = np.load(f"{image_name}-2D-Histogram-Equalized.npy")
1616
assert np.all(gray_image_2DHisteq == gray_image_2DHisteq_cmpr)
17-
#test_im2dhisteq_with_param()
17+
18+
19+
# test_im2dhisteq_with_param()

0 commit comments

Comments
 (0)