Skip to content

Commit

Permalink
BUG: Address race condition in SLIC filter
Browse files Browse the repository at this point in the history
Addresses issue InsightSoftwareConsortium#4711.

When RelabelConnectedRegion is called by ThreadedConnectivity, both
outputLabel and requeredLabel are the same value. Don't re-write the
same label value and check the unmodifeld label image first.

Also consolidated duplicate code into a loop.
  • Loading branch information
blowekamp authored and hjmjohnson committed Jun 21, 2024
1 parent 1f4fad1 commit bbf9f17
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions Modules/Segmentation/SuperPixel/include/itkSLICImageFilter.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -802,20 +802,19 @@ SLICImageFilter<TInputImage, TOutputImage, TDistancePixel>::RelabelConnectedRegi
labelIt.SetLocation(idx);
for (unsigned int j = 0; j < ImageDimension; ++j)
{
unsigned int nIdx = center + stride[j];

if (markerIter.GetPixel(nIdx) == 0 && labelIt.GetPixel(nIdx) == requiredLabel)
{
indexStack.push_back(labelIt.GetIndex(nIdx));
markerIter.SetPixel(nIdx, 1);
labelIt.SetPixel(nIdx, outputLabel);
}
nIdx = center - stride[j];
if (markerIter.GetPixel(nIdx) == 0 && labelIt.GetPixel(nIdx) == requiredLabel)
for (const auto &nIdx : { center + stride[j], center - stride[j] })
{
indexStack.push_back(labelIt.GetIndex(nIdx));
markerIter.SetPixel(nIdx, 1);
labelIt.SetPixel(nIdx, outputLabel);
// When run in threaded mode, requiredLabel is the same as outputLabel and only the marker images is modified.
// The label image must be checked first to avoid race conditions with the marker image.
if (labelIt.GetPixel(nIdx) == requiredLabel && markerIter.GetPixel(nIdx) == 0 )
{
indexStack.push_back(labelIt.GetIndex(nIdx));
markerIter.SetPixel(nIdx, 1);
if (requiredLabel != outputLabel)
{
labelIt.SetPixel(nIdx, outputLabel);
}
}
}
}
}
Expand Down

0 comments on commit bbf9f17

Please sign in to comment.