diff --git a/computer_vision/histogram_equalization.py b/computer_vision/histogram_equalization.py new file mode 100644 index 000000000000..63a11a0a2cbb --- /dev/null +++ b/computer_vision/histogram_equalization.py @@ -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) + 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) + 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() diff --git a/greedy_methods/fractional_knapsack.py b/greedy_methods/fractional_knapsack.py index d52b56f23569..f7455a9c9fce 100644 --- a/greedy_methods/fractional_knapsack.py +++ b/greedy_methods/fractional_knapsack.py @@ -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]) + ) ) diff --git a/matrix/matrix_class.py b/matrix/matrix_class.py index a5940a38e836..230eb95006fa 100644 --- a/matrix/matrix_class.py +++ b/matrix/matrix_class.py @@ -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)