|
| 1 | +--- |
| 2 | +{"publish":true,"title":"Aufgabe 1 und 2","created":"2025-04-05T19:19:37.241+02:00","modified":"2025-09-12T21:25:00.742+02:00","published":"2025-09-12T21:25:00.742+02:00","tags":["2DCV","Semester-5","Informatik"],"cssclasses":""} |
| 3 | +--- |
| 4 | + |
| 5 | + |
| 6 | +## Bild einlesen und anzeigen mithilfe von Scikit-Image |
| 7 | + |
| 8 | + |
| 9 | +```python |
| 10 | +from skimage.io import imread |
| 11 | +import numpy as np |
| 12 | +import matplotlib.pyplot as plt |
| 13 | +``` |
| 14 | + |
| 15 | + |
| 16 | +```python |
| 17 | +def default_show(image, title=None): |
| 18 | + """ |
| 19 | + Displays an image using matplotlib. |
| 20 | +
|
| 21 | + Parameters: |
| 22 | + - image: The image to be displayed. |
| 23 | + - title: Optional title for the plot. |
| 24 | + """ |
| 25 | + plt.figure(figsize=(10, 10)) # Set the figure size to be big |
| 26 | + plt.imshow(image, cmap='gray') |
| 27 | + if title: |
| 28 | + plt.title(title) |
| 29 | + plt.axis('off') |
| 30 | + plt.show() |
| 31 | +``` |
| 32 | + |
| 33 | + |
| 34 | +```python |
| 35 | +image = imread('sea_wall.jpg') |
| 36 | + |
| 37 | +# showing the image |
| 38 | +default_show(image, "Sea Wall") |
| 39 | + |
| 40 | +# printing the type and data of the image |
| 41 | +print("Datentyp:", image.dtype) |
| 42 | +print("Shape:", image.shape) |
| 43 | +print("Dimensionen:", image.ndim) |
| 44 | +``` |
| 45 | + |
| 46 | +![[Studium/2DCV/Bilder/Uebung01_3_0.png]] |
| 47 | + |
| 48 | + |
| 49 | + |
| 50 | + Datentyp: uint8 |
| 51 | + Shape: (322, 468, 3) |
| 52 | + Dimensionen: 3 |
| 53 | + |
| 54 | + |
| 55 | +Das eingelesene Bild ist ein NumPy-Array mit dem Datentyp uint8, d. h. jedes Pixel kann Werte von 0 bis 255 annehmen. |
| 56 | + |
| 57 | +Der Aufbau des Arrays ist dreidimensional mit der Struktur (Höhe, Breite, Kanäle). Ein Farbbild hat typischerweise 3 Kanäle (RGB), also z. B. image.shape = (512, 768, 3). |
| 58 | + |
| 59 | +Jeder Pixel besteht aus drei Werten: Rot, Grün und Blau. |
| 60 | +Zugriff auf den roten Kanal eines Pixels z. B. über image[y, x, 0]. |
| 61 | + |
| 62 | +### Drei Farbkanäle des Bildes getrennt |
| 63 | + |
| 64 | + |
| 65 | +```python |
| 66 | +def read_red_channel(image): |
| 67 | + """ |
| 68 | + Function to read the red channel of an image |
| 69 | + :param image: input image |
| 70 | + :return: red channel of the image |
| 71 | + """ |
| 72 | + # Extracting the red channel |
| 73 | + red_channel = image[:, :, 0] |
| 74 | + return red_channel |
| 75 | + |
| 76 | +def read_green_channel(image): |
| 77 | + """ |
| 78 | + Function to read the green channel of an image |
| 79 | + :param image: input image |
| 80 | + :return: green channel of the image |
| 81 | + """ |
| 82 | + # Extracting the green channel |
| 83 | + green_channel = image[:, :, 1] |
| 84 | + return green_channel |
| 85 | + |
| 86 | +def read_blue_channel(image): |
| 87 | + """ |
| 88 | + Function to read the blue channel of an image |
| 89 | + :param image: input image |
| 90 | + :return: blue channel of the image |
| 91 | + """ |
| 92 | + # Extracting the blue channel |
| 93 | + blue_channel = image[:, :, 2] |
| 94 | + return blue_channel |
| 95 | +``` |
| 96 | + |
| 97 | + |
| 98 | +```python |
| 99 | +# Show each color channel separately |
| 100 | +images = ['hidden.png', 'sea_wall.jpg', 'wood.jpg'] |
| 101 | +fig, axes = plt.subplots(3, 3, figsize=(15, 10)) |
| 102 | + |
| 103 | +for i, img_path in enumerate(images): |
| 104 | + img = imread(img_path) |
| 105 | + axes[i, 0].imshow(read_red_channel(img), cmap='Reds') |
| 106 | + axes[i, 0].set_title(f"{img_path} - Red") |
| 107 | + axes[i, 1].imshow(read_green_channel(img), cmap='Greens') |
| 108 | + axes[i, 1].set_title(f"{img_path} - Green") |
| 109 | + axes[i, 2].imshow(read_blue_channel(img), cmap='Blues') |
| 110 | + axes[i, 2].set_title(f"{img_path} - Blue") |
| 111 | + |
| 112 | +plt.tight_layout() |
| 113 | +plt.show() |
| 114 | +``` |
| 115 | + |
| 116 | + |
| 117 | + |
| 118 | +![[Studium/2DCV/Bilder/Uebung01_7_0.png]] |
| 119 | + |
| 120 | + |
| 121 | + |
| 122 | + |
| 123 | +```python |
| 124 | +def invert_horizontal(image): |
| 125 | + return [row[::-1] for row in image] |
| 126 | + |
| 127 | +def invert_vertical(image): |
| 128 | + return image[::-1] |
| 129 | +``` |
| 130 | + |
| 131 | + |
| 132 | +```python |
| 133 | +image = imread('monkey.jpg') |
| 134 | + |
| 135 | +plt.figure(figsize=(15, 10)) |
| 136 | +plt.subplot(121) |
| 137 | +plt.imshow(invert_horizontal(image)) |
| 138 | +plt.subplot(122) |
| 139 | +plt.imshow(invert_vertical(image)) |
| 140 | +``` |
| 141 | + |
| 142 | + |
| 143 | + |
| 144 | + |
| 145 | + <matplotlib.image.AxesImage at 0x1473084a850> |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | + |
| 150 | + |
| 151 | +![[Studium/2DCV/Bilder/Uebung01_9_1.png]] |
| 152 | + |
| 153 | + |
| 154 | + |
| 155 | + |
| 156 | +```python |
| 157 | +def computeHisto(image): |
| 158 | + histo = np.zeros(256, dtype=int) |
| 159 | + |
| 160 | + height, width = image.shape |
| 161 | + |
| 162 | + # Gehe alle Pixel durch und zähle Häufigkeiten |
| 163 | + for y in range(height): |
| 164 | + for x in range(width): |
| 165 | + intensity = image[y, x] |
| 166 | + histo[intensity] += 1 |
| 167 | + |
| 168 | + return histo |
| 169 | +``` |
| 170 | + |
| 171 | + |
| 172 | +```python |
| 173 | +fig, axes = plt.subplots(2, 3, figsize=(20, 10)) # Create a 2x3 grid of subplots |
| 174 | + |
| 175 | +# Process images from bild01.jpg to bild05.jpg |
| 176 | +for i in range(1, 6): |
| 177 | + grayscale_image = imread(f'bild0{i}.jpg', as_gray=True) # Read image in grayscale |
| 178 | + grayscale_image = (grayscale_image * 255).astype(np.uint8) # Convert to uint8 |
| 179 | + histo = computeHisto(grayscale_image) # Compute histogram |
| 180 | + |
| 181 | + # Display histogram in subplot |
| 182 | + row, col = divmod(i - 1, 3) # Calculate row and column indices |
| 183 | + axes[row, col].bar(range(256), histo, width=1, color='gray') |
| 184 | + axes[row, col].set_title(f"bild0{i}.jpg") |
| 185 | + axes[row, col].set_xlabel("Intensitätsstufen") |
| 186 | + axes[row, col].set_ylabel("Anzahl der Pixel") |
| 187 | + |
| 188 | +# Hide the unused subplot |
| 189 | +axes[1, 2].axis('off') |
| 190 | + |
| 191 | +plt.tight_layout() |
| 192 | +plt.show() |
| 193 | +``` |
| 194 | + |
| 195 | + |
| 196 | + |
| 197 | +![[Studium/2DCV/Bilder/Uebung01_11_0.png]] |
| 198 | + |
| 199 | + |
| 200 | + |
| 201 | +#### a) Welche Aufnahmefehler sind in 01 und 03 zu erkennen? Woran ist dies im Histogramm erkennbar? |
| 202 | + |
| 203 | +- Bild01: |
| 204 | + Fehler: Bild ist unterbelichtet (zu dunkel). Die Pixelwerte sind stark im dunklen Bereich (nahe 0) konzentriert. Es gibt fast keine hohen Helligkeitswerte. |
| 205 | + |
| 206 | +- Bild03: |
| 207 | + Fehler: Kein echter Fehler – Bild ist gut belichtet. Die Helligkeitswerte sind über den gesamten Bereich (0–255) gleichmäßig verteilt → kein Kontrastverlust, keine Unter-/Überbelichtung. |
| 208 | + |
| 209 | +#### b) Bild01 ist das aufgenommene Bild. Bild02 wurde nachbearbeitet. Die Helligkeit wurde erhöht. Woran ist dies im Histogramm erkennbar? Welche Daten gehen dabei verloren? |
| 210 | + |
| 211 | +Pixel, die in Bild01 bereits nah an 255 lagen, sind beim Helligkeitsschub "übergelaufen" und wurden auf 255 gekappt. |
| 212 | + |
| 213 | +Dadurch gehen Helligkeitsunterschiede in den hellen Bereichen verloren → Details sind abgeschnitten (Clipping). |
| 214 | + |
| 215 | + |
| 216 | +#### c) Bild04 ist das aufgenommene Bild. Bild05 wurde einem Bearbeitungsschritt unterzogen. Was wurde in Bild05 verändert? Woran kann man dies in seinem Histogramm erkennen? |
| 217 | + |
| 218 | +Das Histogramm von Bild05 hat nur wenige schmale Peaks (z. B. bei 0, 128, 255), der Rest ist leer. |
| 219 | + |
| 220 | +Das bedeutet: Es gibt nur noch ganz bestimmte Intensitätswerte → typische Folge von Posterisierung oder Farb-/Graustufenumwandlung mit geringer Bit-Tiefe. |
| 221 | + |
| 222 | + |
| 223 | +```python |
| 224 | +def create_lut_brighten(): |
| 225 | + """ |
| 226 | + Erzeugt eine Lookup-Tabelle, die dunkle Bereiche aufhellt, |
| 227 | + ohne helle Bereiche stark zu verändern. |
| 228 | + → z. B. mit einer Gammakorrektur (gamma < 1) |
| 229 | + """ |
| 230 | + gamma = 0.5 # Geringer als 1 → Aufhellung dunkler Bereiche |
| 231 | + lut = np.array([int(255 * ((i / 255) ** gamma)) for i in range(256)], dtype=np.uint8) |
| 232 | + return lut |
| 233 | +``` |
| 234 | + |
| 235 | + |
| 236 | +```python |
| 237 | +def apply_lut(image, lut): |
| 238 | + """ |
| 239 | + Wendet eine Lookup-Tabelle auf ein Graustufenbild an. |
| 240 | + image: 2D NumPy-Array |
| 241 | + lut: 1D NumPy-Array mit 256 Einträgen |
| 242 | + """ |
| 243 | + height, width = image.shape |
| 244 | + output = np.zeros_like(image) |
| 245 | + |
| 246 | + for y in range(height): |
| 247 | + for x in range(width): |
| 248 | + output[y, x] = lut[image[y, x]] |
| 249 | + |
| 250 | + return output |
| 251 | +``` |
| 252 | + |
| 253 | + |
| 254 | +```python |
| 255 | +# Bild laden (zuvor in Graustufen umgewandelt) |
| 256 | +image = imread("Bild01.jpg", as_gray=True) |
| 257 | +image = (image * 255).astype(np.uint8) |
| 258 | + |
| 259 | +lut = create_lut_brighten() |
| 260 | +brightened = apply_lut(image, lut) |
| 261 | + |
| 262 | +# Plot |
| 263 | +plt.figure(figsize=(15, 4)) |
| 264 | +plt.subplot(1, 3, 1) |
| 265 | +plt.imshow(image, cmap='gray') |
| 266 | +plt.title("Original") |
| 267 | + |
| 268 | +plt.subplot(1, 3, 2) |
| 269 | +plt.imshow(brightened, cmap='gray') |
| 270 | +plt.title("Aufgehellt mit LUT") |
| 271 | + |
| 272 | +plt.subplot(1, 3, 3) |
| 273 | +plt.plot(lut) |
| 274 | +plt.title("LUT-Kurve (Gamma)") |
| 275 | +plt.xlabel("Originalwert") |
| 276 | +plt.ylabel("Neuer Wert") |
| 277 | + |
| 278 | +plt.tight_layout() |
| 279 | +plt.show() |
| 280 | + |
| 281 | +``` |
| 282 | + |
| 283 | + |
| 284 | + |
| 285 | +![[Studium/2DCV/Bilder/Uebung01_15_0.png]] |
| 286 | + |
| 287 | + |
0 commit comments