Skip to content

Commit 5cef77a

Browse files
committed
Added histogram equalization in computer vision
1 parent 0040ad4 commit 5cef77a

File tree

19 files changed

+130
-87
lines changed

19 files changed

+130
-87
lines changed

ciphers/base64_cipher.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -105,13 +105,13 @@ def base64_decode(encoded_data: str) -> bytes:
105105

106106
# Check if the encoded string contains non base64 characters
107107
if padding:
108-
assert all(char in B64_CHARSET for char in encoded_data[:-padding]), (
109-
"Invalid base64 character(s) found."
110-
)
108+
assert all(
109+
char in B64_CHARSET for char in encoded_data[:-padding]
110+
), "Invalid base64 character(s) found."
111111
else:
112-
assert all(char in B64_CHARSET for char in encoded_data), (
113-
"Invalid base64 character(s) found."
114-
)
112+
assert all(
113+
char in B64_CHARSET for char in encoded_data
114+
), "Invalid base64 character(s) found."
115115

116116
# Check the padding
117117
assert len(encoded_data) % 4 == 0 and padding < 3, "Incorrect padding"
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import cv2
2+
import numpy as np
3+
4+
"""
5+
Histogram Equalization for Image Enhancement
6+
https://en.wikipedia.org/wiki/Histogram_equalization
7+
"""
8+
9+
10+
def hist_equalization(image):
11+
"""
12+
Returns the histogram equalization image
13+
:param image: input image
14+
"""
15+
L, a, b = cv2.split(image)
16+
histogram = cv2.calcHist([L], [0], None, [256], [0, 256])
17+
histogram_sum = np.sum(histogram)
18+
probability_density_function = histogram / histogram_sum
19+
cumulative_distribution_function = np.cumsum(probability_density_function)
20+
lookup_table = np.round(cumulative_distribution_function * 255).astype(np.uint8)
21+
equalized_L = cv2.LUT(L, lookup_table)
22+
new_image = cv2.merge((equalized_L, a, b))
23+
new_image = cv2.cvtColor(new_image, cv2.COLOR_LAB2BGR)
24+
return new_image
25+
26+
27+
if __name__ == "__main__":
28+
image = cv2.imread("path_to_image")
29+
image = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)
30+
new_image = hist_equalization(image)
31+
cv2.imshow("image", new_image)
32+
cv2.waitKey(0)
33+
cv2.destroyAllWindows()

data_structures/hashing/number_theory/prime_numbers.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,9 @@ def is_prime(number: int) -> bool:
3232
"""
3333

3434
# precondition
35-
assert isinstance(number, int) and (number >= 0), (
36-
"'number' must been an int and positive"
37-
)
35+
assert isinstance(number, int) and (
36+
number >= 0
37+
), "'number' must been an int and positive"
3838

3939
if 1 < number < 4:
4040
# 2 and 3 are primes

data_structures/heap/min_heap.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,9 +124,9 @@ def is_empty(self):
124124
return len(self.heap) == 0
125125

126126
def decrease_key(self, node, new_value):
127-
assert self.heap[self.idx_of_element[node]].val > new_value, (
128-
"newValue must be less that current value"
129-
)
127+
assert (
128+
self.heap[self.idx_of_element[node]].val > new_value
129+
), "newValue must be less that current value"
130130
node.val = new_value
131131
self.heap_dict[node.name] = new_value
132132
self.sift_up(self.idx_of_element[node])

data_structures/kd_tree/tests/test_kdtree.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,14 @@ def test_build_kdtree(num_points, cube_size, num_dimensions, depth, expected_res
4848
assert kdtree is not None, "Expected a KDNode, got None"
4949

5050
# Check if root has correct dimensions
51-
assert len(kdtree.point) == num_dimensions, (
52-
f"Expected point dimension {num_dimensions}, got {len(kdtree.point)}"
53-
)
51+
assert (
52+
len(kdtree.point) == num_dimensions
53+
), f"Expected point dimension {num_dimensions}, got {len(kdtree.point)}"
5454

5555
# Check that the tree is balanced to some extent (simplistic check)
56-
assert isinstance(kdtree, KDNode), (
57-
f"Expected KDNode instance, got {type(kdtree)}"
58-
)
56+
assert isinstance(
57+
kdtree, KDNode
58+
), f"Expected KDNode instance, got {type(kdtree)}"
5959

6060

6161
def test_nearest_neighbour_search():

data_structures/suffix_tree/tests/test_suffix_tree.py

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -22,37 +22,37 @@ def test_search_existing_patterns(self) -> None:
2222
patterns = ["ana", "ban", "na"]
2323
for pattern in patterns:
2424
with self.subTest(pattern=pattern):
25-
assert self.suffix_tree.search(pattern), (
26-
f"Pattern '{pattern}' should be found."
27-
)
25+
assert self.suffix_tree.search(
26+
pattern
27+
), f"Pattern '{pattern}' should be found."
2828

2929
def test_search_non_existing_patterns(self) -> None:
3030
"""Test searching for patterns that do not exist in the suffix tree."""
3131
patterns = ["xyz", "apple", "cat"]
3232
for pattern in patterns:
3333
with self.subTest(pattern=pattern):
34-
assert not self.suffix_tree.search(pattern), (
35-
f"Pattern '{pattern}' should not be found."
36-
)
34+
assert not self.suffix_tree.search(
35+
pattern
36+
), f"Pattern '{pattern}' should not be found."
3737

3838
def test_search_empty_pattern(self) -> None:
3939
"""Test searching for an empty pattern."""
4040
assert self.suffix_tree.search(""), "An empty pattern should be found."
4141

4242
def test_search_full_text(self) -> None:
4343
"""Test searching for the full text."""
44-
assert self.suffix_tree.search(self.text), (
45-
"The full text should be found in the suffix tree."
46-
)
44+
assert self.suffix_tree.search(
45+
self.text
46+
), "The full text should be found in the suffix tree."
4747

4848
def test_search_substrings(self) -> None:
4949
"""Test searching for substrings of the full text."""
5050
substrings = ["ban", "ana", "a", "na"]
5151
for substring in substrings:
5252
with self.subTest(substring=substring):
53-
assert self.suffix_tree.search(substring), (
54-
f"Substring '{substring}' should be found."
55-
)
53+
assert self.suffix_tree.search(
54+
substring
55+
), f"Substring '{substring}' should be found."
5656

5757

5858
if __name__ == "__main__":

digital_image_processing/filters/gabor_filter.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,9 @@ def gabor_filter_kernel(
4848
_y = -sin_theta * px + cos_theta * py
4949

5050
# fill kernel
51-
gabor[y, x] = np.exp(-(_x**2 + gamma**2 * _y**2) / (2 * sigma**2)) * np.cos(
52-
2 * np.pi * _x / lambd + psi
53-
)
51+
gabor[y, x] = np.exp(
52+
-(_x**2 + gamma**2 * _y**2) / (2 * sigma**2)
53+
) * np.cos(2 * np.pi * _x / lambd + psi)
5454

5555
return gabor
5656

dynamic_programming/climbing_stairs.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,9 @@ def climb_stairs(number_of_steps: int) -> int:
2525
...
2626
AssertionError: number_of_steps needs to be positive integer, your input -7
2727
"""
28-
assert isinstance(number_of_steps, int) and number_of_steps > 0, (
29-
f"number_of_steps needs to be positive integer, your input {number_of_steps}"
30-
)
28+
assert (
29+
isinstance(number_of_steps, int) and number_of_steps > 0
30+
), f"number_of_steps needs to be positive integer, your input {number_of_steps}"
3131
if number_of_steps == 1:
3232
return 1
3333
previous, current = 1, 1

dynamic_programming/iterating_through_submasks.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ def list_of_submasks(mask: int) -> list[int]:
3737
3838
"""
3939

40-
assert isinstance(mask, int) and mask > 0, (
41-
f"mask needs to be positive integer, your input {mask}"
42-
)
40+
assert (
41+
isinstance(mask, int) and mask > 0
42+
), f"mask needs to be positive integer, your input {mask}"
4343

4444
"""
4545
first submask iterated will be mask itself then operation will be performed

greedy_methods/fractional_knapsack.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,11 @@ def frac_knapsack(vl, wt, w, n):
3939
return (
4040
0
4141
if k == 0
42-
else sum(vl[:k]) + (w - acc[k - 1]) * (vl[k]) / (wt[k])
43-
if k != n
44-
else sum(vl[:k])
42+
else (
43+
sum(vl[:k]) + (w - acc[k - 1]) * (vl[k]) / (wt[k])
44+
if k != n
45+
else sum(vl[:k])
46+
)
4547
)
4648

4749

0 commit comments

Comments
 (0)