-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
90 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,60 @@ | ||
#include <jni.h> | ||
|
||
extern "C" JNIEXPORT void JNICALL | ||
Java_pl_pk_binarizer_cpp_SimpleBinarization_binarize(JNIEnv *env, jobject thiz, jbyteArray input, jint size) { | ||
Java_pl_pk_binarizer_cpp_SimpleBinarization_binarize(JNIEnv *env, jobject thiz, jbyteArray input, jbyteArray output, jint size) { | ||
jboolean *isCopy = new jboolean; | ||
jbyte *bytes = env->GetByteArrayElements(input, isCopy); | ||
jbyte *originalBytes = env->GetByteArrayElements(input, isCopy); | ||
jbyte *processedBytes = env->GetByteArrayElements(output, nullptr); | ||
|
||
#pragma omp parallel for num_threads(8) schedule(static) shared(bytes, size) default(none) | ||
#pragma omp parallel for num_threads(8) schedule(static) shared(originalBytes, processedBytes, size) default(none) | ||
for (int i = 0; i < size; ++i) { | ||
if (bytes[i] < 0) { | ||
bytes[i] = -1; | ||
if (originalBytes[i] < 0) { | ||
processedBytes[i] = -1; | ||
} else { | ||
bytes[i] = 0; | ||
processedBytes[i] = 0; | ||
} | ||
} | ||
|
||
delete isCopy; | ||
env->ReleaseByteArrayElements(input, bytes, 0); | ||
env->ReleaseByteArrayElements(input, originalBytes, 0); | ||
env->ReleaseByteArrayElements(output, processedBytes, 0); | ||
} | ||
|
||
int bradleyThreshold(jbyte *data, int index, int cols, int rows, int neighbourRadius, int neighbourPixels) { | ||
int bradleyThreshold(jbyte *data, int index, int cols, int rows) { | ||
long sum = 0; | ||
|
||
int start = index - (cols * neighbourRadius) - neighbourRadius; | ||
int maxOffset = neighbourRadius * 2 * cols + neighbourRadius * 2; | ||
int start = index - (cols * 7) - 7; | ||
int maxOffset = 7 * 2 * cols + 7 * 2; | ||
|
||
if((start + maxOffset) > (cols * rows) || start < 0) return 127; | ||
if ((start + maxOffset) > (cols * rows) || start < 0) return 127; | ||
|
||
for(int y = 0; y < neighbourRadius * 2; y++) { | ||
for(int x = 0; x < neighbourRadius * 2; x ++) { | ||
for (int y = 0; y < 7 * 2; y++) { | ||
for (int x = 0; x < 7 * 2; x++) { | ||
sum += data[start + y * cols + x] & 0xFF; | ||
} | ||
} | ||
|
||
int average = sum / neighbourPixels; | ||
int average = sum / 225; | ||
return average * 78 / 100; | ||
} | ||
|
||
extern "C" JNIEXPORT void JNICALL | ||
Java_pl_pk_binarizer_cpp_BradleyBinarization_binarize(JNIEnv *env, jobject thiz, jbyteArray input, jint size) { | ||
Java_pl_pk_binarizer_cpp_BradleyBinarization_binarize(JNIEnv *env, jobject thiz, jbyteArray input, jbyteArray output, jint size) { | ||
jboolean *isCopy = new jboolean; | ||
jbyte *bytes = env->GetByteArrayElements(input, isCopy); | ||
jbyte *originalBytes = env->GetByteArrayElements(input, isCopy); | ||
jbyte *processedBytes = env->GetByteArrayElements(output, nullptr); | ||
|
||
#pragma omp parallel for num_threads(8) schedule(static) shared(bytes, size) default(none) | ||
#pragma omp parallel for num_threads(8) schedule(static) shared(originalBytes, processedBytes, size) default(none) | ||
for (int i = 0; i < size; ++i) { | ||
int th = bradleyThreshold(bytes, i, 1280, 720, 7, 225); | ||
if ((bytes[i] & 0xFF) > th) { | ||
bytes[i] = -1; | ||
int th = bradleyThreshold(originalBytes, i, 1280, 720); | ||
if ((originalBytes[i] & 0xFF) > th) { | ||
processedBytes[i] = -1; | ||
} else { | ||
bytes[i] = 0; | ||
processedBytes[i] = 0; | ||
} | ||
} | ||
|
||
delete isCopy; | ||
env->ReleaseByteArrayElements(input, bytes, 0); | ||
env->ReleaseByteArrayElements(input, originalBytes, 0); | ||
env->ReleaseByteArrayElements(output, processedBytes, 0); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters