2016-06-21 19 views
1

Ich möchte eine Doppelhysterisschwelle durchführen. Ich übergebe die Bitmap als Parameter, und dann schwelle ich es, um es zurück zu android zurückzugeben. mein nativen Code ist:NDK - Bildschwellenwert

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

Mein Problem ist, dass der App stürzt mit dem folgenden Fehler:

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

Jede Hilfe?

Antwort

0

Ihre Schleifenbeendigungsbedingungen sind für x und y unterschiedlich. Für y sollten Sie iterieren, während < infocolor.height - 1, so dass der letzte y-Wert gleich infocolor.height - 2 ist, was bedeutet, dass Sie, wenn Sie 1 zu y hinzufügen, infocolor.height - 1 adressieren, was der maximale y-Wert ist, bevor Sie außerhalb des Bereichs gehen.

Ändern Sie es an:

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

Auch der Schritt ist die Anzahl der Bytes pro Zeile, nicht die Anzahl der Pixel, so dass Sie durch 4 teilen müssen:

int pixelsPerRow = infocolor.stride/4; 

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

etc

+0

änderte es, das gleiche Problem – yanisk

+0

Ich habe meine Antwort bearbeitet, die Schrittberechnungen müssen auch ändern – samgak

+0

die App nicht mehr abstürzen. Aber der Algorithmus tut nicht, was beabsichtigt ist. Ich habe immer noch die grauen Pixel. Ich habe ein LOG in das 'if (p == 0xFF888888)' eingefügt, es wird nicht gedruckt. – yanisk