Finish coding
This commit is contained in:
@@ -19,6 +19,9 @@ class InvisCloak (Algorithm):
|
||||
# Includes noice reduction and histogram spread
|
||||
self.middle_value_picture = None
|
||||
|
||||
# Buffer for background image
|
||||
self.background = None
|
||||
|
||||
# Clean up results folder
|
||||
folder_path = os.path.join("results")
|
||||
|
||||
@@ -78,6 +81,7 @@ class InvisCloak (Algorithm):
|
||||
elif event == cv2.EVENT_MBUTTONUP:
|
||||
# Save current image as background
|
||||
cv2.imwrite(f"results/background.png", self.picture_buffer[self.n - 1])
|
||||
self.background = cv2.imread("results/background.png")
|
||||
|
||||
|
||||
def _plotNoise(self, img, name:str):
|
||||
@@ -172,29 +176,23 @@ class InvisCloak (Algorithm):
|
||||
Hier steht Ihr Code zu Aufgabe 2.3.1 (StatischesSchwellwertverfahren)
|
||||
- Binärmaske erstellen
|
||||
"""
|
||||
# 0 = blue, 1 = green, 2 = red
|
||||
selected_color_channel = 2
|
||||
# Convert BGR -> HSV
|
||||
hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
|
||||
|
||||
# Color threshold values for color deletion
|
||||
lower_bound, upper_bound = 100, 255
|
||||
# Area 1: H = 0-10 (strong Rot)
|
||||
lower_red1 = np.array([0, 100, 50])
|
||||
upper_red1 = np.array([0, 255, 255])
|
||||
|
||||
# Creating binary mask
|
||||
binary_mask = (lower_bound < img[:, :, selected_color_channel]) * (img[:, :, selected_color_channel] < upper_bound)
|
||||
# Area 2: H = 169-179 (red-violet)
|
||||
lower_red2 = np.array([171, 100, 50])
|
||||
upper_red2 = np.array([179, 255, 255])
|
||||
|
||||
# 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")
|
||||
# Create binary mask for both red areas
|
||||
mask1 = cv2.inRange(hsv, lower_red1, upper_red1)
|
||||
mask2 = cv2.inRange(hsv, lower_red2, upper_red2)
|
||||
|
||||
# Combine both masks
|
||||
mask = cv2.bitwise_or(mask1, mask2)
|
||||
|
||||
"""
|
||||
Hier steht Ihr Code zu Aufgabe 2.3.2 (Binärmaske)
|
||||
@@ -202,12 +200,42 @@ class InvisCloak (Algorithm):
|
||||
- Wahl größte zusammenhängende Region
|
||||
"""
|
||||
|
||||
# Optimizing mask with opening and closing
|
||||
kernel = np.ones((5, 5), np.uint8)
|
||||
|
||||
mask = cv2.morphologyEx(mask, cv2.MORPH_OPEN, kernel)
|
||||
mask = cv2.morphologyEx(mask, cv2.MORPH_CLOSE, kernel)
|
||||
|
||||
# Select biggest connected area
|
||||
cnts, _ = cv2.findContours(mask, cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
|
||||
|
||||
if cnts:
|
||||
c = max(cnts, key=cv2.contourArea)
|
||||
new_mask = np.zeros_like(mask)
|
||||
cv2.drawContours(new_mask, [c], -1, color = 255, thickness = -1)
|
||||
mask = new_mask
|
||||
else:
|
||||
mask = np.zeros_like(mask)
|
||||
|
||||
"""
|
||||
Hier steht Ihr Code zu Aufgabe 2.3.1 (Bildmodifizerung)
|
||||
Hier steht Ihr Code zu Aufgabe 2.3.3 (Bildmodifizerung)
|
||||
- Hintergrund mit Mausklick definieren
|
||||
- Ersetzen des Hintergrundes
|
||||
"""
|
||||
|
||||
# Return image if no background is set
|
||||
if self.background is None:
|
||||
return img
|
||||
|
||||
return img
|
||||
# 3-channel mask for binary operations
|
||||
mask_3ch = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
|
||||
|
||||
# Background are: Get from save background image
|
||||
background_part = cv2.bitwise_and(self.background, mask_3ch)
|
||||
|
||||
# Foreground area: Extract from current image
|
||||
foreground_part = cv2.bitwise_and(img, cv2.bitwise_not(mask_3ch))
|
||||
|
||||
# Merge both areas
|
||||
output = cv2.add(background_part, foreground_part)
|
||||
return output
|
||||
|
||||
Reference in New Issue
Block a user