Skip to content

Instantly share code, notes, and snippets.

@farmaker47
Created April 17, 2021 05:52
Show Gist options
  • Save farmaker47/b2efad7029c90e28082316778d0730cd to your computer and use it in GitHub Desktop.
Save farmaker47/b2efad7029c90e28082316778d0730cd to your computer and use it in GitHub Desktop.
fun convertArrayToBitmap(
imageArray: Array<Array<Array<FloatArray>>>,
imageWidth: Int,
imageHeight: Int
): Pair<Bitmap, Bitmap> {
// Convert multidimensional array to 1D
val oneDFloatArray = ArrayList<Float>()
for (m in imageArray[0].indices) {
for (x in imageArray[0][0].indices) {
for (y in imageArray[0][0][0].indices) {
oneDFloatArray.add(imageArray[0][0][x][y])
}
}
}
val maxValue: Float = oneDFloatArray.max() ?: 0f
val minValue: Float = oneDFloatArray.min() ?: 0f
val conf = Bitmap.Config.ARGB_8888 // see other conf types
val grayToneImage = Bitmap.createBitmap(imageWidth, imageHeight, conf)
val blackWhiteImage = Bitmap.createBitmap(imageWidth, imageHeight, conf)
// Use manipulation like Colab post processing...... // 255 * (depth - depth_min) / (depth_max - depth_min)
for (x in imageArray[0][0].indices) {
for (y in imageArray[0][0][0].indices) {
// Create black and transparent bitmap based on pixel value above a certain number eg. 150
// make all pixels black in case value of grayscale image is above 150
blackWhiteImage.setPixel(
y,
x,
if ((255 * (imageArray[0][0][x][y] - minValue) / (maxValue - minValue)).toInt() > 150) Color.BLACK else Color.TRANSPARENT
)
// Create grayscale image to show on screen after inference
val color = Color.rgb(
(255 * (imageArray[0][0][x][y] - minValue) / (maxValue - minValue)).toInt(), //((imageArray[0][0][x][y] * 255).toInt()),
(255 * (imageArray[0][0][x][y] - minValue) / (maxValue - minValue)).toInt(),//((imageArray[0][0][x][y] * 255).toInt()),
(255 * (imageArray[0][0][x][y] - minValue) / (maxValue - minValue)).toInt()//(imageArray[0][0][x][y] * 255).toInt()
)
// this y, x is in the correct order!!!
grayToneImage.setPixel(y, x, color)
}
}
return Pair(grayToneImage, blackWhiteImage)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment