I have trouble implementing Sobele filter for edge detection.
First of all, I make SOBEL_VERTICAL and SOBEL_HORIZONTAL convolution and then calculate color of pixel using this formula: G = sqrt(Gx * Gx + Gy * Gy)
Code:
val log = KotlinLogging.logger { }
val width = fastImage.width
val height = fastImage.height
override fun filter(): FastImage {
val convolution = Convolution(fastImage)
val obsSobelHorizontal = Observable.fromCallable { convolution.convolve(Convolution.SOBEL_HORIZONTAL) }
val obsSobelVertical = Observable.fromCallable { convolution.convolve(Convolution.SOBEL_VERTICAL) }
var fastImageSobel: FastImage? = null
Observable.zip(obsSobelHorizontal, obsSobelVertical, { r1, r2 ->
log.info { Thread.currentThread() }
val fast = FastImage(width, height)
for (x in 0..width - 1) {
for (y in 0..height - 1) {
val argb1: Int? = r1.getARGB(x, y)
val argb2: Int? = r2.getARGB(x, y)
if (argb1 != null && argb2 != null) {
val G = sqrt(((argb1 * argb1) + (argb2 * argb2)).toDouble()).toInt().clamp()
val color = Color(G,G,G)
fast.setARGB(x, y, color.rgb)
}
}
}
return@zip fast
}).subscribe({ fastImageSobel = it }, { log.error("Can`t do sobel!", it) })
return fastImageSobel!!
}