Salt and pepper noise

Salt and pepper noise, also known as impulse noise, is a kind of noise commonly seen in images. It is a random white or black dot, which may have black pixels in the bright area or white pixels in the dark area (or both). Salt and pepper noise may be caused by sudden strong interference of image signal, analog to digital converter or bit transmission error, etc. For example, invalid sensor results in minimum pixel value and saturated sensor results in maximum pixel value. Salt and pepper noise refers to two kinds of noise: salt noise and pepper noise. Salt noise is generally white noise, pepper noise is generally black noise, the former is high grayscale noise, the latter belongs to low grayscale noise, generally two kinds of noise appear at the same time, presenting on the image is black and white.

API

So far, OpenCV 4 does not provide a function specifically for adding salt and pepper noise to the image. But it can be manually generated based on the understanding of salt and pepper. The steps are shown below.

Generate steps

operation

As the number of noise points increases, the time of adding noise will also increase. To prevent ANR, consider asynchronous processing yourself.

/** * Saltpeppernoise * author: yidong * 2020/3/31 */ Class SaltPepperNoiseActivity: AppCompatActivity() { companion object { fun launch(context: Context) { val intent = Intent(context, SaltPepperNoiseActivity::class.java) context.startActivity(intent) } } private lateinit var mBinding: ActivitySaltPepperNoiseBinding private lateinit var mRgb: Mat override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) mBinding = DataBindingUtil.setContentView(this, R.layout.activity_salt_pepper_noise) mBinding.presenter = this val bgr = Utils.loadResource(this, R.drawable.lena) mRgb = Mat() Imgproc.cvtColor(bgr, mRgb, Imgproc.COLOR_BGR2RGB) showMat(mBinding.ivLena, mRgb) } fun addNoise() { hideKeyboard() val source = mRgb.clone() var number = 10000 try { number = mBinding.etNoiseNumber.text.toString().toInt() } catch (e: NumberFormatException) { } for (k in 0.. number) { val i = (0.. 1000).random() % source.cols() val j = (0.. 1000).random() % source.rows() when ((0.. 100).random() % 2) {0 -> {when (source.channels()) {1 -> {source.put(j, I, 255.0)} 2 -> {source.put(j, I, 255.0)} 2 -> {source.put(j, I, 255.0, Put (j, I, 255.0, 255.0, 255.0)} else -> {source.put(j, I, 255.0, 255.0, 255.0, 255.0)}}} - > {1 when (source) channels ()) {1 - > {source. The put (j, I, 0.0)} - > {source. Put 2 (j, I, 0.0, 3 - > 0.0)} {source. The put (j, I, 0.0, 0.0, 0.0)} else - > {source. The put (j, I, 0.0, 0.0, 0.0, 0.0)}}}}} showMat(mBinding. IvResult, source) source.release()} private fun showMat(view: ImageView, source: Mat) { val bitmap = Bitmap.createBitmap(source.width(), source.height(), Bitmap.Config.ARGB_8888) Utils.matToBitmap(source, bitmap) view.setImageBitmap(bitmap) } private fun hideKeyboard() { val inputMethodManager = this.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager inputMethodManager.hideSoftInputFromWindow( mBinding.ivLena.windowToken, InputMethodManager.HIDE_NOT_ALWAYS ) } override fun onDestroy() { mRgb.release() super.onDestroy() } }Copy the code

The effect

Salt and pepper noise

The source code

Github.com/onlyloveyd/…