From 3f114dd7923e94fe9e528d043082d2adc13b04f9 Mon Sep 17 00:00:00 2001 From: HellaJanssen <80407622+HellaJanssen@users.noreply.github.com> Date: Sat, 21 Jun 2025 19:09:47 +0200 Subject: [PATCH] Added bounding box aroung the hedgehog for the IR video --- Pipfile | 1 + Pipfile.lock | 9 +++++++- hedgehogbox.py | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 hedgehogbox.py diff --git a/Pipfile b/Pipfile index 822056a..7cc5836 100644 --- a/Pipfile +++ b/Pipfile @@ -7,6 +7,7 @@ name = "pypi" pyserial = "*" matplotlib = "*" opencv-python = "*" +imutils = "*" [dev-packages] diff --git a/Pipfile.lock b/Pipfile.lock index 60bf98d..a18b0e6 100644 --- a/Pipfile.lock +++ b/Pipfile.lock @@ -1,7 +1,7 @@ { "_meta": { "hash": { - "sha256": "cfeb134953c2360663a51e5238f6c573c2a9ff637cee621f45aedadd9b5b432d" + "sha256": "b4cc5fe5d9d7af541c033d4f692a774ea0d60013b0d2594b1cdbb648b09c8cce" }, "pipfile-spec": 6, "requires": { @@ -136,6 +136,13 @@ "markers": "python_version >= '3.9'", "version": "==4.58.1" }, + "imutils": { + "hashes": [ + "sha256:03827a9fca8b5c540305c0844a62591cf35a0caec199cb0f2f0a4a0fb15d8f24" + ], + "index": "pypi", + "version": "==0.5.4" + }, "kiwisolver": { "hashes": [ "sha256:01c3d31902c7db5fb6182832713d3b4122ad9317c2c5877d0539227d96bb2e50", diff --git a/hedgehogbox.py b/hedgehogbox.py new file mode 100644 index 0000000..98084f2 --- /dev/null +++ b/hedgehogbox.py @@ -0,0 +1,60 @@ +from argparse import ArgumentParser +from pathlib import Path + +import cv2 +import imutils + + +def main(): + parser = ArgumentParser() + parser.add_argument("--output", type=Path, required=False, default="output.avi") + parser.add_argument("video_file", type=Path) + args = parser.parse_args() + + cap = cv2.VideoCapture(args.video_file) + output = cv2.VideoWriter(args.output, cv2.VideoWriter_fourcc(*'MPEG'), 30, (720, 1792)) + + static_frame = None + + while True: + ret, frame = cap.read() + if ret: + cropped_frame = frame[55:720, 0:540] + grey_frame = cv2.cvtColor(cropped_frame, cv2.COLOR_BGR2GRAY) + + if static_frame is None: + static_frame = grey_frame + continue + + + _, thresh = cv2.threshold(grey_frame, 120, 255, cv2.THRESH_BINARY) + dilated_image = cv2.dilate(thresh, None, iterations=2) + + cnts = cv2.findContours(dilated_image.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) + cnts = imutils.grab_contours(cnts) + + for c in cnts: + if cv2.contourArea(c) < 700: + continue + + # get the bounding box coordinates + (x, y, w, h) = cv2.boundingRect(c) + cv2.rectangle(cropped_frame, (x, y), (x+w, y+h), (0, 255, 0), 2) + + output.write(frame) + cv2.imshow("output", frame) + + # exit if 's' key is pressed + if cv2.waitKey(1) & 0xFF == ord('s'): + break + else: + print("Could not read frame!") + break + + cv2.destroyAllWindows() + output.release() + cap.release() + + +if __name__ == "__main__": + main() \ No newline at end of file