NDK - 图像阈值

时间:2016-06-21 22:35:13

标签: c++ image-processing android-ndk java-native-interface threshold

我想进行双重hysterisis阈值处理。 我将位图作为参数传递,然后我将其阈值返回到android。 我的原生代码是:

#include <jni.h>
#include <android/log.h>
#include <android/bitmap.h>
#include <stdint.h>
#include <stdlib.h>
#include <math.h>
#include <stdio.h>
#include <inttypes.h>


#define  LOGI(...)  __android_log_print(ANDROID_LOG_INFO,LOG_TAG,__VA_ARGS__)
#define  LOGE(...)  __android_log_print(ANDROID_LOG_ERROR,LOG_TAG,__VA_ARGS__)

typedef struct {

    uint8_t red;
    uint8_t green;
    uint8_t blue;
    uint8_t alpha;
} argb;

JNIEXPORT void JNICALL Java_com_mypakage_MainActivity_Hysterisis
  (JNIEnv *env, jobject obj, jobject bmp){

        AndroidBitmapInfo  infocolor;
        void*              pixelscolor;
        int                ret;
        int             y;
        int             x;
        uint32_t *pixel;


        if ((ret = AndroidBitmap_getInfo(env, bmp, &infocolor)) < 0) {
            return;
        }

        if ((ret = AndroidBitmap_lockPixels(env, bmp, &pixelscolor)) < 0) {
        }


        pixel = (uint32_t *) pixelscolor;

            uint32_t p,ac,bc,cc,dc,ec,fc,gc,hc;
            bool again = true;
            while (again) {
                again=false;
                for (y=1;y<=infocolor.height - 1;y++) {
                    for (x=1;x<infocolor.width -1;x++) {
                        p = (uint32_t) ( (*(pixel + x + (y ) * infocolor.stride)));


                        if (p == 0xFF888888) {
                            ac = (uint32_t) ( (*(pixel + x -1 + (y -1 ) * infocolor.stride)));
                            bc = (uint32_t) ( (*(pixel + x + (y -1 ) * infocolor.stride)));
                            cc = (uint32_t) ( (*(pixel + x +1 + (y -1 ) * infocolor.stride)));
                            dc = (uint32_t) ( (*(pixel + x +1 + (y ) * infocolor.stride)));
                            ec = (uint32_t) ( (*(pixel + x +1 + (y+1 ) * infocolor.stride)));
                            fc = (uint32_t) ( (*(pixel + x + (y +1 ) * infocolor.stride)));
                            fc = (uint32_t) ( (*(pixel + x -1 + (y +1 ) * infocolor.stride)));
                            fc = (uint32_t) ( (*(pixel + x -1 + (y ) * infocolor.stride)));
                            if (ac ==0xFFFFFFFF || bc ==0xFFFFFFFF || cc ==0xFFFFFFFF || dc ==0xFFFFFFFF
                                    || ec ==0xFFFFFFFF || fc ==0xFFFFFFFF || gc ==0xFFFFFFFF || hc ==0xFFFFFFFF) {
                                ( (*(pixel + x + (y ) * infocolor.stride)))= 0xFFFFFFFF;
                                again=true;
                            }
                        }

                    }

                }
            }

      for (y=1;y<=infocolor.height - 1;y++) {
            for (x=1;x<infocolor.width -1;x++) {
                p = (uint32_t) ( (*(pixel + x + (y ) * infocolor.stride)));
                if (p == 0xFF888888) {
                    ( (*(pixel + x + (y ) * infocolor.stride)))= 0xFF000000;
                }

            }
        }


        AndroidBitmap_unlockPixels(env, bmp);
}

我的问题是该应用程序因以下错误而崩溃:

A/libc(29191): Fatal signal 11 (SIGSEGV) at ...

任何帮助?

1 个答案:

答案 0 :(得分:0)

x和y的循环终止条件不同。对于y,您应该在< infocolor.height - 1时进行迭代,以便最后的y值等于infocolor.height - 2,这意味着当您向y添加1时,您将解决infocolor.height - 1,这是最大值超出范围之前的y值。

将其更改为:

for (y=1;y<infocolor.height - 1;y++)

此外,步幅是每行的字节数,而不是像素数,因此您需要除以4:

int pixelsPerRow = infocolor.stride / 4;

ac = (uint32_t) ( (*(pixel + x -1 + (y -1 ) * pixelsPerRow)));