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

Image segmentation

Image segmentation is the technology and process of dividing an image into several specific and unique regions and proposing objects of interest. It is the key step from image processing to image analysis. The existing image segmentation methods can be divided into the following categories: thresholding based segmentation method, region based segmentation method, edge based segmentation method and specific theory based segmentation method. From a mathematical point of view, image segmentation is the process of dividing a digital image into non-intersecting areas. The process of image segmentation is also a marking process, that is, the pixels belonging to the same region are assigned the same number.

Mean-shift

MeanShfit algorithm is a general clustering algorithm, which can usually achieve color image segmentation.

Basic idea is: first, choose a central point, and then calculate the center of all points within a certain range to the center of the average of the distance vector, calculating the average mean an offset, and then move the center point to offset the mean position, through the repeated move, can make the center gradually approaching to the best position. This idea is similar to the gradient descent method, which can reach the local optimal solution or global optimal solution on the gradient by constantly moving towards the gradient descent direction.

Algorithm principle reference: www.biaodianfu.com/mean-shift….

API

PyrMeanShiftFiltering function is also a kind of filtering, which can only achieve the effect of smoothing the image, but can not directly segment the image. But the smoothed image is more convenient for us to carry out image segmentation.

public static void pyrMeanShiftFiltering(Mat src, Mat dst, double sp, double sr, int maxLevel, TermCriteria termcrit)
Copy the code
  • Parameter 1: SRC, input image, type is 8-bit three-channel.
  • Parameter two: DST, output image, same size type as SRC.
  • Parameter 3: SP, the radius of the physical space of drift.
  • Parameter 4: Sr, the radius of color space of drift.
  • Parameter 5: maxLevel, the maximum level of the pyramid to be split.
  • Parameter 6: TERmcrit, termination criteria: when to stop mean-shift iteration.

The larger the SP and SR are, the more obvious the smoothing effect is and the longer the processing time is

operation

Since mean-shift Mean Shift could not segment the image directly, we performed the segmentation operation with flooding filling method.

/** * mean-shift **@author yidong
 * @date11/25/20 * /
class MeanShiftActivity : AppCompatActivity() {

    private val mBinding: ActivityMeanShiftBinding by lazy {
        ActivityMeanShiftBinding.inflate(layoutInflater)
    }
    private lateinit var mRgb: Mat

    override fun onCreate(savedInstanceState: Bundle?). {
        super.onCreate(savedInstanceState)
        setContentView(mBinding.root)
        mRgb = Mat()
        val bgr = Utils.loadResource(this, R.drawable.number)
        Imgproc.cvtColor(bgr, mRgb, Imgproc.COLOR_BGR2RGB)
        mBinding.ivLena.showMat(mRgb)
        mBinding.isLoading = true
        GlobalScope.launch(Dispatchers.IO) {
            doMeanShift()
        }
    }

    private fun doMeanShift(a) {
        val dst = Mat()
        Imgproc.pyrMeanShiftFiltering(mRgb, dst, 40.0.40.0)

        val maskers = Mat(dst.rows() + 2, dst.cols() + 2, CvType.CV_8UC1, Scalar.all(0.0))
        Imgproc.floodFill(
            dst,
            maskers,
            Point(7.0.7.0),
            Scalar(65.0.105.0.225.0),
            Rect(),
            Scalar.all(10.0),
            Scalar.all(10.0),
            Imgproc.LINE_4 or Imgproc.FLOODFILL_FIXED_RANGE or (250 shl 8)
        )

        GlobalScope.launch(Dispatchers.Main) {
            mBinding.isLoading = false
            mBinding.ivResult.showMat(dst)
        }
    }
}
Copy the code

The effect

The source code

Github.com/onlyloveyd/…