tonilopezmr / tonilopezmr.github.io

My web portfolio.
https://tonilopezmr-github-io.vercel.app
Other
4 stars 1 forks source link

Get histogram from image with open cv #42

Open tonilopezmr opened 5 years ago

tonilopezmr commented 5 years ago

Get Histogram in Android using opencv for android

private const val HUE_SIZE = 180
    private const val SATURATION_SIZE = 256
    private const val VALUE_SIZE = 256
    private const val HUE_RANGE = 179f
    private const val SATURATION_RANGE = 255f
    private const val VALUE_RANGE = 255f

fun createHistogram(bitmap: Bitmap): Histogram {
    val mat = convertBitmapToHSVMat(bitmap)
    val hue = calculateHistogramForHueImage(mat).getValues(HUE_SIZE)
    val saturation = calculateHistogramForSaturationImage(mat).getValues(SATURATION_SIZE)
    val value = calculateHistogramForValueImage(mat).getValues(VALUE_SIZE)

    return Histogram(
      hue = hue,
      saturation = saturation,
      value = value
    )
  }

  private fun convertBitmapToHSVMat(bitmap: Bitmap): Mat {
    val matFromBitmap = Mat(bitmap.height, bitmap.width, CvType.CV_8UC4)
    val matInRGBFormat = Mat()
    val matInHSVFormat = Mat()

    Utils.bitmapToMat(bitmap, matFromBitmap)
    Imgproc.cvtColor(matFromBitmap, matInRGBFormat, Imgproc.COLOR_RGBA2BGR, 4)
    Imgproc.cvtColor(matInRGBFormat, matInHSVFormat, Imgproc.COLOR_BGR2HSV, 3)
    return matInHSVFormat
  }

  private fun calculateHistogramForValueImage(mHsv: Mat): Mat {
    val histogramSize = MatOfInt(VALUE_SIZE)
    val histogramRange = MatOfFloat(0f, VALUE_RANGE)
    val histogram = Mat()

    Imgproc.calcHist(listOf(mHsv), MatOfInt(2), Mat(), histogram, histogramSize, histogramRange)
    Core.normalize(histogram, histogram, 0.0, 1.0, Core.NORM_MINMAX)
    Log.info(TAG, "VALUE: ${histogram.dumpToList()}")
    return histogram
  }

  private fun calculateHistogramForSaturationImage(mHsv: Mat): Mat {
    val histogramSize = MatOfInt(SATURATION_SIZE)
    val histogramRange = MatOfFloat(0f, SATURATION_RANGE)
    val histogram = Mat()

    Imgproc.calcHist(listOf(mHsv), MatOfInt(1), Mat(), histogram, histogramSize, histogramRange)
    Core.normalize(histogram, histogram, 0.0, 1.0, Core.NORM_MINMAX)
    Log.info(TAG, "SATURATION: ${histogram.dumpToList()}")
    return histogram
  }

  private fun calculateHistogramForHueImage(mHsv: Mat): Mat {
    val histogramSize = MatOfInt(HUE_SIZE)
    val histogramRange = MatOfFloat(0f, HUE_RANGE)
    val histogram = Mat()

    Imgproc.calcHist(listOf(mHsv), MatOfInt(0), Mat(), histogram, histogramSize, histogramRange)
    Core.normalize(histogram, histogram, 0.0, 1.0, Core.NORM_MINMAX)
    Log.info(TAG, "HUE: ${histogram.dumpToList()}")
    return histogram
  }

  private fun Mat.dumpToList(): String =
    dump().replace(";", ",")

  private fun Mat.getValues(size: Int): List<Double> {
    val values = mutableListOf<Double>()
    for (i in 0 until size) {
      values.add(get(i, 0)[0])
    }
    return values
  }

Reference

Tutorial histogram calculation opencv