Initial commit with project files

This commit is contained in:
2025-06-27 14:34:11 +02:00
commit 7ea3207e63
310 changed files with 9331 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
# Übung 12: Automatische Schwellwertfindung nach Otsu
Sie haben folgendes Bild gegeben und sollen den Kamera-Mann und seine Kamera möglichst gut mit einem binären Schwellwert
segmentieren:
![](../../data/cameraman.png)
Hierzu soll das Verfahren von Otsu verwendet werden.
Quelle: [https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=4310076](https://ieeexplore.ieee.org/stamp/stamp.jsp?tp=&arnumber=4310076)
## a) Otsu
Berechnen Sie für jeden Schwellwert zwischen {0, ..., 255} die Mittelwerte und Varianzen der beiden Gaussverteilungen
<p align="center">
<img src="https://latex.codecogs.com/svg.image?n_1(s)=\sum_{k=0}^sh(k)" title="\sum_1" />
<p>
<p align="center">
<img src="https://latex.codecogs.com/svg.image?n_2(s)=\sum_{k=s+1}^{255}h(k)" title="\sum_1" />
<p>
<p align="center">
<img src="https://latex.codecogs.com/svg.image?\mu_1(s)=\frac{1}{n_1}\sum_{k=0}^sh(k)k" title="\sum_1" />
<p>
<p align="center">
<img src="https://latex.codecogs.com/svg.image?\mu_2(s)=\frac{1}{n_2}\sum_{k=s+1}^{255}h(k)k" title="\sum_1" />
<p>
<p align="center">
<img src="https://latex.codecogs.com/svg.image?\sigma_1(s)=\sqrt{\frac{1}{n_1}\sum_{k=0}^sh(k)(k-\mu_1)^2}" title="\sum_1" />
<p>
<p align="center">
<img src="https://latex.codecogs.com/svg.image?\sigma_2(s)=\sqrt{\frac{1}{n_2}\sum_{k=s+1}^{255}h(k)(k-\mu_2)^2}" title="\sum_1" />
<p>
und maximieren Sie den Quotienten
<p align="center">
<img src="https://latex.codecogs.com/svg.image?Q(s)=\frac{\sigma(s)_{zw}^2}{\sigma(s)_{in}^2}=\frac{n_1(s)(\mu_1(s)-\mu)^2+n_2(s)(\mu_2(s)-\mu)^2}{n_1(s)\sigma_1(s)^2+n_2(s)\sigma_2(s)^2}" title="\sum_1" />
<p>
mit dem Mittelwert des Grauwertbildes
<p align="center">
<img src="https://latex.codecogs.com/svg.image?\mu=\frac{1}{n_1(255)}\sum_{k=0}^{255}h(k)k," title="\sum_1" />
<p>
sodass Sie den optimalen Schwellwert s bekommen.
Bitte führen Sie für die Bearbeitung der Aufgabe das Skript [a.py](a.py) fort.
Die Lösung befindet sich in Datei [l_a.py](l_a.py).

41
5_Bildanalyse/ü12/a.py Normal file
View File

@@ -0,0 +1,41 @@
import numpy as np
import cv2
from matplotlib import pyplot as plt
def n1(hist, s):
...
def n2(hist, s):
...
def mean(hist):
...
def mu1(hist, s, n):
...
def mu2(hist, s, n):
...
def sigma1(hist, s, n, mu):
...
def sigma2(hist, s, n, mu):
...
def Q(hist, s):
...
''' Load data '''
# Load images and template
img = cv2.imread("../../data/cameraman.png", cv2.IMREAD_GRAYSCALE)

77
5_Bildanalyse/ü12/l_a.py Normal file
View File

@@ -0,0 +1,77 @@
import numpy as np
import cv2
from matplotlib import pyplot as plt
def n1(hist, s):
return np.sum(hist[0:s+1])
def n2(hist, s):
return np.sum(hist[s+1:256])
def mean(hist):
k = np.arange(0, 256)
return np.sum((hist * k)) / np.sum(hist)
def mu1(hist, s, n):
k = np.arange(0, 256)
return (1 / n) * np.sum((hist * k)[0:s+1])
def mu2(hist, s, n):
k = np.arange(0, 256)
return (1 / n) * np.sum((hist * k)[s+1:256])
def sigma1(hist, s, n, mu):
k = np.arange(0, 256)
return np.sqrt((1 / n) * np.sum((hist * np.power(k - mu, 2))[0:s+1]))
def sigma2(hist, s, n, mu):
k = np.arange(0, 256)
return np.sqrt((1 / n) * np.sum((hist * np.power(k - mu, 2))[s+1:256]))
def Q(hist, s):
n_1, n_2 = n1(hist, s), n2(hist, s)
if n_1 == 0 or n_2 == 0:
return 0
mu_1, mu_2 = mu1(hist, s, n_1), mu2(hist, s, n_2)
sigma_1, sigma_2 = sigma1(hist, s, n_1, mu_1), sigma2(hist, s, n_2, mu_2)
_mean = mean(hist)
sigma_zw = n_1 * np.power(mu_1 - _mean, 2) + n_2 * np.power(mu_2 - _mean, 2)
sigma_in = n_1 * np.power(sigma_1, 2) + n_2 * np.power(sigma_2, 2)
q = np.power(sigma_zw, 2) / np.power(sigma_in, 2)
return q
''' Load data '''
# Load images and template
img = cv2.imread("../../data/cameraman.png", cv2.IMREAD_GRAYSCALE)
histr = cv2.calcHist([img], [0], None, [256], [0, 256])
histr = histr[:, 0]
plt.plot(histr)
plt.xlim([0, 256])
''' Iterate over all s '''
q_list = list()
for s in range(1, 255):
q_list.append(Q(histr, s))
optimal_s = np.argmax(np.asarray(q_list))
print("Threshold:", optimal_s)
''' Visualize result '''
mask = img >= optimal_s
cv2.imshow("img", img)
cv2.imshow("mask", mask.astype(np.uint8) * 255)
plt.show()
cv2.waitKey(0)