Home > Net >  What is the equivalent of MATLAB's imfill(BW,locations,conn) in OpenCV , C ?
What is the equivalent of MATLAB's imfill(BW,locations,conn) in OpenCV , C ?

Time:07-07

I think I should use floodFill, but I don't know how exactly. Note that the problem is with this specific variation of imfill, with these parameters.

CodePudding user response:

MATLAB

According to MATLAB's documentation:

  • BW is the binary image (black & white image)
  • locations are pixel coordinates where the operation has to be initiated
  • conn the type of connectivity; how the neighboring pixels need to be filled. more details here

OpenCV

OpenCV's floodfill() has those parameters and a few more. Check this page for details

There are 4 parameters that are must be passed:

  • image: this can be 1 or 3-channel image (similar to BW in MATLAB)
  • mask: 1-channel image that must be 2 pixels larger in height and width than image
  • seedPoint: the point where the flood fill operation must start (similar to locations in MATLAB)
  • newVal: the new pixel value to be assigned to locations including and surrounding the seedPoint.

The optional parameter flags allows you to choose the connectivity (similar to conn in MATLAB). The good thing about flags is that you can come up with combinations. A few examples:

  • flags = 4 -> flood operation with 4-way connectivity
  • flags = 8 -> flood operation with 8-way connectivity
  • flags = 4 | (255 << 8) -> flood operation with 4-way connectivity and fill the mask with value 255

There are additional options that can be used see here

CodePudding user response:

Managed to find a solution, the general form of it is as follows: for an image Mat im, and vector of locations vector<Point> vec and using the default of connectivity = 4:

for (int i = 0; i < vec.size(); i  )
{
    floodFill(im, vec[i], Scalar(255, 255, 255));
}
  • Related