Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Crop detection box #25

Open
Pzhenhua opened this issue Jun 3, 2024 · 2 comments
Open

Crop detection box #25

Pzhenhua opened this issue Jun 3, 2024 · 2 comments

Comments

@Pzhenhua
Copy link

Pzhenhua commented Jun 3, 2024

How to crop out images of objects in the detection box? The object has been correctly recognized, but cropping according to the object coordinates is incorrect. Thank you for your guidance!
IMG_0031

@pderrenger
Copy link
Member

@Pzhenhua hello!

It sounds like you're on the right track with using object coordinates for cropping, but if the results aren't as expected, it might be due to how the coordinates are being handled. Here’s a quick guide to ensure accurate cropping:

  1. Ensure Correct Coordinates: Make sure the coordinates (x1, y1, x2, y2) from the detection box are being used correctly. x1 and y1 should be the top-left corner, and x2 and y2 the bottom-right corner of the bounding box.

  2. Coordinate Conversion: If your image has been resized or padded during processing, you'll need to scale or shift these coordinates back to match the original image dimensions.

  3. Cropping Code: Use the coordinates directly with your image array like this:

    cropped_image = original_image[int(y1):int(y2), int(x1):int(x2)]

If you're still facing issues, it might help to check the exact values of the coordinates and ensure they fall within the image dimensions. If you need further assistance, feel free to share the code snippet you're using for cropping, and we can look into it together!

@john-rocky
Copy link
Contributor

@Pzhenhua
Thank you for your question.
The coordinates of the detection results of the Vision Framework's VNCoreMLRequest are normalized to 0 to 1, so before cropping the image based on the detection results, we need to normalize the coordinates of the detection box to the original image size.
You can use Vision methods for normalization.

let denormalizedBox = VNImageRectForNormalizedRect(observation.box, Int(imageWidth), Int(imageHeight))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants