Filling gaps in shape edges

后端 未结 4 971
名媛妹妹
名媛妹妹 2020-12-15 01:27

Is there an algorithm that would perform well in terms of filling holes like those on the sample image? Dilation doesn\'t work well, cause before I eventually manage to conn

4条回答
  •  悲哀的现实
    2020-12-15 02:04

    This is an OpenCV, C++ implementation of Mark Setchell's algorithm. It is very straightforward, uses the same kernel and convolutes the input image via the cv::filter2D function. I've, optionally, inverted the input image so target pixels have a value of 255:

    //Read input Image
    cv::Mat inputImage = cv::imread( "C://opencvImages//blobs.png", cv::IMREAD_GRAYSCALE );
    
    //Invert the image:
    inputImage = 255 - inputImage;
    
    //Threshold the image so that white pixels get a value of 0 and
    //black pixels a value of 10:
    cv::threshold( inputImage, inputImage, 128, 10, cv::THRESH_BINARY );
    

    Now, setup the kernel and convolute the image, like this:

    //Set up the end-point kernel:
    cv::Mat kernel = ( cv::Mat_(3, 3) <<
      1, 1, 1,
      1, 10, 1,
      1, 1, 1
    );
    
    //Convolute image with kernel:
    cv::filter2D( inputImage, inputImage, -1 , kernel, cv::Point( -1, -1 ), 0, cv::BORDER_DEFAULT );
    

    The direct result of the convolution is this, pixels at the end points now have a value of 110, which can be seen (barely) in this output:

    Let's threshold these pixels and overlay them on the original image. This is the result (pixels in red):

    Additionally, the skeleton of the image can be computed at the beginning. The skeleton has a normalized line-width of 1 pixel. The function is part of the Extended Image Processing module of OpenCV:

    #include 
    
    //Compute the skeleton of the input:
    cv::Mat skel;
    int algorithmType = 1;
    cv::ximgproc::thinning( inputImage, skel, algorithmType ); 
    

提交回复
热议问题