OpenCV removing gulfs from binary image

181 Views Asked by At

I have a binary thresholed image of cart with ribs and some of them are cracked.

threshold image

I need to use something like cv2.distanceTransform() to get markers for watershed algorithm, but I need to get rid of cracks. If I keep cracks, distance transform disconnects me the rib into several pieces

distance transformed image

I tried closing of dilating, but it efects balls in between ribs and I don't get each rib separate

Of course I have tried different values of thresholding in distance transformed image, but when I filter balls by threshold, I split rib too.

Is there something like bay-removing algorithm? Or is there some better way?

2

There are 2 best solutions below

0
On

Create a filter that looks for surrounding pixels (mostly vertical pixels) to decide what color the central pixel should be. ea

//psudo code
for(int x=0;x<maxdimensionX;x++)
{
 for (int y = 0 ;y<maxdimensionY;y++)
 {
 c1= max(y-scope,0)
 c2 = min(y+scope,maxdimenionY)
 for (int c=c1;c<c2;c++)
  {
    color[c]=colorresult(x,y) 
    if( color[c]==mysearchcolor)count++
  }  
  if (count>treshold) set in a new picture pixel(x,y,mysearchcolor)else make it a background pixel.
0
On

Try cv::dilate method with vertical rectangular kernel.