src/dopt_sensor_anomalies/detection.py aktualisiert

changed value computation in measured_length
This commit is contained in:
frasu 2025-12-15 13:31:08 +00:00
parent 34e05476ba
commit aea65430e0

View File

@ -97,15 +97,16 @@ def measure_length(
box = cast(npt.NDArray[np.float32], perspective.order_points(box)) box = cast(npt.NDArray[np.float32], perspective.order_points(box))
(tl, tr, br, bl) = 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)) widthA = np.linalg.norm(br - bl)
dB = dist.euclidean((tlblX, tlblY), (trbrX, trbrY)) 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 continue
is_duplicate = any( is_duplicate = any(
@ -117,14 +118,30 @@ def measure_length(
accepted_boxes.append(rbox) accepted_boxes.append(rbox)
filtered_cnts.append(c) filtered_cnts.append(c)
dimA = dA / pixels_per_metric_Y dimA = max_width / pixels_per_metric_Y
dimB = dB / pixels_per_metric_X 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( data_csv.extend(
[ [
f"{dimB:.3f}".replace(".", ","), f"{dimB:.3f}".replace(".", ","),
f"{dimA:.3f}".replace(".", ","), f"{dimA:.3f}".replace(".", ","),
f"{dimA * dimB:.1f}".replace(".", ","), f"{pixel_count / pixels_per_metric_X / pixels_per_metric_Y:.1f}".replace(".", ","),
] ]
) )