Ask Your Question
0

How to floodfill an image with java api

asked 2017-01-13 09:58:24 -0600

Bazooka gravatar image

I got it working fine when I did it with C++, it was like:

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

floodFill(src, 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, src.cols - 2, src.rows - 2)).copyTo(temp);
floodFilled = temp;
imshow("5. Floodfill", floodFilled);

But I can't reproduce this in java because some features does not exist there, like these lines:

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

I can't subtract a matrix from a scalar this way.

floodFilled(Rect(1, 1, src.cols - 2, src.rows - 2)).copyTo(temp);

this constructor does not exist either.

The output image was exactly the same as the input one.

So, I want to be able to do the same thing that I did in C++, with java instead. Thanks in advance.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-01-16 05:18:48 -0600

Bazooka gravatar image

I just realized that this line

floodFilled(Rect(1, 1, src.cols - 2, src.rows - 2)).copyTo(temp);

It's justing defining a submatrix with the ROI(region of intresting), so I did this and got it working well:

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;
}
edit flag offensive delete link more

Comments

Hi, I am also running the same code as yours but it seems like flood fill is not working because when I am checking the final img(matrix) and converting it to bmp it is same as initial.

Ayesh Rehman gravatar imageAyesh Rehman ( 2018-10-04 00:47:28 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2017-01-13 09:58:24 -0600

Seen: 3,030 times

Last updated: Jan 16 '17