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
I had a little try at this. It may need some tweaking but it is an idea. My algorithm is as follows:
Find all black pixels that have exactly 1 black neighbouring pixel, colour it red and put it on a list of pixels at ends.
Go through list of all red pixels, and find nearest other red pixel and draw straight line between the two.
By the way, I only implemented the first part - gotta leave something for the reader to do ;-)
#!/usr/bin/perl
use strict;
use warnings;
use Image::Magick;
use Data::Dumper;
my $im=Image::Magick->new();
$im->Read('EsmKh.png');
my ($width,$height)=$im->Get('width','height');
my $out=Image::Magick->new();
$out->Read('EsmKh.png');
my @pixels;
# Iterate over pixels
for my $y (0..($height-1)){
for my $x (0..($width-1)){
my (@pixel) = split(/,/, $im->Get("pixel[$x,$y]"));
$pixels[$x][$y]=$pixel[0];
}
}
# Find black pixels that have precisely 1 black neighbour
for my $y (1..($height-2)){
for my $x (1..($width-2)){
next if $pixels[$x][$y]!=0;
my $neighbours=0;
for(my $i=$x-1;$i<=$x+1;$i++){
for(my $j=$y-1;$j<=$y+1;$j++){
$neighbours++ if $pixels[$i][$j]==0;
}
}
$neighbours--; # Uncount ourself !
if($neighbours==1){
$out->Set("pixel[$x,$y]"=>'red');
}
}
}
$out->Write(filename=>'out.png');
Result
You will have to zoom right in to see the red pixels...

Zoomed Image
