Arif Turgut
Arif Turgut

Reputation: 13

Android motion detection

I want to detect motion with Android sensors. For example I hold only bottom of phone and move top of phone to up. I think I need sampling algorithms. I can write a simple application to record data of sensors . For comparing real time data and recorded data ,Is there any libary ? I have suspicious about performace problems if I would make it. Is there a different path for detetion movements ?

Upvotes: 1

Views: 5576

Answers (2)

ksu
ksu

Reputation: 902

http://code.google.com/p/android-motion-detection/ is a good example.

I modified the isDifferent method in RgbMotionDetection class to detect the motion in the center part (25%) of the camera view.

protected static boolean isDifferent(int[] first, int width, int height) {
            if (first==null) throw new NullPointerException();

            if (mPrevious==null) return false;
            if (first.length != mPrevious.length) return true;
            if (mPreviousWidth != width || mPreviousHeight != height) return true;

            int totDifferentPixels = 0;
            int size = height * width;

            int startHeight = height / 4;
            int endHeight = 3 * (height / 4);
            int startWidth = width / 4;
            int endWidth = 3 * (width / 4); 
            int offSet = width / 4;

            Log.d("params", "start height " + startHeight + "end height " + endHeight + "start width " + startWidth + "end width " + endWidth);

            Boolean offSetApplied;

            for (int i = startHeight, ij=0; i < endHeight; i++) {
                {
                    offSetApplied = false;
                    for (int j = startWidth; j < endWidth; j++, ij++) {
                            if (!offSetApplied){
                                offSetApplied = true;
                                ij = startHeight * width + offSet;
                            }

                            int pix = (0xff & ((int)first[ij]));
                            int otherPix = (0xff & ((int)mPrevious[ij]));

                            //Catch any pixels that are out of range
                            if (pix < 0) pix = 0;
                            if (pix > 255) pix = 255;
                            if (otherPix < 0) otherPix = 0;
                            if (otherPix > 255) otherPix = 255;

                            if (Math.abs(pix - otherPix) >= mPixelThreshold) {
                                    totDifferentPixels++;
                                    //Paint different pixel red
                                    //first[ij] = Color.RED;
                            }
                    }
                }
        }



            if (totDifferentPixels <= 0) totDifferentPixels = 1;
            //boolean different = totDifferentPixels > mThreshold;

            int percent = 100/(size/totDifferentPixels);

            //float percent = (float) totDifferentPixels / (float) size;

            boolean different = percent > SENSITIVITY;

            String output = "Number of different pixels: " + totDifferentPixels + "> " + percent + "%";
            if (different) {
                    Log.e(TAG, output);
            } else {
                    Log.d(TAG, output);
            }

            return different;
    }

Upvotes: 0

Related Questions