Skip to content

Instantly share code, notes, and snippets.

@echoeyecodes
Created April 11, 2022 12:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save echoeyecodes/92e2102bf28c8db14cc111f94f2b547a to your computer and use it in GitHub Desktop.
Save echoeyecodes/92e2102bf28c8db14cc111f94f2b547a to your computer and use it in GitHub Desktop.
private fun alphaBlend(imageSrc:Mat, blurredImage: Mat, blurredMask: Mat): Mat{
val total = imageSrc.width() * imageSrc.height()
val channels = imageSrc.channels()
val size = total * channels
val array = FloatArray(size)
val array1 = FloatArray(size)
val array2 = FloatArray(size)
val array3 = FloatArray(size)
imageSrc.convertTo(imageSrc, CV_32F)
blurredImage.convertTo(blurredImage, CV_32F)
blurredMask.convertTo(blurredMask, CV_32F)
val destination = Mat(imageSrc.size(), imageSrc.type())
imageSrc.get(0,0, array)
blurredImage.get(0,0, array1)
blurredMask.get(0,0, array2)
for (index in 0 until size){
val pixel = array[index]
val blurredPixel = array1[index]
val blurVal = (array2[index]) / 255.0f
val newValue = ((blurVal * blurredPixel) + ((1.0f - blurVal) * pixel))
array3[index] = newValue
}
destination.put(0,0, array3)
destination.convertTo(destination, CV_8UC3)
return destination
}
private suspend fun blurImage(bitmap: Bitmap):Bitmap {
return withContext(Dispatchers.IO) {
val imageSrc = Mat()
Utils.bitmapToMat(bitmap, imageSrc)
val innerMask = Mat.zeros(imageSrc.size(), imageSrc.type())
//thickness set to -1 for inner fill
Imgproc.circle(
innerMask,
Point(imageSrc.width() / 2.0, imageSrc.height() / 1.5),
600,
Scalar.all(255.0),
-1
)
val blurredImage = Mat(imageSrc.size(), imageSrc.type())
Imgproc.blur(imageSrc, blurredImage, Size(64.0, 64.0))
val blurredMask = Mat(innerMask.size(), innerMask.type())
Imgproc.blur(innerMask, blurredMask, Size(64.0, 64.0))
val merge = alphaBlend(imageSrc, blurredImage, blurredMask)
val copy = Bitmap.createBitmap(merge.width(), merge.height(), Bitmap.Config.ARGB_8888)
Utils.matToBitmap(merge, copy)
copy
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment