`

15.GrabCut区域分割

阅读更多
Mat image = Imgcodecs.imread("2023.jpg");
        Mat mask = new Mat();
        // 创建前景和背景模型
        Mat bgModel = new Mat();
        Mat fgModel = new Mat();
        // 指定前景的矩形区域
        Rect rect = new Rect(new Point(1500, 1000), new Point(2400, 1800));
//        Mat imageROI = new Mat(image, rect);
        // 运行GrabCut算法
        Imgproc.grabCut(image, mask, rect, bgModel, fgModel, 3, Imgproc.GC_INIT_WITH_RECT);
        // 生成最终的前景和背景掩模
        Core.compare(mask, new Scalar(Imgproc.GC_PR_FGD), mask, Core.CMP_EQ);
        Mat dst = new Mat();
        image.copyTo(dst, mask); // 保留前景
//        Core.compare(mask, new Scalar(Imgproc.GC_PR_BGD), mask, Core.CMP_EQ);
//        image.copyTo(image, mask); // 保留背景
        // 保存结果图像
        Imgcodecs.imwrite("output-bg12.jpg", dst);
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics