java - 当我将 Java 代码转换为 C++ 代码时失败结果

标签 java android c++ android-ndk convolution

我将卷积类从 Java 转换为 C++,但结果是错误的。 我使用卷积对内核应用图像过滤器:

{ -1 ,  0, -1 },
{  0 ,  4,  0 },
{ -1 ,  0, -1 }

Java 中的原始代码:

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

和转换后的c++代码: 在 C++ 中,我使用 int* 而不是包含图像数据的位图

int* ConvolutionMatrix::procImage() {
int sumR = 0;
int sumG = 0;
int sumB = 0;
int **_pixels = new int*[SIZE];
for (int i = 0; i < SIZE; ++i) {
    _pixels[i] = new int[SIZE];
}
for (int y = 0; y < this->height - 2; ++y) {
    for (int x = 0; x < this->width - 2; ++x) {

        // get pixel matrix
        for (int i = 0; i < SIZE; ++i) {
            for (int j = 0; j < SIZE; ++j) {
                int index = (y + j) * width + (x + i);
                _pixels[i][j] = this->pixels[index];
            }
        }
        // init color sum
        sumR = sumG = sumB = 0;
        for (int i = 0; i < SIZE; ++i) {
            for (int j = 0; j < SIZE; ++j) {
                Color color1(_pixels[i][j]);
                sumR += ( color1.R() * this->Matrix[i][j] );
                sumG += ( color1.G() * this->Matrix[i][j] );
                sumB += ( color1.B() * this->Matrix[i][j] );
            }
        }
        // get final Red
        int R = (int) (sumR / Factor + Offset);
        if (R < 0) {
            R = 0;
        } else if (R > 255) {
            R = 255;
        }
        // get final Green
        int G = (int) (sumG / Factor + Offset);
        if (G < 0) {
            G = 0;
        } else if (G > 255) {
            G = 255;
        }

        // get final Blue
        int B = (int) (sumB / Factor + Offset);
        if (B < 0) {
            B = 0;
        } else if (B > 255) {
            B = 255;
        }
        int index = ( y + 1 ) * width + ( x + 1 );
        //__android_log_print(ANDROID_LOG_VERBOSE,"Android Filter", "The Index is %d", index);
        this->pixels[index] = RGB2Color(R, G, B);
    }
}

return this->pixels;

但结果是: Java 结果: enter image description here

C++ 结果:

enter image description here

我错在哪里了?

编辑:

我想我发现了错误

1)

  // get pixel matrix
        for (int i = 0; i < SIZE; ++i) {
            for (int j = 0; j < SIZE; ++j) {
                int index = (x + i) * width + (y + j);
                _pixels[i][j] = this->pixels[index];
            }
        }

2)

int index = ( x + 1 ) * width + ( y + 1 );
            //__android_log_print(ANDROID_LOG_VERBOSE,"Android Filter", "The Index is %d", index);
this->pixels[index] = RGB2Color(R, G, B);

但现在出现错误:

Fatal signal 11 (SIGSEGV) at 0xff975e1f (code=1)

最佳答案

好吧,一个问题是您在提取像素矩阵时混淆了索引:

    // get pixel matrix
    for (int i = 0; i < SIZE; ++i) {
        for (int j = 0; j < SIZE; ++j) {
            int index = (y + j) * width + (x + i);
            _pixels[i][j] = this->pixels[index];
        }
    }

您正在使用 i 作为 _pixels 数组的行索引和 j 作为列索引,但是在计算 1D 时index 您正在使用 j 作为行索引偏移量,使用 i 作为列索引偏移量。

但这可能不是唯一的问题 -- RGB2Color 函数是什么样的?

关于java - 当我将 Java 代码转换为 C++ 代码时失败结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30005171/

相关文章:

java - 如何通过 LDAP SSL 启用连接池?

Java 小程序不会在 Safari 中执行

android - Android Studio是否会自动尝试更新库?

c++ - Windows.h 或 StdAfx.h,Petzold 书中的第一个窗口示例 "Programming Windows"

c++ - 如何在 C++ GRPC 客户端中创建到一个特定地址的多个连接

java - dom4j.Node 迭代元素映射

java - 不同用例的不同 CascadeType 值

android - "Request Desktop Site"与 Xamarin Android 中的 Intent

java - 寻找 android Facebook SDK 示例

c++ - 如何使用dll?