Android OpenCV(五):亮度和对比度

亮度和对比度

对RGB色彩图像来讲,亮度越高,像素点对应的RGB值应该越大;亮度越低,像素点对应的RGB值应该越小。而对比度则是用来描述图像颜色与亮度之间的差异感知,对比度越大,图像的每个像素与周围的差异性也就越大,整个图像的细节就越显著;反之亦然。

调整图像亮度和对比度属于像素变换-点操作
g(i,j) = αf(i,j) +β (其中 α>0 ,α 增益(放大倍数),用来控制图像的对比度,β (偏置),用控制图像的亮度

操作

  • 像素值加法
  • 像素值乘法
class ContrastBrightnessActivity : AppCompatActivity() {
    private lateinit var mBinding: ActivityContrastBrightnessBinding
    private lateinit var source: Mat

    private var originBrightness: Double = 0.0

    private var brightness: Double = 0.0
        set(value) {
            field = value
            adjustBrightnessContrast()
        }
    private var contrast: Double = 100.0
        set(value) {
            field = value
            adjustBrightnessContrast()
        }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mBinding = DataBindingUtil.setContentView(this, R.layout.activity_contrast_brightness)
        val bgr = Utils.loadResource(this, R.drawable.lena)

        source = Mat()
        Imgproc.cvtColor(bgr, source, Imgproc.COLOR_BGR2RGB)
        val bitmap = Bitmap.createBitmap(source.width(), source.height(), Bitmap.Config.ARGB_8888)
        Utils.matToBitmap(source, bitmap)
        mBinding.ivSource.setImageBitmap(bitmap)
        originBrightness = Core.mean(source).`val`[0]
        mBinding.sbBrightness.progress = originBrightness.toInt()
        mBinding.sbBrightness.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                brightness = progress.toDouble()
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {
            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {
            }
        })

        mBinding.sbContrast.progress = 100
        mBinding.sbContrast.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                contrast = progress.toDouble()
            }

            override fun onStartTrackingTouch(seekBar: SeekBar?) {
            }

            override fun onStopTrackingTouch(seekBar: SeekBar?) {
            }
        })
    }

    private fun adjustBrightnessContrast() {
        val pre = Mat()
        Core.add(
            source,
            Scalar(
                brightness - originBrightness,
                brightness - originBrightness,
                brightness - originBrightness
            ),
            pre
        )
        val dst = Mat()
        Core.multiply(
            pre,
            Scalar(
                contrast / 100,
                contrast / 100,
                contrast / 100,
                contrast / 100
            ),
            dst
        )
        val bitmap = Bitmap.createBitmap(dst.cols(), dst.rows(), Bitmap.Config.ARGB_8888)
        Utils.matToBitmap(dst, bitmap)
        mBinding.ivSource.setImageBitmap(bitmap)
        pre.release()
        dst.release()
    }

    override fun onDestroy() {
        source.release()
        super.onDestroy()
    }
}

结果

源码

https://github.com/onlyloveyd/LearningAndroidOpenCV

onlyloveyd CSDN认证博客专家 Android Kotlin OpenCV
个人公众号【Android or OpenCV】,热爱Android、Kotlin、Flutter和OpenCV。毕业于华中科技大学计算机专业,曾就职于华为武汉研究所。目前在三线小城市生活,专注技术与研发。
©️2020 CSDN 皮肤主题: 数字20 设计师: CSDN官方博客 返回首页
实付0元
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、C币套餐、付费专栏及课程。

余额充值