如何删除背景图片

时间:2019-10-23 13:12:47

标签: android opencv image-segmentation

我正在研究Android上的Opencv项目。分割图像时,我不知道如何删除背景。这是图像分割的源代码。

关闭后的以下图片。我不知道如何将白色物体变成1张灰度图像

http://prntscr.com/pn2tlk

private void Threshold() {

        Utils.bitmapToMat(imageBmp,srcMat);
        Imgproc.cvtColor(srcMat, gray, Imgproc.COLOR_RGBA2GRAY);
        grayBmp = Bitmap.createBitmap(imageBmp.getWidth(),imageBmp.getHeight(),Bitmap.Config.RGB_565);
        Utils.matToBitmap(gray, grayBmp);

        Mat threshold = new Mat();
        Imgproc.threshold(gray,threshold, 0, 255, Imgproc.THRESH_BINARY_INV+ Imgproc.THRESH_OTSU);
        thresBmp = Bitmap.createBitmap(imageBmp.getWidth(),imageBmp.getHeight(),Bitmap.Config.RGB_565);
        Utils.matToBitmap(threshold, thresBmp);

        Mat closing = new Mat();
        Mat kernel = Mat.ones(3, 3, CvType.CV_8U);
        Imgproc.morphologyEx(threshold, closing, Imgproc.MORPH_CLOSE, kernel, new Point(-1, -1), 2);
        closingBmp = Bitmap.createBitmap(imageBmp.getWidth(),imageBmp.getHeight(),Bitmap.Config.RGB_565);
        Utils.matToBitmap(closing, closingBmp);

        resultBmp = Bitmap.createBitmap(imageBmp.getWidth(),imageBmp.getHeight(),Bitmap.Config.RGB_565);
        Utils.matToBitmap(closing, resultBmp);

        Glide.with(ResultActivity.this).asBitmap().load(resultBmp).into(ivAfter);
}

0 个答案:

没有答案