Finish 2.3.1 exercise 1

This commit is contained in:
2025-06-27 15:58:46 +02:00
parent 12c70e3bbc
commit cc5199146a
2 changed files with 45 additions and 3 deletions

View File

@@ -63,7 +63,7 @@ class InvisCloak (Algorithm):
# Stores the current image to data folder
cv2.imwrite(f"results/{datetime.datetime.now().strftime('%Y-%m-%d_%H:%M:%S')}_original_image.png",
self.picture_buffer[4])
self.picture_buffer[self.n - 1])
# Create RGB histogram
self._221_RGB(self.middle_value_picture)
@@ -71,6 +71,14 @@ class InvisCloak (Algorithm):
# Create HSV histogram
self._222_HSV(self.middle_value_picture)
# Get binary mask and write it to file
binary_mask = self._23_SegmentUndBildmodifizierung(self.middle_value_picture, True)
cv2.imwrite(f"results/{datetime.datetime.now().strftime('%Y-%m-%d_%H:%M:%S')}_binary_mask.png",
binary_mask)
elif event == cv2.EVENT_MBUTTONUP:
# Save current image as background
cv2.imwrite(f"results/background.png", self.picture_buffer[self.n - 1])
def _plotNoise(self, img, name:str):
height, width = np.array(img.shape[:2])
@@ -136,14 +144,16 @@ class InvisCloak (Algorithm):
Hier steht Ihr Code zu Aufgabe 2.2.1 (RGB)
- Histogrammberechnung und Analyse
"""
# Names of the color spectrums
# Names of the colors in histogram
channels = ["b", "g", "r"]
# Calc histogram
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])
# Save histogram, clear cache
plt.savefig(f"results/{datetime.datetime.now().strftime('%Y-%m-%d_%H:%M:%S')}_histogram_{colorspectrum}.png")
plt.clf()
@@ -153,14 +163,37 @@ class InvisCloak (Algorithm):
Hier steht Ihr Code zu Aufgabe 2.2.2 (HSV)
- Histogrammberechnung und Analyse im HSV-Raum
"""
# Convert image to hsv, call _221_RGB to create histogram
self._221_RGB(cv2.cvtColor(img, cv2.COLOR_BGR2HSV), "hsv")
def _23_SegmentUndBildmodifizierung (self, img):
def _23_SegmentUndBildmodifizierung (self, img, save_binary_mask = False):
"""
Hier steht Ihr Code zu Aufgabe 2.3.1 (StatischesSchwellwertverfahren)
- Binärmaske erstellen
"""
# 0 = blue, 1 = green, 2 = red
selected_color_channel = 2
# Color threshold values for color deletion
lower_bound, upper_bound = 100, 255
# Creating binary mask
binary_mask = (lower_bound < img[:, :, selected_color_channel]) * (img[:, :, selected_color_channel] < upper_bound)
# Store binary mask to results folder
if save_binary_mask:
cv2.imwrite(f"results/{datetime.datetime.now().strftime('%Y-%m-%d_%H:%M:%S')}_binary_mask.png",
binary_mask * 256)
try:
# Get background image
background = cv2.imread("results/background.png")
# Apply mask to image
img[binary_mask] = background[binary_mask]
except:
print("No background image")
"""