简体   繁体   中英

OpenCV - Filling an Image using Java

I got it working fine when using C++, code bellow:

Mat floodFilled = cv::Mat::zeros(dilateGrad.rows + 2, dilateGrad.cols + 2, CV_8U);

floodFill(dilateGrad, floodFilled, cv::Point(0, 0), 0, 0, cv::Scalar(), cv::Scalar(), 4 + (255 << 8) + cv::FLOODFILL_MASK_ONLY);
floodFilled = cv::Scalar::all(255) - floodFilled;

Mat temp;
floodFilled(Rect(1, 1, dilateGrad.cols - 2, dilateGrad.rows - 2)).copyTo(temp);
floodFilled = temp;
imshow("5. Floodfill", floodFilled);

But I want to be able to do the same thing in Java, where I can't do something like this:

floodFilled = cv::Scalar::all(255) - floodFilled;

In java my code it's like:

private static Mat floodFill(Mat img)
{
    Mat floodfilled = Mat.zeros(img.rows() + 2, img.cols()+2, CvType.CV_8U);
    Imgproc.floodFill(img, floodfilled, new Point(0,0), new Scalar(0,255,0));

    return floodfilled;
}

The result is that the image I return is exactly the same the one I recieve in the function. I mean, I expect the filled image, but I got the original image as result, that is, nothing happens.

Got it working fine with this code:

private static Mat floodFill(Mat img)
{
    Mat floodfilled = Mat.zeros(img.rows() + 2, img.cols() + 2, CvType.CV_8U);
    Imgproc.floodFill(img, floodfilled, new Point(0, 0), new Scalar(255), new OpenCVForUnity.Rect(), new Scalar(0), new Scalar(0), 4 + (255 << 8) + Imgproc.FLOODFILL_MASK_ONLY);

    Core.subtract(floodfilled, Scalar.all(0), floodfilled);

    Rect roi = new Rect(1, 1, img.cols() - 2, img.rows() - 2);
    Mat temp = new Mat();

    floodfilled.submat(roi).copyTo(temp);

    img = temp;

    //Core.bitwise_not(img, img);

    return img;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM