Skip to content

Added histogram equalization in computer vision #12527

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 33 additions & 0 deletions computer_vision/histogram_equalization.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import cv2
import numpy as np

"""
Histogram Equalization for Image Enhancement
https://en.wikipedia.org/wiki/Histogram_equalization
"""


def hist_equalization(image):
"""
Returns the histogram equalization image
:param image: input image
"""
L, a, b = cv2.split(image)

Check failure on line 15 in computer_vision/histogram_equalization.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N806)

computer_vision/histogram_equalization.py:15:5: N806 Variable `L` in function should be lowercase
histogram = cv2.calcHist([L], [0], None, [256], [0, 256])
histogram_sum = np.sum(histogram)
probability_density_function = histogram / histogram_sum
cumulative_distribution_function = np.cumsum(probability_density_function)
lookup_table = np.round(cumulative_distribution_function * 255).astype(np.uint8)
equalized_L = cv2.LUT(L, lookup_table)

Check failure on line 21 in computer_vision/histogram_equalization.py

View workflow job for this annotation

GitHub Actions / ruff

Ruff (N806)

computer_vision/histogram_equalization.py:21:5: N806 Variable `equalized_L` in function should be lowercase
new_image = cv2.merge((equalized_L, a, b))
new_image = cv2.cvtColor(new_image, cv2.COLOR_LAB2BGR)
return new_image


if __name__ == "__main__":
image = cv2.imread("path_to_image")
image = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)
new_image = hist_equalization(image)
cv2.imshow("image", new_image)
cv2.waitKey(0)
cv2.destroyAllWindows()
8 changes: 5 additions & 3 deletions greedy_methods/fractional_knapsack.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,11 @@ def frac_knapsack(vl, wt, w, n):
return (
0
if k == 0
else sum(vl[:k]) + (w - acc[k - 1]) * (vl[k]) / (wt[k])
if k != n
else sum(vl[:k])
else (
sum(vl[:k]) + (w - acc[k - 1]) * (vl[k]) / (wt[k])
if k != n
else sum(vl[:k])
)
)


Expand Down
8 changes: 5 additions & 3 deletions matrix/matrix_class.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,9 +204,11 @@ def cofactors(self) -> Matrix:
return Matrix(
[
[
self.minors().rows[row][column]
if (row + column) % 2 == 0
else self.minors().rows[row][column] * -1
(
self.minors().rows[row][column]
if (row + column) % 2 == 0
else self.minors().rows[row][column] * -1
)
for column in range(self.minors().num_columns)
]
for row in range(self.minors().num_rows)
Expand Down
Loading