Advertisement
zemf4you

java.awt.image.BufferedImage processing: Otsu thresholding, graying, bynarizing, resizing

Jun 19th, 2021
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.49 KB | None | 0 0
  1. package images
  2.  
  3. import java.awt.Color
  4. import java.awt.image.BufferedImage
  5.  
  6.  
  7. object Processing {
  8.     fun BufferedImage.getColor(x: Int, y: Int) = Color(getRGB(x, y))
  9.     fun BufferedImage.setColor(x: Int, y: Int, color: Color) = setRGB(x, y, color.rgb)
  10.  
  11.     val BufferedImage.averageBrightness: Int
  12.         get() {
  13.             val grayed = grayed
  14.             var sum = 0
  15.             for (y in 0 until height)
  16.                 for (x in 0 until width)
  17.                     sum += grayed.getColor(x, y).red  // any channel
  18.             return sum / (width * height)
  19.         }
  20.  
  21.     val BufferedImage.grayed: BufferedImage
  22.         get() {
  23.             val result = BufferedImage(width, height, type)
  24.             for (y in 0 until height) {
  25.                 for (x in 0 until width) {
  26.                     val color = getColor(x, y)
  27.                     val gray = (0.21 * color.red + 0.71 * color.green + 0.07 * color.blue).toInt()
  28.                     val grayColor = Color(gray, gray, gray, color.alpha)
  29.                     result.setColor(x, y, grayColor)
  30.                 }
  31.             }
  32.             return result
  33.         }
  34.  
  35.     private val BufferedImage.histogram: IntArray
  36.         get() {
  37.             val grayed = grayed
  38.             val histogram = IntArray(256) { 0 }
  39.             for (i in 0 until width) {
  40.                 for (j in 0 until height) {
  41.                     val brightness = grayed.getColor(i, j).red  // any channel
  42.                     histogram[brightness]++
  43.                 }
  44.             }
  45.             return histogram
  46.         }
  47.  
  48.     val BufferedImage.otsuThreshold: Int
  49.         get() {
  50.             val histogram = histogram
  51.             val total = width * height
  52.             val sum = histogram.sum()
  53.             var sumB = 0f
  54.             var wB = 0
  55.             var varMax = 0f
  56.             var threshold = 0
  57.             for ((i, value) in histogram.withIndex()) {
  58.                 wB += value
  59.                 if (wB == 0)
  60.                     continue
  61.                 val wF = total - wB
  62.                 if (wF == 0)
  63.                     break
  64.                 sumB += (i * value)
  65.                 val mB = sumB / wB
  66.                 val mF = (sum - sumB) / wF
  67.                 val varBetween = wB.toFloat() * wF.toFloat() * (mB - mF) * (mB - mF)
  68.                 if (varBetween > varMax) {
  69.                     varMax = varBetween
  70.                     threshold = i
  71.                 }
  72.             }
  73.             return threshold
  74.         }
  75.  
  76.     val BufferedImage.binarized: BufferedImage
  77.         get() = binarize(averageBrightness)
  78.  
  79.     val BufferedImage.otsuBinarized: BufferedImage
  80.         get() = binarize(otsuThreshold)
  81.  
  82.     fun BufferedImage.binarize(threshold: Int): BufferedImage {
  83.         val grayed = grayed
  84.         val binarized = BufferedImage(width, height, BufferedImage.TYPE_BYTE_BINARY)
  85.         for (y in 0 until height) {
  86.             for (x in 0 until width) {
  87.                 val color = grayed.getColor(x, y)
  88.                 val brightness = color.red  // any channel
  89.                 val value = if (brightness > threshold) 255 else 0
  90.                 val newColor = Color(value, value, value, color.alpha)
  91.                 binarized.setColor(x, y, newColor)
  92.             }
  93.         }
  94.         return binarized
  95.     }
  96.  
  97.     fun BufferedImage.resize(w: Int, h: Int): BufferedImage {
  98.         val image = BufferedImage(w, h, type)
  99.         val g = image.createGraphics()
  100.         g.drawImage(this, 0, 0, w, h, null)
  101.         g.dispose()
  102.         return image
  103.     }
  104.  
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement