How to use FloodFill algorithm

asked 2015-07-11 06:26:22 -0600

RB gravatar image

i want to use the FlooFill algorithm, i reviewd some posts and i found that i have to apply canny edge detection algorithm then the output of it will be the input for the FloodFill function as shown below in the code. the problem is, at run time i receive the below posted errors and i do not know how o fix it. please help be to set it right.


Size maskSize = new Size(gsMat.size.width()+2, gsMat.size.height()+2);
Mat edges = new Mat(maskSize, CVType.CV_32F);
Imgproc.Canny(gsMat, edges, 100, 200);
Imgproc.floodFill(gsMat, edges, new Point(350, 50), new Scalar(255), new Rect(), new Scalar(255), new Scalar(255), Imgproc.FLOODFILL_FIXED_RANGE);

ImageUtils.showMat(bgrMat, "");


OpenCV Error: Sizes of input arguments do not match (mask must be 2 pixel wider and 2 pixel taller than filled image) in cvFloodFill, file ..\..\..\..\opencv\modules\imgproc\src\floodfill.cpp, line 552
Exception in thread "main" CvException [org.opencv.core.CvException: cv::Exception: ..\..\..\..\opencv\modules\imgproc\src\floodfill.cpp:552: error: (-209) mask must be 2 pixel wider and 2 pixel taller than filled image in function cvFloodFill
at org.opencv.imgproc.Imgproc.floodFill_0(Native Method)
at org.opencv.imgproc.Imgproc.floodFill(
at com.example.seedgrowtest.MainClass.main(
edit retag flag offensive close merge delete


Canny will overwrite / reallocate your edges Mat to exactly the size of gsMat, so it is no more 2 pixels larger.

can you explain, why you think, you need a mask, and why you try to use Canny for it ? (an edge filter does not seem appropriate here)

berak gravatar imageberak ( 2015-07-12 03:25:48 -0600 )edit

no idea, but maybe this demo is useful to you.

berak gravatar imageberak ( 2015-07-12 04:44:49 -0600 )edit