From aea65430e038540acd3effd2ca6b86f5f5de3063 Mon Sep 17 00:00:00 2001 From: frasu Date: Mon, 15 Dec 2025 13:31:08 +0000 Subject: [PATCH] src/dopt_sensor_anomalies/detection.py aktualisiert changed value computation in measured_length --- src/dopt_sensor_anomalies/detection.py | 37 +++++++++++++++++++------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/src/dopt_sensor_anomalies/detection.py b/src/dopt_sensor_anomalies/detection.py index 6f7a8b2..038c8c9 100644 --- a/src/dopt_sensor_anomalies/detection.py +++ b/src/dopt_sensor_anomalies/detection.py @@ -97,15 +97,16 @@ def measure_length( box = cast(npt.NDArray[np.float32], perspective.order_points(box)) (tl, tr, br, bl) = box - (tltrX, tltrY) = midpoint(tl, tr) - (blbrX, blbrY) = midpoint(bl, br) - (tlblX, tlblY) = midpoint(tl, bl) - (trbrX, trbrY) = midpoint(tr, br) - dA = dist.euclidean((tltrX, tltrY), (blbrX, blbrY)) - dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY)) + widthA = np.linalg.norm(br - bl) + widthB = np.linalg.norm(tr - tl) + max_width = int(max(widthA, widthB)) - if dA < 100 or dB < 100: + heightA = np.linalg.norm(tr - br) + heightB = np.linalg.norm(tl - bl) + max_height = int(max(heightA, heightB)) + + if max_width < 100 or max_height < 100: continue is_duplicate = any( @@ -117,14 +118,30 @@ def measure_length( accepted_boxes.append(rbox) filtered_cnts.append(c) - dimA = dA / pixels_per_metric_Y - dimB = dB / pixels_per_metric_X + dimA = max_width / pixels_per_metric_Y + dimB = max_height / pixels_per_metric_X + + offset = 20 + + dst = np.array([ + [offset, offset], + [max_width - 1 + offset, offset], + [max_width - 1 + offset, max_height - 1 + offset], + [offset, max_height - 1 + offset] + ], dtype="float32") + + M = cv2.getPerspectiveTransform(box, dst) + warped = cv2.warpPerspective(orig, M, (max_width + 2 * offset, max_height + 2 * offset)) + + gray_warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY) + _, binary_warped = cv2.threshold(gray_warped, 80, 255, cv2.THRESH_BINARY) + pixel_count = np.sum(binary_warped == 0) data_csv.extend( [ f"{dimB:.3f}".replace(".", ","), f"{dimA:.3f}".replace(".", ","), - f"{dimA * dimB:.1f}".replace(".", ","), + f"{pixel_count / pixels_per_metric_X / pixels_per_metric_Y:.1f}".replace(".", ","), ] )