-
Notifications
You must be signed in to change notification settings - Fork 3
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
8 changed files
with
239 additions
and
93 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
102 changes: 102 additions & 0 deletions
102
android/amg8833/app/src/main/java/jp/araobp/amg8833/analyzer/Image.kt
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 |
---|---|---|
@@ -0,0 +1,102 @@ | ||
package jp.araobp.amg8833.analyzer | ||
|
||
import android.graphics.Color | ||
import android.util.Log | ||
import android.view.SurfaceView | ||
import kotlin.math.roundToInt | ||
|
||
class Image(val surfaceView: SurfaceView) { | ||
|
||
companion object { | ||
val TAG: String = this::class.java.simpleName | ||
} | ||
|
||
enum class Colormap { | ||
Grayscale, | ||
Okinawa, | ||
Heatmap, | ||
Devil, | ||
Rainbow | ||
} | ||
|
||
fun draw( | ||
data: ByteArray, | ||
minMaxNormalization: Boolean, | ||
showTemperature: Boolean, | ||
colorMap: Colormap, | ||
interpolationRepeat: Int = 0 | ||
) { | ||
|
||
val src = data.toUByteArray().reversed().toUByteArray() | ||
|
||
val pixels: UByteArray = when (minMaxNormalization) { | ||
true -> minMaxNormalize(src) | ||
false -> src | ||
} | ||
|
||
val canvas = surfaceView.holder.lockCanvas() | ||
canvas.drawColor(Color.DKGRAY) | ||
|
||
val width = canvas.width.toFloat() | ||
val height = canvas.height.toFloat() | ||
|
||
var tempList: List<String>? = null | ||
|
||
if (showTemperature) { | ||
tempList = tempList(src) | ||
} | ||
|
||
var numRows = 8 | ||
var numCols = 8 | ||
|
||
val finalPixels = if (interpolationRepeat > 0) { | ||
val srcMat = pixels.toMat() | ||
val dstMat = interpolate(srcMat, interpolationRepeat) | ||
numRows = dstMat.rows() | ||
numCols = dstMat.cols() | ||
convertToUByteArray(dstMat) | ||
} else { | ||
pixels | ||
} | ||
|
||
//Log.d(TAG, "numRows: $numRows, numCols: $numCols, numPixels: ${finalPixels.size}") | ||
|
||
val xStep = width / numCols | ||
val yStep = xStep | ||
val yMargin = (height - width) / 2F | ||
val textMargin = xStep / 5F | ||
|
||
for (row in 0 until numRows) { | ||
for (col in 0 until numCols) { | ||
val idx = row * numCols + col | ||
val pixel = finalPixels[idx] | ||
val left = xStep * col | ||
val top = yStep * row + yMargin | ||
val right = xStep * (col + 1) | ||
val bottom = yStep * (row + 1) + yMargin | ||
val brightness = when (colorMap) { | ||
Colormap.Grayscale -> paintGrayscale(pixel) | ||
Colormap.Okinawa -> paintOkinawa(pixel) | ||
Colormap.Heatmap -> paintHeatmap(pixel) | ||
Colormap.Devil -> paintDevil(pixel) | ||
Colormap.Rainbow -> paintRainbow(pixel) | ||
} | ||
canvas.drawRect(left, top, right, bottom, brightness) | ||
|
||
tempList?.let { | ||
if (interpolationRepeat == 0) { | ||
canvas.drawText( | ||
it[idx], | ||
left + textMargin, | ||
top + TEMP_COLOR.textSize + textMargin, | ||
TEMP_COLOR | ||
) | ||
} | ||
|
||
} | ||
} | ||
} | ||
|
||
surfaceView.holder.unlockCanvasAndPost(canvas) | ||
} | ||
} |
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
67 changes: 0 additions & 67 deletions
67
android/amg8833/app/src/main/java/jp/araobp/amg8833/analyzer/RawImage.kt
This file was deleted.
Oops, something went wrong.
50 changes: 41 additions & 9 deletions
50
android/amg8833/app/src/main/java/jp/araobp/amg8833/analyzer/Utils.kt
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,32 +1,64 @@ | ||
package jp.araobp.amg8833.analyzer | ||
|
||
import android.graphics.Color | ||
import org.opencv.core.CvType | ||
import org.opencv.core.Mat | ||
import org.opencv.core.Size | ||
import org.opencv.imgproc.Imgproc | ||
import org.opencv.imgproc.Imgproc.INTER_CUBIC | ||
import org.opencv.imgproc.Imgproc.resize | ||
import kotlin.math.roundToInt | ||
|
||
val MAX_VALUE = UByte.MAX_VALUE.toFloat() | ||
val TEMP_SCALE = 0.25F | ||
|
||
fun tempList(data: UByteArray) = data.map { (it.toFloat() * TEMP_SCALE).roundToInt().toString() }.toList() | ||
|
||
fun minMaxNormalize(data: UByteArray): FloatArray { | ||
fun minMaxNormalize(src: UByteArray): UByteArray { | ||
|
||
val floatArray = FloatArray(data.size) | ||
val dst = UByteArray(src.size) | ||
var min = UByte.MAX_VALUE.toFloat() | ||
var max = UByte.MIN_VALUE.toFloat() | ||
|
||
data.forEachIndexed { idx, byte -> | ||
val d = data[idx].toFloat() | ||
floatArray[idx] = d | ||
src.forEachIndexed { idx, _ -> | ||
val d = src[idx].toFloat() | ||
if (d > max) max = d | ||
if (d < min) min = d | ||
} | ||
|
||
val minmax = max - min | ||
|
||
floatArray.forEachIndexed { idx, float -> | ||
val f = floatArray[idx] | ||
floatArray[idx] = MAX_VALUE * (f - min) / minmax | ||
src.forEachIndexed { idx, _ -> | ||
val f = src[idx].toFloat() | ||
dst[idx] = (MAX_VALUE * (f - min) / minmax).toUInt().toUByte() | ||
} | ||
|
||
return floatArray | ||
return dst | ||
} | ||
|
||
fun UByteArray.toMat(): Mat { | ||
val mat = Mat(8, 8, CvType.CV_8U) | ||
mat.put(0, 0, this.toByteArray()) | ||
return mat | ||
} | ||
|
||
fun convertToUByteArray(mat: Mat): UByteArray { | ||
val dst = ByteArray(mat.width() * mat.height()) | ||
mat.get(0, 0, dst) | ||
return dst.toUByteArray() | ||
} | ||
|
||
/* | ||
* Interpolate image | ||
* | ||
* The reason why I use this function instead of cv::resize() is that | ||
* the original image (8x8) is too small for bicubic interpolation. | ||
* I found that repeating bicubic interpolation works well. | ||
*/ | ||
fun interpolate(src: Mat, repeat: Int): Mat { | ||
val dst = src.clone() | ||
for (i in 1 .. repeat) { | ||
resize(dst, dst, Size(dst.cols()*4.0, dst.rows()*4.0), 0.0, 0.0, INTER_CUBIC); | ||
} | ||
return dst | ||
} |
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
Oops, something went wrong.