-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOpenCV-Konturen.txt
88 lines (69 loc) · 3.28 KB
/
OpenCV-Konturen.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import cv2
import numpy as np
import matplotlib.pyplot as plt
import os
import json
# Bildpfad
image_path = "daco4.bmp"
# Überprüfen, ob das Bild existiert
if not os.path.exists(image_path):
print("Fehler: Bild wurde nicht gefunden.")
else:
# Bild laden
image = cv2.imread(image_path)
# Überprüfen, ob das Bild erfolgreich geladen wurde
if image is None:
print("Fehler: Das Bild konnte nicht geladen werden. Bitte ueberpruefen Sie den Pfad.")
else:
# Bild in Graustufen konvertieren
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
# Bildkontrast erhöhen
alpha = 1.8 # Kontrastfaktor leicht erhöht
adjusted = cv2.convertScaleAbs(gray, alpha=alpha)
# Bild glätten (um Rauschen zu reduzieren, aber nicht zu stark)
blurred = cv2.GaussianBlur(adjusted, (3, 3), 0)
# Canny-Kantendetektion anwenden (angepasste Schwellenwerte)
edges = cv2.Canny(blurred, threshold1=30, threshold2=100)
# Bild nach Kanten anzeigen
plt.imshow(edges, cmap='gray')
plt.title("Canny-Kanten")
plt.show()
# Konturen finden, keine Erosion anwenden, um feine Details zu behalten
contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# Filtern nach Konturlänge statt Fläche (z.B. minimale Länge)
filtered_contours = [contour for contour in contours if cv2.arcLength(contour, True) > 50]
# Anzahl der gefundenen Konturen überprüfen
print(f"Anzahl der gefilterten Konturen: {len(filtered_contours)}")
# Zeichne gefundene Konturen
contour_image = image.copy()
cv2.drawContours(contour_image, filtered_contours, -1, (0, 255, 0), 1)
# Konturen anzeigen
plt.imshow(cv2.cvtColor(contour_image, cv2.COLOR_BGR2RGB))
plt.title("Gefilterte Konturen")
plt.show()
# Polygon-Approximation für jede Kontur und die Koordinaten für WebGL speichern
approx_polygons = []
polygons_data = [] # Diese Liste wird die Polygonpunkte für den JSON-Export speichern
for contour in filtered_contours:
epsilon = 0.02 * cv2.arcLength(contour, True) # Genauigkeit der Approximation
approx = cv2.approxPolyDP(contour, epsilon, True)
approx_polygons.append(approx)
# Speichere die Punkte in einer Liste für JSON
polygon_points = []
for point in approx:
x, y = point[0]
polygon_points.append([int(x), int(y)]) # Speichern der Koordinaten als Ganzzahlen
polygons_data.append(polygon_points)
# Zeichne die approximierten Polygone
approx_image = image.copy()
for polygon in approx_polygons:
cv2.drawContours(approx_image, [polygon], -1, (255, 0, 0), 2)
# Approximierte Polygone anzeigen
plt.imshow(cv2.cvtColor(approx_image, cv2.COLOR_BGR2RGB))
plt.title("Approximierte Polygone")
plt.show()
# JSON-Daten speichern
json_file_path = "polygons_data.json"
with open(json_file_path, "w") as json_file:
json.dump(polygons_data, json_file)
print(f"Polygon-Koordinaten wurden in '{json_file_path}' gespeichert.")