如何衡量目标和android手机摄像头之间的距离 [英] how can we measure distance between object and android phone camera

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

问题描述

我要计算的相机和认可object.For这个我尝试了很多方法,我试图找到对象与相机之间的角度用加速度计,然后用

之间的距离

D = H *晒得

h是从基通常是1.4的高度

和我试图用GET方向的方法来计算角度。请让我知道我做错了。它已经超过2天我一直在努力与这一要求。我们看着相机的各种应用中这是在Android商店,并试图了解相同的功能,但什么也没有收获。

  mSensorManager =(的SensorManager)getSystemService(SENSOR_SERVICE);
            accSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
            magnetSensor = mSensorManager
                    .getDefaultSensor(Sensor.TYPE_MAGNETIC_FIELD);
    @覆盖
    公共无效onAccuracyChanged(传感器传感器,诠释精度){
        // TODO自动生成方法存根

    }

    @覆盖
    公共无效onSensorChanged(SensorEvent事件){
        // TODO自动生成方法存根
        如果(event.sensor.getType()== Sensor.TYPE_ACCELEROMETER)
            重力= event.values​​;
        如果(event.sensor.getType()== Sensor.TYPE_MAGNETIC_FIELD)
            地磁= event.values​​;
        如果(重力= NULL和放大器;!&安培;!地磁= NULL){
            浮动 -  [R [] =新的浮动[9];
            浮动我[] =新的浮动[9];
            布尔成功= SensorManager.getRotationMatrix(R,I,重力,
                    地磁);
            如果(成功){
                / *方向具有方位角,俯仰和横滚* /
                浮方位[] =新的浮动[3];
                //SensorManager.remapCoordinateSystem(R,1,3,取向);
                SensorManager.getOrientation(R,取向);
                AZIMUT = 57.29578F *方向[0];
                距= 57.29578F *取向[1];
                卷= 57.29578F *方向[2];
            }
        }
    }


        captureButton.setOnClickListener(新View.OnClickListener(){
            @覆盖
            公共无效的onClick(视图v){
                //从照相机获得的图像

                双D =(Math.tan(Math.toRadians(Math.abs(沥青)))* sensorHeight);
                Toast.makeText(
                        getApplicationContext(),
                        距离=
                                +将String.valueOf(D)
                                        +M角=
                                        +将String.valueOf(Math.toRadians(Math.abs(沥青)))
                        Toast.LENGTH_LONG).show();


            }
        });



保护无效onResume(){
        super.onResume();
        mSensorManager.registerListener(这一点,accSensor,
                SensorManager.SENSOR_DELAY_NORMAL);
        mSensorManager.registerListener(这一点,magnetSensor,
                SensorManager.SENSOR_DELAY_NORMAL);
    }
 

解决方案

您getRotationMatrix可能是返回假的!你应该值复制到自己的载体,使他们不会被弄混了!使用clone()方法来做到这一点!

 如果(event.sensor.getType()== Sensor.TYPE_ACCELEROMETER)
        比重= event.values​​.clone();
    如果(event.sensor.getType()== Sensor.TYPE_MAGNETIC_FIELD)
        地磁= event.values​​.clone();
 

使用您的code以及这种变化我能得到的方位/俯仰/滚动值,如果没有这种改变的成功标志返回false:

  Log.d(A,方向值:+ AZIMUT +/+沥青+/+卷);
05-21 16:07:55.743:D / A(29429):定向值:77.71578 / 43.352722 / -152.39603
05-21 16:07:55.883:D / A(29429):定向值:175.26134 / 23.031355 / -148.72844
05-21 16:07:56.793:D / A(29429):定向值:-146.3089 / 4.1098075 / -14.46417
 

您应该使用,如果你持有的手机在纵向模式下的螺距值,如果你拿着手机在横向模式下,你应该使用ROLL值。

如果您是拿着手机在一个1.4的高度,那么你将有:

 浮法DIST = Math.abs((浮动)(1.4f * Math.tan(距* Math.PI / 180)));
 

请注意,您应该使用弧度和不度的Math.tan功能。

我这里测试的数值似乎是有效的!

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

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

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);
    }

解决方案

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();

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

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.

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)));

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!

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

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