3dbox and 2d box bounding_box

 转载from https://stackoverflow.com/questions/51905936/unity-function-to-access-the-2d-box-immediately-from-the-3d-pipeline

 

 

D bounding box

  1. Get given GameObject 3D bounding box's center and size
  2. Compute 8 corners
  3. Transform positions to GUI space (screen space)

Function GUI3dRectWithObject will return the 3D bounding box of given GameObject on screen.

2D bounding box

  1. Iterate through every vertex in a given GameObject
  2. Transform every vertex's position to world space, and transform to GUI space (screen space)
  3. Find 4 corner value: x1, x2, y1, y2

Function GUI2dRectWithObject will return the 2D bounding box of given GameObject on screen.

public static Rect GUI3dRectWithObject(GameObject go)
{

    Vector3 cen = go.GetComponent<Renderer>().bounds.center;
    Vector3 ext = go.GetComponent<Renderer>().bounds.extents;
    Vector2[] extentPoints = new Vector2[8]
    {
            WorldToGUIPoint(new Vector3(cen.x-ext.x, cen.y-ext.y, cen.z-ext.z)),
            WorldToGUIPoint(new Vector3(cen.x+ext.x, cen.y-ext.y, cen.z-ext.z)),
            WorldToGUIPoint(new Vector3(cen.x-ext.x, cen.y-ext.y, cen.z+ext.z)),
            WorldToGUIPoint(new Vector3(cen.x+ext.x, cen.y-ext.y, cen.z+ext.z)),
            WorldToGUIPoint(new Vector3(cen.x-ext.x, cen.y+ext.y, cen.z-ext.z)),
            WorldToGUIPoint(new Vector3(cen.x+ext.x, cen.y+ext.y, cen.z-ext.z)),
            WorldToGUIPoint(new Vector3(cen.x-ext.x, cen.y+ext.y, cen.z+ext.z)),
            WorldToGUIPoint(new Vector3(cen.x+ext.x, cen.y+ext.y, cen.z+ext.z))
    };
    Vector2 min = extentPoints[0];
    Vector2 max = extentPoints[0];
    foreach (Vector2 v in extentPoints)
    {
        min = Vector2.Min(min, v);
        max = Vector2.Max(max, v);
    }
    return new Rect(min.x, min.y, max.x - min.x, max.y - min.y);
}

public static Rect GUI2dRectWithObject(GameObject go)
{
    Vector3[] vertices = go.GetComponent<MeshFilter>().mesh.vertices;

    float x1 = float.MaxValue, y1 = float.MaxValue, x2 = 0.0f, y2 = 0.0f;

    foreach (Vector3 vert in vertices)
    {
        Vector2 tmp = WorldToGUIPoint(go.transform.TransformPoint(vert));

        if (tmp.x < x1) x1 = tmp.x;
        if (tmp.x > x2) x2 = tmp.x;
        if (tmp.y < y1) y1 = tmp.y;
        if (tmp.y > y2) y2 = tmp.y;
    }

    Rect bbox = new Rect(x1, y1, x2 - x1, y2 - y1);
    Debug.Log(bbox);
    return bbox;
}

public static Vector2 WorldToGUIPoint(Vector3 world)
{
    Vector2 screenPoint = Camera.main.WorldToScreenPoint(world);
    screenPoint.y = (float)Screen.height - screenPoint.y;
    return screenPoint;
}

 

 

for i in np.arange(len(radar_lines)): radar_line=radar_lines[i] pcd_line=pcd_lines[i] pcd_obj = Object3d(pcd_line) center = np.array(pcd_obj.t) center[2] = center[2]+pcd_obj.h # ry=obj.ry heading_angle = -pcd_obj.ry - np.pi / 2 R = rotz((heading_angle)) # only boundingbox range = (pcd_obj.l, pcd_obj.w, pcd_obj.h) # all vertical range = (pcd_obj.l, pcd_obj.w, 10) # print(center,obj.ry,range) bbx = o3d.geometry.OrientedBoundingBox(center, R, range) cropped_cloud = pcd.crop(bbx) # if set colors colors = [[0, 255, 0] for i in np.arange(len(cropped_cloud.points))] # cropped_cloud.colors = o3d.utility.Vector3dVector(colors) o3d.visualization.draw_geometries([cropped_cloud, bbx]) print(pcd_obj.h) radar_obj = Object2d(radar_line) center = [radar_obj.box2d[0], radar_obj.box2d[1]] w = radar_obj.box2d[2] h = radar_obj.box2d[3] angle = radar_obj.angle # rect = cv2.minAreaRect(cnt) box = cv2.boxPoints((center, (w, h), angle)) print(box) box = np.int0(box) cv2.drawContours(im, [box], 0, (0, 0, 255), 2) mask = np.zeros_like(im) # 使用旋转框的角点绘制多边形掩膜 cv2.drawContours(mask, [box], 0, (255, 255, 255), -1) # 使用掩膜提取旋转框内的像素 masked_image = cv2.bitwise_and(im, mask) cv2.imshow("2d bbx", masked_image) cv2.waitKey(0) cv2.destroyAllWindows() 这里的mask里面都是1,以外的都是0,所以mask加起来就是2dbox里radar image的像素个数。masked_image里,mask以外的都是0,mask内的都是radar的值,所以masked_image里面的都加起来就是2dbox 里radar image的反射强度值。这两个一除就能算radar里有车object的区域里每个像素的平均反射强度。根据上述截取的部分代码和信息,添加代码,算出区域内的平均反射强度并输出。
05-26
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值