mirror of
https://github.com/TheAlgorithms/Python.git
synced 2024-11-30 16:31:08 +00:00
bc8df6de31
* [pre-commit.ci] pre-commit autoupdate updates: - [github.com/astral-sh/ruff-pre-commit: v0.2.2 → v0.3.2](https://github.com/astral-sh/ruff-pre-commit/compare/v0.2.2...v0.3.2) - [github.com/pre-commit/mirrors-mypy: v1.8.0 → v1.9.0](https://github.com/pre-commit/mirrors-mypy/compare/v1.8.0...v1.9.0) * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
65 lines
1.9 KiB
Python
65 lines
1.9 KiB
Python
"""
|
|
Created on Fri Sep 28 15:22:29 2018
|
|
|
|
@author: Binish125
|
|
"""
|
|
|
|
import copy
|
|
import os
|
|
|
|
import cv2
|
|
import numpy as np
|
|
from matplotlib import pyplot as plt
|
|
|
|
|
|
class ConstantStretch:
|
|
def __init__(self):
|
|
self.img = ""
|
|
self.original_image = ""
|
|
self.last_list = []
|
|
self.rem = 0
|
|
self.L = 256
|
|
self.sk = 0
|
|
self.k = 0
|
|
self.number_of_rows = 0
|
|
self.number_of_cols = 0
|
|
|
|
def stretch(self, input_image):
|
|
self.img = cv2.imread(input_image, 0)
|
|
self.original_image = copy.deepcopy(self.img)
|
|
x, _, _ = plt.hist(self.img.ravel(), 256, [0, 256], label="x")
|
|
self.k = np.sum(x)
|
|
for i in range(len(x)):
|
|
prk = x[i] / self.k
|
|
self.sk += prk
|
|
last = (self.L - 1) * self.sk
|
|
if self.rem != 0:
|
|
self.rem = int(last % last)
|
|
last = int(last + 1 if self.rem >= 0.5 else last)
|
|
self.last_list.append(last)
|
|
self.number_of_rows = int(np.ma.count(self.img) / self.img[1].size)
|
|
self.number_of_cols = self.img[1].size
|
|
for i in range(self.number_of_cols):
|
|
for j in range(self.number_of_rows):
|
|
num = self.img[j][i]
|
|
if num != self.last_list[num]:
|
|
self.img[j][i] = self.last_list[num]
|
|
cv2.imwrite("output_data/output.jpg", self.img)
|
|
|
|
def plot_histogram(self):
|
|
plt.hist(self.img.ravel(), 256, [0, 256])
|
|
|
|
def show_image(self):
|
|
cv2.imshow("Output-Image", self.img)
|
|
cv2.imshow("Input-Image", self.original_image)
|
|
cv2.waitKey(5000)
|
|
cv2.destroyAllWindows()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
file_path = os.path.join(os.path.basename(__file__), "image_data/input.jpg")
|
|
stretcher = ConstantStretch()
|
|
stretcher.stretch(file_path)
|
|
stretcher.plot_histogram()
|
|
stretcher.show_image()
|