Hi,
Another good option is the following:
# invert mask
fslmaths mask -mul -1 -add 1 invmask
# find clusters
cluster -i invmask -t 0.5 -o clusters
# find max cluster number
maxind=`fslstats clusters -R | awk `{ print $2 }'`;
# threshold to get rid of everything except the max cluster (the background) and then the inverse is the filled mask
thresh=`echo $maxind - 0.5 | bc -l`;
fslmaths clusters -thr $thresh -bin -mul -1 -add 1 filledmask
It takes a few lines but the logic is pretty simple.
Assume that all connected components (clusters) of the
zero voxels are holes except for the biggest one which is
the background. Then pick out the background one and
by definition, everything else is either mask or hole and
so should be part of the filled mask. If that's not true (because
the mask spans the volume from one side to another, splitting
the background into more than one part - or if a hole
could potentially be bigger than the background) then
this would not work. But in the vast majority of cases it
is exactly what you want for filling holes of any size without
modifying the borders in any way.
Hope this helps.
Cheers,
Mark
On 28 Sep 2011, at 13:54, Anderson Winkler wrote:
> Hi Rolf,
>
> Yeah, this will work when the holes are 1 voxel and not close to the surface, i.e. at least 2 voxels deep I believe.
> On the Octave trick, at least 2 dimensions are needed, even for 1 voxel holes, but not always the same 2 dimensions (this depends on the local configuration of the surface), so we have to cover all 3. The method may still fail if the object has a very curled shape, like a spiraled whorl, etc. For almond or C- or S-shape structures, it should work. But with ANTS it may be easier and faster (its compiled I think, no?).
>
> All the best,
>
> Anderson
>
>
> On 28/09/11 14:05, Rolf A. Heckemann wrote:
>> Hi Ed, hi Anderson
>>
>> It is true that the closing operation (dilate and erode) can also fill
>> crevices at the surface. If this is a worry, you can restore the
>> original surface afterwards. Again, this is possible because the
>> holes to fill are the size of one voxel, according to Ed.
>>
>> fslmaths mask -kernel 3D -dilM -ero mask-holefilled # as previously
>> fslmaths mask-holefilled -kernel 3D -ero mask-hf-ero
>> fslmaths mask -add mask-hf-ero -bin mask-holefilled-surfacecorrected
>>
>> This works for superficial crevices of a depth of up to one voxel.
>>
>> On the octave advice, I would contend that (again under the
>> 1-voxel-holesize-constraint) it is only necessary to fill in one
>> dimension.
>>
>> The ANTs package (http://picsl.upenn.edu/ANTS/) has a very capable
>> holefilling function, integrated in the ImageMath tool. It works in
>> two or three dimensions, and with holes of any size.
>>
>> Rolf
>>
>>
>> On 28 September 2011 18:18, Anderson Winkler
>> <[log in to unmask]> wrote:
>>> Hi Ed and Rolf,
>>>
>>> Hmm, I'm afraid this dilation and erosion will fill the holes, but it will
>>> also change a bit the external shape of the mask...
>>>
>>> It's difficult to accomplish automated filling without recursion and this
>>> gets complicated with shell scripting only. Perhaps a faster solution, if
>>> you have access to Matlab with the Image Processing toolbox, is to use the
>>> "imfill", which works for both 2-D and 3-D images.
>>>
>>> If you don't have access to Matlab, you can use Octave. The following
>>> function appears to do the trick:
>>> http://www.irit.fr/PERSONNEL/SAMOVA/joly/Teaching/M2IRR/IRR05/Estrade-Mercadier/imfill.m
>>> However, it works for 2D only. To make it work in 3D, you have to run it
>>> sequentially for every slice in coronal, sagital and axial directions, which
>>> will produce 3 filled volumes. Use the AND operator to combine all 3 into a
>>> single volume, and you should have then the result you need.
>>>
>>> Hope this helps!
>>>
>>> All the best,
>>>
>>> Anderson
>>>
>>>
>>> On 28/09/11 11:18, Rolf A. Heckemann wrote:
>>>> Hi Ed
>>>>
>>>> If they're really that small, this should work:
>>>>
>>>> fslmaths mask.nii -kernel 3D -dilM -ero mask-holefilled
>>>>
>>>> Regards
>>>>
>>>> Rolf
>>>>
>>>>
>>>> On 28 September 2011 16:44, Ed Gronenschild
>>>> <[log in to unmask]> wrote:
>>>>> Hi,
>>>>>
>>>>> I'm looking for a tool to fill holes of only 1 voxel in size
>>>>> in a binary mask.
>>>>>
>>>>> Ed
>>>>>
>>>>
>>
>>
>
|