Implement anti-ghosting inpainting for original background blur during photo capture as described in 25_FIX_CAMERA.md

This commit is contained in:
2026-07-06 19:52:12 +07:00
parent c95f12cedc
commit a7a539b97f
3 changed files with 300 additions and 1 deletions
@@ -1829,7 +1829,20 @@ class MainActivity : AppCompatActivity() {
solidBg
}
} else {
capturedImage
// Triệt tiêu Ghost Shadow (Bóng ma rế viền) bằng giãn cơ thể ẩn (Inpainting Dilate)
val cleanBg = android.graphics.Bitmap.createBitmap(w, h, android.graphics.Bitmap.Config.ARGB_8888)
for (y in 0 until h) {
for (x in 0 until w) {
val idx = y * w + x
val alpha = alphaMask[idx]
if (alpha > 0.01f) {
cleanBg.setPixel(x, y, getNearestBackgroundPixel(capturedImage, alphaMask, x, y, w, h))
} else {
cleanBg.setPixel(x, y, capturedImage.getPixel(x, y))
}
}
}
cleanBg
}
val bgPixels = IntArray(w * h)
@@ -1896,6 +1909,37 @@ class MainActivity : AppCompatActivity() {
return capturedImage
}
private fun getNearestBackgroundPixel(
src: android.graphics.Bitmap,
alphaMask: FloatArray,
startX: Int,
startY: Int,
w: Int,
h: Int
): Int {
val maxDist = 30
for (r in 1..maxDist) {
// Check 4 directions: left, right, up, down
var px = startX - r
if (px >= 0) {
if (alphaMask[startY * w + px] <= 0.01f) return src.getPixel(px, startY)
}
px = startX + r
if (px < w) {
if (alphaMask[startY * w + px] <= 0.01f) return src.getPixel(px, startY)
}
var py = startY - r
if (py >= 0) {
if (alphaMask[py * w + startX] <= 0.01f) return src.getPixel(startX, py)
}
py = startY + r
if (py < h) {
if (alphaMask[py * w + startX] <= 0.01f) return src.getPixel(startX, py)
}
}
return src.getPixel(0, 0)
}
private fun blurAlphaMaskBox(arr: FloatArray, w: Int, h: Int, radius: Int) {
if (radius <= 0) return
val temp = FloatArray(w * h)