NDK - 图像阈值

NDK - Image thresholding

我想执行双滞后阈值处理。 我将位图作为参数传递,然后将其阈值设为 return,再返回至 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 ...

有什么帮助吗?

x 和 y 的循环终止条件不同。对于y,应该while迭代< infocolor.height - 1,这样最后的y值就等于infocolor.height - 2,也就是说y加1的时候会寻址infocolor.height - 1,也就是最大值超出范围之前的 y 值。

改为:

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

另外,stride是每行的字节数,不是像素数,所以需要除以4:

int pixelsPerRow = infocolor.stride / 4;

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

等等