95 lines
2.7 KiB
Python
95 lines
2.7 KiB
Python
import os
|
|
|
|
import cv2
|
|
import cv2 as cv
|
|
import os
|
|
|
|
class Obraz:
|
|
def __init__(self, filename:str, mode:int = cv.IMREAD_COLOR_BGR):
|
|
self.filename = filename
|
|
self.mode = mode
|
|
self.loaded = False
|
|
self.img = None
|
|
self.img_other = None
|
|
self.transform_name = "NOTHING"
|
|
|
|
def load_image(self):
|
|
if self.loaded:
|
|
return True
|
|
if not os.path.exists(self.filename):
|
|
return False
|
|
# raise FileNotFoundError(self.filename)
|
|
try:
|
|
self.img = cv.imread(self.filename, flags=self.mode)
|
|
self.loaded = True
|
|
except Exception as e:
|
|
print(e)
|
|
|
|
return True
|
|
|
|
def show_image(self, window_name:str="Window"):
|
|
if not self.loaded:
|
|
return False
|
|
window_name += f" - {self.filename}"
|
|
cv.imshow(window_name, self.img)
|
|
cv.waitKey(0)
|
|
cv.destroyAllWindows()
|
|
return None
|
|
|
|
def show_image_other(self, window_name:str="Window after "):
|
|
if not self.loaded:
|
|
return False
|
|
if self.img_other is None:
|
|
return False
|
|
window_name += f" - {self.filename} - {self.transform_name}"
|
|
combined = cv.hconcat([self.img, self.img_other])
|
|
cv.imshow(window_name, combined)
|
|
cv.waitKey(0)
|
|
cv.destroyAllWindows()
|
|
return None
|
|
|
|
def transform_equalize_histogram(self):
|
|
if not self.loaded:
|
|
return False
|
|
if self.mode == cv.IMREAD_COLOR or self.mode == cv.IMREAD_COLOR_BGR:
|
|
gray_image = cv2.cvtColor(self.img, cv2.COLOR_BGR2GRAY)
|
|
self.img_other = cv.equalizeHist(gray_image)
|
|
self.transform_name = "Equalize Histogram"
|
|
return True
|
|
|
|
def transform_smoothing(self, smoothing_type="GaussianBlur"):
|
|
"""
|
|
|
|
:param smoothing_type: GaussianBlur (default) or medianBlur
|
|
:return: True or False
|
|
"""
|
|
if not self.loaded:
|
|
return False
|
|
try:
|
|
if smoothing_type == "GaussianBlur":
|
|
self.img_other = cv.GaussianBlur(self.img, (5, 5), 0)
|
|
else:
|
|
self.img_other = cv.medianBlur(self.img, 5)
|
|
self.transform_name = smoothing_type
|
|
return True
|
|
except Exception as e:
|
|
print(e)
|
|
return False
|
|
|
|
def return_image_stream(self, type:str="other"):
|
|
"""
|
|
|
|
:param type: other | original
|
|
:return:
|
|
"""
|
|
if not self.loaded:
|
|
return None
|
|
if type == "original":
|
|
return self.img
|
|
elif type == "other":
|
|
if self.img_other is None:
|
|
return None
|
|
return self.img_other
|
|
else:
|
|
return None
|