LUT lookup table

LUT lookup table is a mapping table of pixel gray value. It takes pixel gray value as the index and the value after gray value mapping as the content in the table. It can be analogous to Map used in our programming process. Key is pixel gray Value, and Value is the Value after gray Value mapping.


API

public static void LUT(Mat src, Mat lut, Mat dst)

Copy the code
  • Parameter 1: SRC, input image matrix, its data type can only be CV_8U
  • Parameter 2: LUT, lookup table with 256 pixel gray values, single channel or the same number of SRC channels
  • Parameter 3: DST, output image matrix, its size is the same as SRC, data type is the same as LUT

If the second argument is a single channel, each channel in the input variable is mapped according to a LUT lookup table. If the second argument is multichannel, the i-th channel in the input variable is mapped to the I-th channel LUT lookup table for the second argument. The data type of the output image of the function is not consistent with the data type of the original image, but with the data type of the LUT lookup table. This is because the original gray value is mapped to a new space, so it needs to be consistent with the data type in the new space.

operation

class LutActivity : AppCompatActivity() {

private lateinit var mBinding: ActivityLutBinding

private lateinit var mRgb: Mat

override fun onCreate(savedInstanceState: Bundle?). {

super.onCreate(savedInstanceState)

mBinding = DataBindingUtil.setContentView(this, R.layout.activity_lut)



mRgb = Mat()

val bgr = Utils.loadResource(this, R.drawable.lena)

Imgproc.cvtColor(bgr, mRgb, Imgproc.COLOR_BGR2RGB)

showMat(mRgb)

bgr.release()



mBinding.btLutMulti.setOnClickListener {

doLutMulti()

}



mBinding.btLutSingle.setOnClickListener {

doLutSingle()

}

}



private fun doLutSingle(a) {

val lutOneByteArray = ByteArray(256)

for (i in 0.255) {

if (i in 0.100) lutOneByteArray[i] = 0

if (i in 101.200) lutOneByteArray[i] = 100

if (i > 200) lutOneByteArray[i] = 255.toByte()

}

val lutTable = Converters.vector_uchar_to_Mat(lutOneByteArray.toList())

val result = Mat()

Core.LUT(mRgb, lutTable, result)

showMat(result)

}



private fun doLutMulti(a) {

val lutOneByteArray = ByteArray(256)

for (i in 0.255) {

if (i in 0.100) lutOneByteArray[i] = 0

if (i in 101.200) lutOneByteArray[i] = 100

if (i > 200) lutOneByteArray[i] = 255.toByte()

}

val lutOne = Converters.vector_uchar_to_Mat(lutOneByteArray.toList())



val lutTwoByteArray = ByteArray(256)

for (i in 0.255) {

if (i in 0.100) lutOneByteArray[i] = 0

if (i in 101.150) lutOneByteArray[i] = 100

if (i in 151.200) lutOneByteArray[i] = 150.toByte()

if (i > 200) lutOneByteArray[i] = 255.toByte()

}



val lutTwo = Converters.vector_uchar_to_Mat(lutTwoByteArray.toList())



val lutThreeByteArray = ByteArray(256)

for (i in 0.255) {

if (i in 0.100) lutOneByteArray[i] = 100

if (i in 101.200) lutOneByteArray[i] = 200.toByte()

if (i > 200) lutOneByteArray[i] = 255.toByte()

}

val lutThree = Converters.vector_uchar_to_Mat(lutThreeByteArray.toList())



val lutTable = Mat()

Core.merge(listOf(lutOne, lutTwo, lutThree), lutTable)

lutOne.release()

lutTwo.release()

lutThree.release()



val result = Mat()

Core.LUT(mRgb, lutTable, result)

showMat(result)



lutTable.release()

result.release()

}





private fun showMat(source: Mat) {

val bitmap = Bitmap.createBitmap(source.width(), source.height(), Bitmap.Config.ARGB_8888)

Utils.matToBitmap(source, bitmap)

mBinding.ivLena.setImageBitmap(bitmap)

}



override fun onDestroy(a) {

mRgb.release()

super.onDestroy()

}

}

Copy the code

The results of


The source code

Github.com/onlyloveyd/…


This article is formatted using MDNICE