Unity / C#来自图像的对象框

时间:2017-04-13 19:39:26

标签: c# image-processing unity3d augmented-reality vuforia

目标:我想了解通过Vuforia的AR相机(网络摄像头/手机相机)看到的真实物体的宽度。

想法:使用Vuforia的AR相机拍照,然后从该图像中找到一个物体(例如一个杯子)的边界框;然后得到边界框的宽度(宽度信息我将缩放3d对象)。

示例:

image example1

image example2

我知道资产商店中有OpenCV用于统一,但我正在寻找一种免费方法。 如果您对如何实现提议的想法有任何想法,或者有任何关于如何实现目标的新想法,那么我们将非常感激。

1 个答案:

答案 0 :(得分:0)

(修改)

经过一番搜索后,我发现了这种优秀的方法here

public static Rect ObjectBounds(GameObject go)
    {
        Vector3 cen = go.GetComponent<Renderer>().bounds.center;
        Vector3 ext = go.GetComponent<Renderer>().bounds.extents;
        Vector2[] extentPoints = new Vector2[8]
        {
         HandleUtility.WorldToGUIPoint(new Vector3(cen.x-ext.x, cen.y-ext.y, cen.z-ext.z)),
         HandleUtility.WorldToGUIPoint(new Vector3(cen.x+ext.x, cen.y-ext.y, cen.z-ext.z)),
         HandleUtility.WorldToGUIPoint(new Vector3(cen.x-ext.x, cen.y-ext.y, cen.z+ext.z)),
         HandleUtility.WorldToGUIPoint(new Vector3(cen.x+ext.x, cen.y-ext.y, cen.z+ext.z)),
         HandleUtility.WorldToGUIPoint(new Vector3(cen.x-ext.x, cen.y+ext.y, cen.z-ext.z)),
         HandleUtility.WorldToGUIPoint(new Vector3(cen.x+ext.x, cen.y+ext.y, cen.z-ext.z)),
         HandleUtility.WorldToGUIPoint(new Vector3(cen.x-ext.x, cen.y+ext.y, cen.z+ext.z)),
         HandleUtility.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);
    }

将方法调用到rect并在屏幕上绘制的示例:

void OnGUI () //EditorGUI.DrawRect must be in this function name
{
    //create a rect using the method and the GameObject this script is attached to (gameObject)
    Rect boundsRect = new Rect(GUIRectWithObject(gameObject));
    //draw a blue rect on the box of the rect (will hide the object)
    EditorGUI.DrawRect(boundsRect, Color.blue);
}

为了获得rect写入的宽度:

float boundswidth = boundsRect.width;