Skip to content

Commit 501f938

Browse files
committed
Add integral Bradley in FilterScript
1 parent 5d88269 commit 501f938

File tree

8 files changed

+90
-4
lines changed

8 files changed

+90
-4
lines changed

app/src/main/java/pl/pk/binarycamera2/BinaryCameraActivity.kt

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class BinaryCameraActivity : AppCompatActivity() {
2222
originalPreviewButton,
2323
simpleKotlinButton, simpleKtNativeButton, simpleCppButton, simpleRsButton,
2424
bradleyKotlinButton, bradleyKotlinNativeButton, bradleyCppButton, bradleyFsButton, bradleyRsButton,
25-
bradleyIntKotlinButton, bradleyIntKotlinNativeButton, bradleyIntCppButton, bradleyIntRsButton
25+
bradleyIntKotlinButton, bradleyIntKotlinNativeButton, bradleyIntCppButton, bradleyIntRsButton, bradleyIntFsButton
2626
)
2727
}
2828

@@ -67,6 +67,7 @@ class BinaryCameraActivity : AppCompatActivity() {
6767
bradleyIntRsButton.setOnClickListener { viewModel.switchMode(ProcessingMode.BRADLEY_INT_RS) }
6868

6969
bradleyFsButton.setOnClickListener { viewModel.switchMode(ProcessingMode.BRADLEY_FS) }
70+
bradleyIntFsButton.setOnClickListener { viewModel.switchMode(ProcessingMode.BRADLEY_INT_FS) }
7071

7172
viewModel.processingTime
7273
.observeOn(AndroidSchedulers.mainThread())
@@ -97,6 +98,7 @@ class BinaryCameraActivity : AppCompatActivity() {
9798
ProcessingMode.BRADLEY_RS -> bradleyRsButton.disable()
9899
ProcessingMode.BRADLEY_INT_RS -> bradleyIntRsButton.disable()
99100
ProcessingMode.BRADLEY_FS -> bradleyFsButton.disable()
101+
ProcessingMode.BRADLEY_INT_FS -> bradleyIntFsButton.disable()
100102
ProcessingMode.SIMPLE_KT -> simpleKotlinButton.disable()
101103
ProcessingMode.SIMPLE_KT_NATIVE -> simpleKtNativeButton.disable()
102104
ProcessingMode.SIMPLE_CPP -> simpleCppButton.disable()

app/src/main/java/pl/pk/binarycamera2/ProcessorProxy.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,8 @@ class ProcessorProxy(rs: RenderScript) : Processor, Benchmarkable, KoinComponent
4747
ProcessingMode.BRADLEY_RS to pl.pk.binarizer.rs.BradleyBinarizationRS(rs, previewSize),
4848
ProcessingMode.BRADLEY_INT_RS to pl.pk.binarizer.rs.IntegralBradleyBinarizationRS(rs, previewSize),
4949

50-
ProcessingMode.BRADLEY_FS to pl.pk.binarizer.rs.BradleyBinarizationFS(rs, previewSize)
50+
ProcessingMode.BRADLEY_FS to pl.pk.binarizer.rs.BradleyBinarizationFS(rs, previewSize),
51+
ProcessingMode.BRADLEY_INT_FS to pl.pk.binarizer.rs.IntegralBradleyBinarizationFS(rs, previewSize)
5152
)
5253

5354
init {

app/src/main/res/layout/main.xml

+6
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,12 @@
145145
android:layout_height="wrap_content"
146146
android:text="@string/bradley_fs" />
147147

148+
<Button
149+
android:id="@+id/bradleyIntFsButton"
150+
android:layout_width="150dp"
151+
android:layout_height="wrap_content"
152+
android:text="@string/bradley_int_fs" />
153+
148154
<Button
149155
android:id="@+id/bradleyRsButton"
150156
android:layout_width="150dp"

app/src/main/res/values/strings.xml

+1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
<string name="bradley_rs">Bradley RS</string>
1515
<string name="bradley_int_rs">Bradley Int RS</string>
1616
<string name="bradley_fs">Bradley FS</string>
17+
<string name="bradley_int_fs">Bradley Int FS</string>
1718
<string name="preview_fps">Preview FPS: %d</string>
1819
<string name="processing_fps">Processing FPS: %d</string>
1920
</resources>

binarizer/src/main/java/pl/pk/binarizer/ProcessingMode.kt

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,5 @@ enum class ProcessingMode {
44
ORIGINAL,
55
SIMPLE_KT, SIMPLE_KT_NATIVE, SIMPLE_CPP, SIMPLE_RS,
66
BRADLEY_KT, BRADLEY_KT_NATIVE, BRADLEY_CPP, BRADLEY_RS, BRADLEY_FS,
7-
BRADLEY_INT_KT, BRADLEY_INT_KT_NATIVE, BRADLEY_INT_CPP, BRADLEY_INT_RS
7+
BRADLEY_INT_KT, BRADLEY_INT_KT_NATIVE, BRADLEY_INT_CPP, BRADLEY_INT_RS, BRADLEY_INT_FS
88
}

binarizer/src/main/java/pl/pk/binarizer/rs/BradleyBinarizationRS.kt

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import android.renderscript.RenderScript
55
import android.util.Size
66
import pl.pk.binarizer.Processor
77

8-
class BradleyBinarizationRS(rs: RenderScript, private val dimensions: Size) : Processor {
8+
class BradleyBinarizationRS(private val rs: RenderScript, private val dimensions: Size) : Processor {
99

1010
private val kernel = ScriptC_BradleyBinarizationRS(rs)
1111

@@ -14,6 +14,7 @@ class BradleyBinarizationRS(rs: RenderScript, private val dimensions: Size) : Pr
1414
kernel._width = dimensions.width.toLong()
1515
kernel._height = dimensions.height.toLong()
1616
kernel.forEach_process(output)
17+
rs.finish()
1718
}
1819

1920
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package pl.pk.binarizer.rs
2+
3+
import android.renderscript.Allocation
4+
import android.renderscript.Element
5+
import android.renderscript.RenderScript
6+
import android.util.Size
7+
import pl.pk.binarizer.Processor
8+
9+
class IntegralBradleyBinarizationFS(private val rs: RenderScript, private val dimensions: Size) : Processor {
10+
11+
private val kernel = ScriptC_IntegralBradleyBinarizationFS(rs)
12+
13+
override fun process(input: Allocation, output: Allocation) {
14+
val monochromeBytesSize = (input.bytesSize / 1.5).toInt()
15+
val originalBytes = ByteArray(monochromeBytesSize)
16+
val integral = IntArray(monochromeBytesSize)
17+
18+
input.copy1DRangeTo(0, monochromeBytesSize, originalBytes)
19+
calculateIntegral(originalBytes, integral)
20+
21+
val integralAllocation = Allocation.createSized(rs, Element.I32(rs), monochromeBytesSize, Allocation.USAGE_SCRIPT)
22+
integralAllocation.copy1DRangeFrom(0, monochromeBytesSize, integral)
23+
24+
kernel._currentFrame = input
25+
kernel._integral = integralAllocation
26+
kernel._width = dimensions.width.toLong()
27+
kernel._height = dimensions.height.toLong()
28+
kernel.forEach_process(output)
29+
rs.finish()
30+
}
31+
32+
private fun calculateIntegral(originalBytes: ByteArray, integral: IntArray) {
33+
for (row in 0 until dimensions.height) {
34+
var sum = 0
35+
for (column in 0 until dimensions.width) {
36+
sum += originalBytes[column + dimensions.width * row].toInt() and 0xFF
37+
if (row == 0) {
38+
integral[column + dimensions.width * row] = sum
39+
} else {
40+
integral[column + dimensions.width * row] = integral[column + dimensions.width * (row - 1)] + sum
41+
}
42+
}
43+
}
44+
}
45+
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#pragma version(1)
2+
#pragma rs java_package_name(pl.pk.binarizer.rs)
3+
4+
rs_allocation currentFrame;
5+
rs_allocation integral;
6+
7+
uint32_t width = 1280;
8+
uint32_t height = 720;
9+
const uint32_t radius = 7;
10+
11+
static int getIntegralAverage(uint32_t index) {
12+
int one = rsGetElementAt_int(integral, index + radius * width + radius);
13+
int two = rsGetElementAt_int(integral, index - radius * width - radius);
14+
int three = rsGetElementAt_int(integral, index + radius * width - radius);
15+
int four = rsGetElementAt_int(integral, index - radius * width + radius);
16+
return (one + two - three - four) / 225;
17+
}
18+
19+
static int bradleyIntegralThreshold(uint32_t x, uint32_t y) {
20+
if (x < radius || x >= width-radius || y < radius || y >=height-radius) return 127;
21+
int average = getIntegralAverage(y*width + x);
22+
return average * 78 / 100;
23+
}
24+
25+
uchar4 __attribute__((kernel)) process(uint32_t x, uint32_t y) {
26+
uchar current = rsGetElementAtYuv_uchar_Y(currentFrame, x, y);
27+
uchar average = bradleyIntegralThreshold(x, y);
28+
return rsYuvToRGBA_uchar4(current > average ? 255 : 0, 128, 128);
29+
}

0 commit comments

Comments
 (0)