我们如何测量物体和安卓手机摄像头之间的距离 [英] how can we measure distance between object and android phone camera

查看:46
本文介绍了我们如何测量物体和安卓手机摄像头之间的距离的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算相机和识别物体之间的距离.为此我尝试了很多方法,我尝试使用加速度计找到物体和相机之间的角度,然后使用

I want to calculate the distance between the camera and the recognized object.For this I tried a lot of methods, I tried to find the angle between the object and the camera using accelerometer and then use

d = h * tan a

d = h * tan a

h 是离底面的高度,一般为 1.4

h is height of from from the base generally which is 1.4

我尝试使用获取方向方法计算角度.请让我知道我哪里做错了.超过 2 天我一直在为这个要求而苦苦挣扎.我们研究了 Android 商店中提供的各种相机应用程序,并试图了解它们的功能,但没有任何成果.

and i tried to calculate the angle by using get orientation method. Kindly let me know where am I doing wrong. Its been more than 2 days I have been struggling with this requirement. We have looked into various Camera applications which are available on Android Store and have tried to understand the functionality of the same but nothing has been fruitful.

 mSensorManager = (SensorManager) getSystemService(SENSOR_SERVICE);
            accSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
            magnetSensor = mSensorManager
                    .getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
    @Override
    public void onAccuracyChanged(Sensor sensor, int accuracy) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onSensorChanged(SensorEvent event) {
        // TODO Auto-generated method stub
        if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
            gravity = event.values;
        if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
            geoMagnetic = event.values;
        if (gravity != null && geoMagnetic != null) {
            float R[] = new float[9];
            float I[] = new float[9];
            boolean success = SensorManager.getRotationMatrix(R, I, gravity,
                    geoMagnetic);
            if (success) {
                /* Orientation has azimuth, pitch and roll */
                float orientation[] = new float[3];
                //SensorManager.remapCoordinateSystem(R, 1, 3, orientation);
                SensorManager.getOrientation(R, orientation);
                azimut = 57.29578F * orientation[0];
                pitch = 57.29578F * orientation[1];
                roll = 57.29578F * orientation[2];
            }
        }
    }


        captureButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // get an image from the camera

                double d = (Math.tan(Math.toRadians(Math.abs(pitch))) * sensorHeight);
                Toast.makeText(
                        getApplicationContext(),
                        "Distance = "
                                + String.valueOf(d)
                                        + "m  Angle = "
                                        + String.valueOf(Math.toRadians(Math.abs(pitch))),
                        Toast.LENGTH_LONG).show();


            }
        });



protected void onResume() {
        super.onResume();
        mSensorManager.registerListener(this, accSensor,
                SensorManager.SENSOR_DELAY_NORMAL);
        mSensorManager.registerListener(this, magnetSensor,
                SensorManager.SENSOR_DELAY_NORMAL);
    }

推荐答案

您的 getRotationMatrix 可能返回 false!您应该将值复制到您自己的向量中,以免混淆!使用 clone() 方法来做到这一点!

Your getRotationMatrix is probably returning false! You should copy the values to your own vectors so they don't get mixed up! Use the clone() method to do so!

    if (event.sensor.getType() == Sensor.TYPE_ACCELEROMETER)
        gravity = event.values.clone();
    if (event.sensor.getType() == Sensor.TYPE_MAGNETIC_FIELD)
        geoMagnetic = event.values.clone();

使用您的代码加上此更改,我能够获得方位角/俯仰/滚转值,如果没有此更改,成功标志将返回 false:

Using your code plus this change I was able to get the azimuth/pitch/roll values, without this change the success flag returns false:

Log.d("a", "orientation values: " + azimut + " / " + pitch + " / " + roll);
05-21 16:07:55.743: D/a(29429): orientation values: 77.71578 / 43.352722 / -152.39603
05-21 16:07:55.883: D/a(29429): orientation values: 175.26134 / 23.031355 / -148.72844
05-21 16:07:56.793: D/a(29429): orientation values: -146.3089 / 4.1098075 / -14.46417

如果您以纵向模式拿手机,则应使用 PITCH 值,如果以横向模式拿手机,则应使用 ROLL 值.

You should use the PITCH value if you are holding the phone in portrait mode, if you are holding the phone in landscape mode you should use the ROLL value.

如果您将手机保持在 1.4 的高度,那么您将:

If you are holding the phone at a 1.4 height then you will have:

float dist = Math.abs((float) (1.4f * Math.tan(pitch * Math.PI / 180)));

请注意,您应该在 Math.tan 函数上使用 RADIANS 而不是 DEGREES.

Please note that you should use RADIANS and not DEGREES on the Math.tan function.

我在这里测试过,值似乎有效!

I tested here and the values seem to be valid!

这篇关于我们如何测量物体和安卓手机摄像头之间的距离的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆