如何在OpenCV中为grabCut设置遮罩图像? [英] How to set a mask image for grabCut in OpenCV?

查看:97
本文介绍了如何在OpenCV中为grabCut设置遮罩图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何为OpenCV中的catchCut函数设置遮罩图像? 我想用选项做GC_INIT_WITH_MASK

How can I set a mask image for the grabCut function in OpenCV? I want to do GC_INIT_WITH_MASK with the options

GC_BGD    = 0,
GC_FGD    = 1,
GC_PR_BGD = 2,
GC_PR_FGD = 3,

如果您可以使用JavaCV回答这个问题,那将是一件很棒的事情,就像我在Scala/Java中所做的那样.

If you can answer this with JavaCV it would be great as I am doing this in Scala/Java.

推荐答案

输入图像(lena.png,lena_mask.png):

Input images (lena.png, lena_mask.png):

代码(JAVA):

import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Rect;
import org.opencv.core.Scalar;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;

/**
 * Example how to use grabCut algorithm in OpenCV using GC_INIT_WITH_MASK.
 *
 */
public class ImageSegmentByMask {

    public static void main(String[] args) {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);

        // will read image
        Mat image = Highgui.imread("lena.png");
        Mat mask = Highgui.imread("lena_mask.png", Highgui.CV_LOAD_IMAGE_GRAYSCALE);

        Rect rectangle = new Rect(10, 10, image.cols() - 20, image.rows() - 20);

        Mat bgdModel = new Mat(); // extracted features for background
        Mat fgdModel = new Mat(); // extracted features for foreground
        Mat source = new Mat(1, 1, CvType.CV_8U, new Scalar(0));

        convertToOpencvValues(mask); // from human readable values to OpenCV values 

        int iterCount = 1;
        Imgproc.grabCut(image, mask, rectangle, bgdModel, fgdModel, iterCount, Imgproc.GC_INIT_WITH_MASK);


        convertToHumanValues(mask); // back to human readable values
        Imgproc.threshold(mask,mask,128,255,Imgproc.THRESH_TOZERO);

        Mat foreground = new Mat(image.size(), CvType.CV_8UC1, new Scalar(0, 0, 0));
        image.copyTo(foreground, mask);

        Highgui.imwrite("sandbox/sucess1.jpg", foreground);
    }

    private static void convertToHumanValues(Mat mask) {
        byte[] buffer = new byte[3];
        for (int x = 0; x < mask.rows(); x++) {
            for (int y = 0; y < mask.cols(); y++) {
                mask.get(x, y, buffer);
                int value = buffer[0];
                if (value == Imgproc.GC_BGD) {
                    buffer[0] = 0; // for sure background
                } else if (value == Imgproc.GC_PR_BGD) {
                    buffer[0] = 85; // probably background
                } else if (value == Imgproc.GC_PR_FGD) {
                    buffer[0] = (byte) 170; // probably foreground
                } else {
                    buffer[0] = (byte) 255; // for sure foreground

                }
                mask.put(x, y, buffer);
            }
        }
    }

    /**
     * Converts level of grayscale into OpenCV values. White - foreground, Black
     * - background.
     * 
     * @param mask
     */
    private static void convertToOpencvValues(Mat mask) {
        byte[] buffer = new byte[3];
        for (int x = 0; x < mask.rows(); x++) {
            for (int y = 0; y < mask.cols(); y++) {
                mask.get(x, y, buffer);
                int value = buffer[0];
                if (value >= 0 && value < 64) {
                    buffer[0] = Imgproc.GC_BGD; // for sure background
                } else if (value >= 64 && value < 128) {
                    buffer[0] = Imgproc.GC_PR_BGD; // probably background
                } else if (value >= 128 && value < 192) {
                    buffer[0] = Imgproc.GC_PR_FGD; // probably foreground
                } else {
                    buffer[0] = Imgproc.GC_FGD; // for sure foreground

                }
                mask.put(x, y, buffer);
            }
        }

    }

}

结果:

这篇关于如何在OpenCV中为grabCut设置遮罩图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆