2017-02-03 7 views
0

가장자리 감지 용 Sobele 필터를 구현하는 데 문제가 있습니다. 모든소벨 필터 기능

첫째, SOBEL_VERTICAL 및 SOBEL_HORIZONTAL 회선을 다음 공식을 이용하여 화소의 색상을 계산한다 : G = SQRT (GX * GX + Gy의 * Gy의)

번호 :

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) 
       } 
      } 
     } 
     [email protected] fast 

    }).subscribe({ fastImageSobel = it }, { log.error("Can`t do sobel!", it) }) 

    return fastImageSobel!! 
} 

enter image description here 오른쪽 이미지가 잘못되었습니다.

답변

0

좋아, 몇 가지 조사를 마친 후 내 문제가 무엇인지 알았습니다. 내 픽셀은 RGB int 배열에 저장되었습니다. 따라서 각 색상 채널에 대해 G를 수동으로 계산해야합니다.

    val cx = Color(argb1) 
        val cy = Color(argb2) 

        val rx = cx.red 
        val gx = cx.green 
        val bx = cx.blue 

        val ry = cy.red 
        val gy = cy.green 
        val by = cy.blue 

        val R = Math.hypot(rx.toDouble(), ry.toDouble()).toInt().clamp() 
        val G = Math.hypot(gx.toDouble(), gy.toDouble()).toInt().clamp() 
        val B = Math.hypot(bx.toDouble(), by.toDouble()).toInt().clamp() 

        val color = Color(R, G, B) 

        fast.setARGB(x, y, color.rgb) 
+0

영수증과 자르기의 가장자리를 자동으로 감지 할 수 있습니까? –