This is the 18th day of my participation in Gwen Challenge

decolorizing

Decoloration is the process of converting a color image to a grayscale image. It is also an essential tool for digital printing, stylized black-and-white photo rendering, and many single-channel image processing applications. Imgproc.cvtColor(RGB, gray, imgproc. COLOR_RGB2GRAY) COLOR_RGB2GRAY (RGB, gray, imgproc. COLOR_RGB2GRAY) COLOR_RGB2GRAY (RGB, gray, imgproc. cvtColor) COLOR_RGB2GRAY (RGB, gray, imgproc. COLOR_RGB2GRAY) COLOR_RGB2GRAY (RGB, gray) COLOR_RGB2GRAY


G R A Y = 0.299 R + 0.587 G + 0.114 B GRAY * R = 0.299 + 0.114 + 0.587 * G * B

However, the transformed image has a loss of contrast, unable to distinguish colors. OpenCV provides us with another decolor method, Photo. Decolor, designed to maximize image contrast.

The principle of

Paper details: www.cse.cuhk.edu.hk/~leojia/pro…

Paper PDF:www.computer.org/csdl/pds/ap…

Compared with the simple R/G/B linear combination, the author constructed a more complex polynomial fitting. The basis of the vector space is: [R, G, B, Rg,rb, GB, R2, G2, B2], and the core is to obtain the grayscale function:


f ( r . g . b ; w ) = i ( w i m i ) m i It’s the first part of the vector space i So the grayscale problem is transformed into a solution 9 a w The coefficient of f(r,g,b; W) = \sum_i(w_im_i)\\ m_i is the ith basis of vector space, so the gray-scale problem is transformed to solving 9 {w} coefficients

API

public static void decolor(Mat src, Mat grayscale, Mat color_boost) 
Copy the code
  • Parameter 1: SRC, input 8-bit three-channel image.
  • Parameter 2: Grayscale, output 8-bit single channel grayscale.
  • Parameter 3: color_boost, output 8-bit three-channel image. Contrast enhanced image.

operation

/** ** * author: yidong * 2020/12/5 */
class DecolorActivity : AppCompatActivity() {

    private val mBinding: ActivityDecolorBinding by lazy {
        ActivityDecolorBinding.inflate(layoutInflater)
    }

    private val mList = mutableListOf<ImageTextObject>()
    private lateinit var mAdapter: ImageTextAdapter

    override fun onCreate(savedInstanceState: Bundle?). {
        super.onCreate(savedInstanceState)
        setContentView(mBinding.root)
        mAdapter = ImageTextAdapter(this, mList)
        mBinding.container.adapter = mAdapter
        val bgr = Utils.loadResource(this, R.drawable.ceil)
        val rgb = Mat()
        Imgproc.cvtColor(bgr, rgb, Imgproc.COLOR_BGR2RGB)
        val gray = Mat()
        val dst = Mat()
        val boost = Mat()
        mBinding.isLoading = true
        GlobalScope.launch(Dispatchers.IO) {
            Imgproc.cvtColor(rgb, gray, Imgproc.COLOR_RGB2GRAY)
            Photo.decolor(rgb, dst, boost)
            launch(Dispatchers.Main) {
                mList.add(ImageTextObject(rgb, "Original"))
                mList.add(ImageTextObject(gray, "RGB2GRAY"))
                mList.add(ImageTextObject(dst, "DeColor"))
                mList.add(ImageTextObject(boost, "ColorBoosting"))
                mAdapter.notifyItemRangeInserted(0.4)
                mBinding.isLoading = false}}}}Copy the code

The effect

Decolor renderings using DECOLor and RGB2GRAY, respectively, differ greatly in contrast and color retention.

The source code

Github.com/onlyloveyd/…