2

私はConvolution Matrixイメージエンボスを作るために私のAndroidアプリに使用しています。私はそれのクラスを次のように定義しました:

public class ConvolutionMatrix {
public static final int SIZE = 3;

public double[][] Matrix;
public double Factor = 1;
public double Offset = 1;

public ConvolutionMatrix(int size) {
    Matrix = new double[size][size];
}

public void setAll(double value) {
    for (int x = 0; x < SIZE; ++x) {
        for (int y = 0; y < SIZE; ++y) {
            Matrix[x][y] = value;
        }
    }
}

public void applyConfig(double[][] config) {
    for (int x = 0; x < SIZE; ++x) {
        for (int y = 0; y < SIZE; ++y) {
            Matrix[x][y] = config[x][y];
        }
    }
}

public static Bitmap computeConvolution3x3(Bitmap src,
        ConvolutionMatrix matrix) {
    int width = src.getWidth();
    int height = src.getHeight();
    Bitmap result = Bitmap.createBitmap(width, height, src.getConfig());

    int A, R, G, B;
    int sumR, sumG, sumB;
    int[][] pixels = new int[SIZE][SIZE];

    for (int y = 0; y < height - 2; ++y) {
        for (int x = 0; x < width - 2; ++x) {

            // get pixel matrix
            for (int i = 0; i < SIZE; ++i) {
                for (int j = 0; j < SIZE; ++j) {
                    pixels[i][j] = src.getPixel(x + i, y + j);
                }
            }

            // get alpha of center pixel
            A = Color.alpha(pixels[1][1]);

            // init color sum
            sumR = sumG = sumB = 0;

            // get sum of RGB on matrix
            for (int i = 0; i < SIZE; ++i) {
                for (int j = 0; j < SIZE; ++j) {
                    sumR += (Color.red(pixels[i][j]) * matrix.Matrix[i][j]);
                    sumG += (Color.green(pixels[i][j]) * matrix.Matrix[i][j]);
                    sumB += (Color.blue(pixels[i][j]) * matrix.Matrix[i][j]);
                }
            }

            // get final Red
            R = (int) (sumR / matrix.Factor + matrix.Offset);
            if (R < 0) {
                R = 0;
            } else if (R > 255) {
                R = 255;
            }

            // get final Green
            G = (int) (sumG / matrix.Factor + matrix.Offset);
            if (G < 0) {
                G = 0;
            } else if (G > 255) {
                G = 255;
            }

            // get final Blue
            B = (int) (sumB / matrix.Factor + matrix.Offset);
            if (B < 0) {
                B = 0;
            } else if (B > 255) {
                B = 255;
            }

            // apply new pixel
            result.setPixel(x + 1, y + 1, Color.argb(A, R, G, B));
        }
    }

    // final image
    return result;
}

}

適切な結果が得られていますが、結果の計算に時間がかかりすぎます。計算を高速化し、効率的に機能させる方法はありますか?

4

2 に答える 2

3

スローダウンの核心は次のとおりです。

    // apply new pixel
    result.setPixel(x + 1, y + 1, Color.argb(A, R, G, B));

これは、各ピクセルをピクセルごとに設定するための各反復での妥当な量の作業であり、ビットマップクラスでは無料ではありません。getPixels()ルーチンを呼び出して、そこにある生のピクセルをいじってから、完了したら1回だけ元に戻すことをお勧めします。

エンボスをハードコーディングすることもできます(ほとんどの場合、大量のデータを取得し、そのカーネルでゼロを掛けると、気になる3つのピクセルのように簡単にチートして取得できます。

private static int hardEmboss(int[] pixels, int stride, int index, int[][] matrix, int parts) {
        //ignoring the matrix
        int p1 = pixels[index];
        int p2 = pixels[index + stride + 1];
        int p3 = pixels[index + stride + stride + 2];
        int r = 2 * ((p1 >> 16) & 0xFF) - ((p2 >> 16) & 0xFF) - ((p3 >> 16) & 0xFF);
        int g = 2 * ((p1 >> 8) & 0xFF) - ((p2 >> 8) & 0xFF) - ((p3 >> 8) & 0xFF);
        int b = 2 * ((p1) & 0xFF) - ((p2) & 0xFF) - ((p3) & 0xFF);
        return 0xFF000000 | ((crimp(r) << 16) | (crimp(g) << 8) | (crimp(b)));
    }

エンボスカーネルが次のようになっていると仮定します。

   int[][] matrix = new int[][]{
            {2, 0, 0},
            {0, -1, 0},
            {0, 0, -1}
    };

また、ほとんどの人には知られていないが、標準の畳み込みアルゴリズムには重大な欠陥があり、結果のピクセルを中央に戻すのにエラーがあります。左上隅に戻すと、スキャンライン操作で左から右、上から下に移動する同じメモリフットプリント内のすべてのデータを簡単に処理できます。

    public static int crimp(int v) { return (v > 255)?255:((v < 0)?0:v); }
    public static void applyEmboss(int[] pixels, int stride) { 
        //stride should be equal to width here, and pixels.length == bitmap.height * bitmap.width;
        int pos;
        pos = 0;
        try {
            while (true) {
                int p1 = pixels[pos];
                int p2 = pixels[pos + stride + 1];
                int p3 = pixels[pos + stride + stride + 2];
                int r = 2 * ((p1 >> 16) & 0xFF) - ((p2 >> 16) & 0xFF) - ((p3 >> 16) & 0xFF);
                int g = 2 * ((p1 >> 8) & 0xFF) - ((p2 >> 8) & 0xFF) - ((p3 >> 8) & 0xFF);
                int b = 2 * ((p1) & 0xFF) - ((p2) & 0xFF) - ((p3) & 0xFF);
                pixels[pos++] = 0xFF000000 | ((crimp(r) << 16) | (crimp(g) << 8) | (crimp(b)));
            }
        }
        catch (ArrayIndexOutOfBoundsException e) { }
    }

欠点は、ピクセルが1ピクセルずつ上下にシフトしているように見えることです。ただし、別のスキャンラインフィルを後方に実行すると、それらを後方にシフトできます。そして、ここのすべてのゴミは、右側と下側に2行になります(これらの場所をチェックするために速度を落とさなかったため、この一部はエンボス加工されたナンセンスで埋められます)。これは、ピクセルを読み取ったときにそれを切り取りたい場合は、高さと幅を2減らし、ストライドを元の幅のサイズのままにしておくことを意味します。すべての適切なデータがトップビットに含まれるため、オフセットをいじる必要はまったくありません。

また、renderscriptを使用するだけです。

于 2015-02-18T13:46:46.670 に答える
1

ご覧ください:コンボリューションデモこれは、 JavaC++で行われた畳み込み実装を比較するアプリです。言うまでもなく、C++バリアントは10倍以上高速に実行されます。

したがって、速度が必要な場合は、NDKまたはシェーダーを介して実装してください。

于 2013-01-13T16:42:25.473 に答える