Search code examples
javaopencvimage-processingedge-detectionflood-fill

FloodFill algorithm fills beyond the specified boundries


i want to floodFill a certain area "the rectangle on the left side" of the image posted below. to floodFill that area, i specified a point inside that rectangle to know the value of the pixel as follows:

    Log.D(TAG, "", "PixVal: "+gsMat.get(100, 240)[0]);//at that position the pixVal is 91 up ok
    Log.D(TAG, "", "PixVal: "+gsMat.get(250, 60)[0]);//at that position the pixVal is 217 l ??peoblem
    Log.D(TAG, "", "PixVal: "+gsMat.get(330, 310)[0]);//at that position the pixVal is 123 down ok
    Log.D(TAG, "", "PixVal: "+gsMat.get(330, 580)[0]);//at that position the pixVal is 84 r ok

and i am sure that point(x= 60, y= 250) lays inside that rectangle on the left. and i wrote the below code to folldfill that area. but i got the images posted below as result

"Mat subbed = gsRawMat.submat(r)" shows the whole orignal image as if the the whole pixel values in the image lays within the range of 216 to 218?

"ImageUtils.showMat(subbed, "subbed");" shows a white image as if the the whole pixel values in the image lays within the range of 216 to 218, hence, the whole area painted in white (255)?

please let me know why such behavior occurs

Code:

    Mat m = new Mat();
    Rect r = new Rect();
    Imgproc.floodFill(gsMat,
            m,
            new Point(60, 250),
            new Scalar(255),//to fill the designated area with color(255)
            r,//the rect that will encompass the designated area
            new Scalar(216),//lower bound
            new Scalar(218),//upper bound
            Imgproc.FLOODFILL_FIXED_RANGE);

    Mat subbed = gsRawMat.submat(r);// to extract the rectangle contains the flooded area from the main image
    ImageUtils.showMat(gsMat, "gsMat");//to show the gsmat
    ImageUtils.showMat(subbed, "subbed");//to show the subbed area from the gsMat

results gsMat: enter image description here

result subbed mat: enter image description here


Solution

  • I have not used this function, but according to the (C++) documentation, the parameters are not "lower bound" and "upper bound", but "lower difference" and "upper difference". Their brightness/color values are relative to the seed point x= 60, y= 250. I haven't had time to think it through, but could that be responsible?

    http://docs.opencv.org/modules/imgproc/doc/miscellaneous_transformations.html#floodfill