Finish 2.2.1 exercise 1

This commit is contained in:
2025-06-27 15:20:15 +02:00
parent 1ded249949
commit 20185748ca

View File

@@ -1,11 +1,10 @@
import cv2 import cv2, datetime, os
import numpy as np import numpy as np
from copy import deepcopy from copy import deepcopy
from matplotlib import pyplot as plt from matplotlib import pyplot as plt
from . import Algorithm from . import Algorithm
class InvisCloak (Algorithm): class InvisCloak (Algorithm):
""" init function """ """ init function """
@@ -20,6 +19,15 @@ class InvisCloak (Algorithm):
# Includes noice reduction and histogram spread # Includes noice reduction and histogram spread
self.middle_value_picture = None self.middle_value_picture = None
# Clean up results folder
folder_path = os.path.join("results")
for filename in os.listdir(folder_path):
file_path = os.path.join(folder_path, filename)
if os.path.isfile(file_path):
os.unlink(file_path)
""" Processes the input image""" """ Processes the input image"""
def process(self, img): def process(self, img):
@@ -38,12 +46,12 @@ class InvisCloak (Algorithm):
""" 2.2 Farbanalyse """ """ 2.2 Farbanalyse """
""" 2.2.1 RGB """ """ 2.2.1 RGB """
self._221_RGB(img) #self._221_RGB(img)
""" 2.2.2 HSV """ """ 2.2.2 HSV """
self._222_HSV(img) #self._222_HSV(img)
""" 2.3 Segmentierung und Bildmdifikation """ """ 2.3 Segmentierung und Bildmodifikation """
img = self._23_SegmentUndBildmodifizierung(img) img = self._23_SegmentUndBildmodifizierung(img)
return img return img
@@ -53,6 +61,14 @@ class InvisCloak (Algorithm):
if event == cv2.EVENT_LBUTTONUP: if event == cv2.EVENT_LBUTTONUP:
print("A Mouse click happend! at position", x, y) print("A Mouse click happend! at position", x, y)
# Stores the current image to data folder
cv2.imwrite(f"results/{datetime.datetime.now().strftime('%Y-%m-%d_%H:%M:%S')}_captured_image.png",
self.picture_buffer[4])
# Create RGB histogram
self._221_RGB(self.middle_value_picture)
def _plotNoise(self, img, name:str): def _plotNoise(self, img, name:str):
height, width = np.array(img.shape[:2]) height, width = np.array(img.shape[:2])
centY = (height / 2).astype(int) centY = (height / 2).astype(int)
@@ -112,12 +128,21 @@ class InvisCloak (Algorithm):
return self.middle_value_picture return self.middle_value_picture
def _221_RGB(self, img): def _221_RGB(self, img, colorspectrum = "bgr"):
""" """
Hier steht Ihr Code zu Aufgabe 2.2.1 (RGB) Hier steht Ihr Code zu Aufgabe 2.2.1 (RGB)
- Histogrammberechnung und Analyse - Histogrammberechnung und Analyse
""" """
pass # Names of the color spectrums
channels = ["b", "g", "r"]
for index, channel_name in enumerate(channels):
hist = cv2.calcHist([img], [index], None, [256], [0, 256])
plt.plot(hist, color=channel_name)
plt.xlim([0, 256])
plt.savefig(f"results/{datetime.datetime.now().strftime('%Y-%m-%d_%H:%M:%S')}_histogram_{colorspectrum}.png")
plt.clf()
def _222_HSV(self, img): def _222_HSV(self, img):