Spaces:
Runtime error
Runtime error
Create diagram_extraction.py
Browse files- diagram_extraction.py +11 -0
diagram_extraction.py
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
|
| 3 |
+
def extract_diagram(image):
|
| 4 |
+
"""Extracts the diagram within the largest contour (border)."""
|
| 5 |
+
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
|
| 6 |
+
edges = cv2.Canny(gray, 50, 150)
|
| 7 |
+
contours, _ = cv2.findContours(edges, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 8 |
+
|
| 9 |
+
largest_contour = max(contours, key=cv2.contourArea)
|
| 10 |
+
x, y, w, h = cv2.boundingRect(largest_contour)
|
| 11 |
+
return image[y:y+h, x:x+w]
|