Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
695 views
in Technique[技术] by (71.8m points)

unity3d - AR camera distance measurement

I have a question about AR(Augmented Reality).

I want to know how to show the distance information(like centermeter...) between AR camera and target object. (Using Smartphone)

Can I do that in Unity ? Should I use AR Foundation? and with ARcore? How to write code?

I tried finding some relative code(below), but it seems just like Printing information between object and object, nothing about "AR camera"...

var other : Transform;
if (other) {
    var dist = Vector3.Distance(other.position, transform.position);
    print ("Distance to other: " + dist);
}
 

Thank again!

question from:https://stackoverflow.com/questions/65623402/ar-camera-distance-measurement

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

I'm working on AR depth image as well and the basic idea is:

  1. Acquire an image using API, normally it's in format Depth16;
  2. Split the image into shortbuffers, as Depth16 means each pixel is 16 bits;
  3. Get the distance value, which is stored in the lower 13 bits of each shortbuffer, you can do this by doing (shortbuffer & 0x1ff), then you can have the distance for each pixel, normally it's in millimeters.

By doing this through all the pixels, you can create a depth image and store it as jpg or other formats, here's the sample code of using AR Engine to get the distance:

try (Image depthImage = arFrame.acquireDepthImage()) {
        int imwidth = depthImage.getWidth();
        int imheight = depthImage.getHeight();
        Image.Plane plane = depthImage.getPlanes()[0];
        ShortBuffer shortDepthBuffer = plane.getBuffer().asShortBuffer();
        File sdCardFile = Environment.getExternalStorageDirectory();
        Log.i(TAG, "The storage path is " + sdCardFile);
        File file = new File(sdCardFile, "RawdepthImage.jpg");

        Bitmap disBitmap = Bitmap.createBitmap(imwidth, imheight, Bitmap.Config.RGB_565);
        for (int i = 0; i < imheight; i++) {
            for (int j = 0; j < imwidth; j++) {
                int index = (i * imwidth + j) ;
                shortDepthBuffer.position(index);
                short depthSample = shortDepthBuffer.get();
                short depthRange = (short) (depthSample & 0x1FFF);
                //If you only want the distance value, here it is
                byte value = (byte) depthRange;
          byte value = (byte) depthRange ;
                disBitmap.setPixel(j, i, Color.rgb(value, value, value));
            }
        }
        //I rotate the image for a better view
        Matrix matrix = new Matrix();
        matrix.setRotate(90);
        Bitmap rotatedBitmap = Bitmap.createBitmap(disBitmap, 0, 0, imwidth, imheight, matrix, true);

        try {
            FileOutputStream out = new FileOutputStream(file);
            rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 90, out);
            out.flush();
            out.close();
            MainActivity.num++;
        } catch (Exception e) {
            e.printStackTrace();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...