Android运动检测|如何不在捕获的图像上绘制红色

时间:2017-08-03 09:31:37

标签: java android image-processing android-camera rgb

我尝试通过比较基于此(https://github.com/phishman3579/android-motion-detection)应用的RGB值来制作动作检测应用。一切都运行正常,它会在检测到运动时捕获图像。但它在拍摄的图像上涂上了红色。 我想摆脱红色,使图像看起来很正常。

现在的样子: enter image description here

还有其他更好的方法来进行动作检测吗?

RgbMotionDetection.java

GET https://fabrikam-fiber-inc.visualstudio.com/DefaultCollectio‌​n/_apis/test/suites?‌​testCaseId=341&api-v‌​ersion=2.0-preview

ImageProcessing.java

public class RgbMotionDetection implements IMotionDetection {

// private static final String TAG = "RgbMotionDetection";

// Specific settings
private static final int mPixelThreshold = 100; // Difference in pixel (RGB)
private static final int mThreshold = 10000; // Number of different pixels
                                             // (RGB)

private static int[] mPrevious = null;
private static int mPreviousWidth = 0;
private static int mPreviousHeight = 0;

/**
 * {@inheritDoc}
 */
@Override
public int[] getPrevious() {
    return ((mPrevious != null) ? mPrevious.clone() : null);
}

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;
    for (int i = 0, ij = 0; i < height; i++) {
        for (int j = 0; j < width; j++, ij++) {
            int pix = (0xff & (first[ij]));
            int otherPix = (0xff & (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 size = height * width; int percent =
     * 100/(size/totDifferentPixels); String output =
     * "Number of different pixels: " + totDifferentPixels + "> " + percent
     * + "%"; if (different) { Log.e(TAG, output); } else { Log.d(TAG,
     * output); }
     */
    return different;
}

/**
 * Detect motion comparing RGB pixel values. {@inheritDoc}
 */
@Override
public boolean detect(int[] rgb, int width, int height) {
    if (rgb == null) throw new NullPointerException();

    int[] original = rgb.clone();

    // Create the "mPrevious" picture, the one that will be used to check
    // the next frame against.
    if (mPrevious == null) {
        mPrevious = original;
        mPreviousWidth = width;
        mPreviousHeight = height;
        // Log.i(TAG, "Creating background image");
        return false;
    }

    // long bDetection = System.currentTimeMillis();
    boolean motionDetected = isDifferent(rgb, width, height);
    // long aDetection = System.currentTimeMillis();
    // Log.d(TAG, "Detection "+(aDetection-bDetection));

    // Replace the current image with the previous.
    mPrevious = original;
    mPreviousWidth = width;
    mPreviousHeight = height;

    return motionDetected;
}
}

0 个答案:

没有答案