Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Voting for the Unity Awards are OPEN! We’re looking to celebrate creators across games, industry, film, and many more categories. Cast your vote now for all categories
    Dismiss Notice
  3. Dismiss Notice

Anyway to get projected 2d bounding box coordinates from 3D space?

Discussion in 'Editor & General Support' started by krotovd, Jun 21, 2018.

  1. krotovd

    krotovd

    Joined:
    Sep 23, 2014
    Posts:
    35
    So I'm currently workign on a POC to see if we can use Unity to generate synthetic labeled data for our DL model to then train on. I've got an overall 3D environment with products on a shelf, but now I need to create labeled data output. I rotate the products and the cam around the shelf and take snapshots. I now want to label the products and find where the bounding boxes are for those products ("x", "y", "w", "h") for each image taken and store it in a JSON file.

    I figured I would first see if anyone has seen anything already created out there before I go trying to figure out how to do this myself. If anyone has any info/advice, I would love to hear it. Thanks!
     
  2. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    You can get the axis-aligned bounding box (AABB) from a collider or a renderer attached to the object. I'm guessing you have a MeshRenderer, so you can access meshRenderer.Bounds to get your bounding box.

    I recommend you make sure you understand how an AABB works before you decide this will work for you because the box can resize when your object rotates. If this does not work for you, then you will probably want to loop through the verts in your object and find the Vector3 min and max in local space, then convert them to world space.

    Once you've got your AABB or Vector3 min/max, use the camera that can see your 3D object to call camera.WorldToScreenPoint() to convert those 3D Vectors into 2D screen coordinates.
     
  3. krotovd

    krotovd

    Joined:
    Sep 23, 2014
    Posts:
    35
    Thanks so much Gambit-MSplitz! This was the perfect solution and gave me what I was looking for. :)
     
  4. jsubrama

    jsubrama

    Joined:
    Jan 28, 2019
    Posts:
    1
    I have similar issue, but i didn't understand the recommendation.
    I am moving camera to various positions and capturing screenshot, I want to know the bounding box of textObject in the scene for each and every camera position. The scale of the text changes each time, the bounds value for MeshRenderer, I get from Unity and converts to WorldToScreenPoint() is not giving useful information. The value of bounds is more than actual image size.

    how to solve this issue for scaling problem?
     
  5. MSplitz-PsychoK

    MSplitz-PsychoK

    Joined:
    May 16, 2015
    Posts:
    1,278
    That is because it is an AABB
    AABB's cannot be rotated, so when you rotate your image, the AABB actually becomes larger so that it can fit your whole rotated object inside without rotating itself. Here is an example image: the red box is the AABB, and even though the text is the same size in both places, the AABB is larger for the rotated text.

    aabb.png

    I'm afraid I don't know enough about what your needs are for the bounds that you're after. I recommend posting a new forum thread and briefly describing what you plan to do with the bounds or points you are trying to get.
     
    Walter_Hulsebos and jsubrama like this.