LabelIntensityStatisticsImageFilter error

I have tried the following code:

sitk::Image imgTemp(img);
sitk::Image cc = sitk::ConnectedComponent(imgTemp, true);
sitk::LabelIntensityStatisticsImageFilter statistics;
statistics.Execute(cc, imgTemp);
std::vector<int64_t> labels = statistics.GetLabels();
for (int64_t i = 0; i < (int64_t)labels.size(); ++i)
{
	statistics.GetMean(i); // <-- error thrown
}

img is of course, sitk::Image object. But when statistics.GetMean(i); is called, it throw in the following error:

itkExceptionMacro(<< "Label " << static_cast<typename NumericTraits<LabelType>::PrintType>(label)
                  << " is the background label.");

in file:
d:\Project\SimpleITK\SuperBuild\bin\ITK\Modules\Filtering\LabelMap\include\itkLabelMap.hxx

What I have done wrong ? Can you help me a little bit ?

Regards,
Flaviu.

Including the actual error message with the value of label would be helpful.

You are using the index I not the value in the labels array.

Here is a C++ for range loop to iterate over the available labels:

for (auto label: statistics.GetLabels())
{
  std::cout << "Label: " << label << " Mean: " << statistics.GetMean(label) << std::end;
} 

I am trying to loop without auto specifier.

Here is my testing code:

	std::vector<int64_t>::iterator it;
	std::vector<int64_t> labels = statistics.GetLabels();
	for (it = labels.begin(); it != labels.end(); ++it)
	{
		TRACE(_T("Label: {%lu} -> Mean: {%f} Size: {%f}\n"), 
			*it, statistics.GetMean(*it), statistics.GetPhysicalSize(*it));
	}

I know, it is more ugly code, but for, is more transparent :slight_smile:

Here is the result:

Label: {1} -> Mean: {1049.634854} Size: {179605.944000}

@blowekamp I have run this code on a CBCT dicom (a mandibula). And I’ve ran this because I intend to use it to know how to segment a bone from this vtkVolume … how can I use this data from LabelIntensityStatisticsImageFilter to remove a bone from this volume ?

The answer to your original question is to start from 1, not 0:
for (int64_t i = 1; ...

One way of “removing” the bone (assuming label 1 is bone) is to mask it away. First apply Not filter, then Mask filter.

With following code:

	sitk::Image cc = sitk::SLIC(imgTemp);
	sitk::LabelIntensityStatisticsImageFilter statistics;
	statistics.Execute(cc, imgTemp);
	std::vector<int64_t>::iterator it;
	std::vector<int64_t> labels = statistics.GetLabels();
	for (it = labels.begin(); it != labels.end(); ++it)
	{
		TRACE(_T("Label: {%lu} -> Mean: {%f} Size: {%f}\n"), 
			*it, statistics.GetMean(*it), statistics.GetPhysicalSize(*it));
	}

I got:

Label: {1} -> Mean: {368.290207} Size: {566.464000}
Label: {2} -> Mean: {2526.748027} Size: {1077.480000}
Label: {3} -> Mean: {1322.812512} Size: {2101.808000}
Label: {4} -> Mean: {5.314739} Size: {1655.520000}
Label: {5} -> Mean: {1152.650079} Size: {2021.784000}
Label: {7} -> Mean: {1261.300340} Size: {1532.264000}
Label: {8} -> Mean: {0.115451} Size: {1778.208000}
Label: {9} -> Mean: {1447.813594} Size: {2024.352000}
Label: {10} -> Mean: {5568.465393} Size: {298.552000}
Label: {11} -> Mean: {241.681532} Size: {711.432000}
Label: {12} -> Mean: {1184.535699} Size: {2184.616000}
Label: {13} -> Mean: {1004.544586} Size: {1606.856000}
Label: {14} -> Mean: {1537.574133} Size: {1212.360000}
Label: {16} -> Mean: {1125.041260} Size: {1670.584000}
Label: {17} -> Mean: {909.822657} Size: {1916.560000}
Label: {18} -> Mean: {571.987740} Size: {1087.808000}
Label: {19} -> Mean: {2970.865338} Size: {569.248000}
Label: {20} -> Mean: {2112.483121} Size: {948.616000}
Label: {21} -> Mean: {855.563037} Size: {1174.800000}
Label: {22} -> Mean: {279.319568} Size: {668.728000}
Label: {23} -> Mean: {796.453627} Size: {1563.768000}
Label: {25} -> Mean: {783.731848} Size: {2054.328000}
Label: {26} -> Mean: {236.623219} Size: {592.832000}
Label: {27} -> Mean: {578.980189} Size: {1630.184000}
Label: {28} -> Mean: {1074.661212} Size: {2048.784000}
Label: {29} -> Mean: {471.110132} Size: {728.288000}
Label: {30} -> Mean: {33.103330} Size: {496.504000}
Label: {31} -> Mean: {582.654392} Size: {1093.424000}
Label: {32} -> Mean: {3004.682777} Size: {1008.576000}
Label: {33} -> Mean: {1502.536099} Size: {599.576000}
Label: {34} -> Mean: {932.294998} Size: {1759.440000}
Label: {35} -> Mean: {672.321851} Size: {1433.184000}
Label: {36} -> Mean: {1.081547} Size: {1344.888000}
Label: {38} -> Mean: {1601.040743} Size: {846.872000}
Label: {39} -> Mean: {154.552404} Size: {489.584000}
Label: {41} -> Mean: {616.459449} Size: {972.712000}
Label: {42} -> Mean: {1158.360874} Size: {1430.040000}
Label: {43} -> Mean: {420.245586} Size: {973.768000}
Label: {44} -> Mean: {0.013475} Size: {1269.888000}
Label: {45} -> Mean: {0.002492} Size: {1804.240000}
Label: {46} -> Mean: {3.379783} Size: {1379.608000}
Label: {47} -> Mean: {595.509167} Size: {1584.880000}
Label: {48} -> Mean: {876.348265} Size: {1709.616000}
Label: {49} -> Mean: {741.060466} Size: {1919.760000}
Label: {50} -> Mean: {7.467693} Size: {409.072000}
Label: {51} -> Mean: {888.895067} Size: {1505.272000}
Label: {52} -> Mean: {5.131299} Size: {834.432000}
Label: {53} -> Mean: {0.000000} Size: {1877.896000}
Label: {54} -> Mean: {0.000000} Size: {1603.504000}
Label: {55} -> Mean: {0.000000} Size: {1781.232000}
Label: {56} -> Mean: {2.317372} Size: {1365.440000}
Label: {57} -> Mean: {539.526680} Size: {1343.616000}
Label: {58} -> Mean: {0.032741} Size: {493.568000}
Label: {59} -> Mean: {603.087515} Size: {1825.872000}
Label: {60} -> Mean: {1.179970} Size: {1170.104000}
Label: {61} -> Mean: {0.000000} Size: {1949.160000}
Label: {62} -> Mean: {0.000000} Size: {1583.984000}
Label: {63} -> Mean: {6.384759} Size: {1738.128000}
Label: {64} -> Mean: {968.834762} Size: {946.952000}
Label: {65} -> Mean: {1173.982044} Size: {2118.912000}
Label: {66} -> Mean: {1282.010115} Size: {1144.464000}
Label: {67} -> Mean: {2.750152} Size: {1684.992000}
Label: {68} -> Mean: {1828.256807} Size: {1617.560000}
Label: {69} -> Mean: {744.446380} Size: {1679.000000}
Label: {70} -> Mean: {2429.636374} Size: {1005.144000}
Label: {71} -> Mean: {0.000000} Size: {1710.064000}
Label: {72} -> Mean: {1037.654082} Size: {1918.584000}
Label: {73} -> Mean: {2335.705597} Size: {1238.848000}
Label: {74} -> Mean: {1872.602247} Size: {1250.224000}
Label: {76} -> Mean: {1251.685430} Size: {2535.880000}
Label: {77} -> Mean: {1729.013019} Size: {752.720000}
Label: {79} -> Mean: {1082.833144} Size: {2276.024000}
Label: {80} -> Mean: {701.851550} Size: {1716.024000}
Label: {81} -> Mean: {669.559224} Size: {2075.304000}
Label: {82} -> Mean: {1288.527396} Size: {1615.408000}
Label: {83} -> Mean: {1477.291494} Size: {1083.904000}
Label: {85} -> Mean: {1082.230514} Size: {1657.272000}
Label: {86} -> Mean: {1118.916965} Size: {1926.704000}
Label: {87} -> Mean: {1906.287068} Size: {786.880000}
Label: {88} -> Mean: {1000.200725} Size: {1945.192000}
Label: {90} -> Mean: {477.780360} Size: {794.464000}
Label: {91} -> Mean: {824.415658} Size: {2400.936000}
Label: {92} -> Mean: {962.614150} Size: {1597.824000}
Label: {93} -> Mean: {2633.126717} Size: {952.040000}
Label: {94} -> Mean: {1651.431359} Size: {915.784000}
Label: {95} -> Mean: {2376.183570} Size: {1473.704000}
Label: {96} -> Mean: {738.362924} Size: {1150.544000}
Label: {97} -> Mean: {604.685956} Size: {1862.136000}
Label: {98} -> Mean: {633.751769} Size: {2076.648000}
Label: {99} -> Mean: {1.038663} Size: {1394.008000}
Label: {101} -> Mean: {716.292855} Size: {1208.872000}
Label: {102} -> Mean: {731.980081} Size: {1546.664000}
Label: {104} -> Mean: {671.521394} Size: {1091.872000}
Label: {107} -> Mean: {0.000000} Size: {1529.456000}
Label: {108} -> Mean: {0.000000} Size: {1536.248000}
Label: {109} -> Mean: {3.321768} Size: {1747.816000}
Label: {110} -> Mean: {571.734155} Size: {993.272000}
Label: {113} -> Mean: {632.352636} Size: {1812.792000}
Label: {114} -> Mean: {680.077052} Size: {2278.672000}
Label: {115} -> Mean: {0.000000} Size: {1759.840000}
Label: {116} -> Mean: {0.000000} Size: {1483.624000}
Label: {117} -> Mean: {0.000000} Size: {1526.600000}
Label: {118} -> Mean: {0.000000} Size: {1561.400000}
Label: {119} -> Mean: {4.027392} Size: {1361.552000}
Label: {120} -> Mean: {609.241966} Size: {1692.272000}
Label: {121} -> Mean: {691.843311} Size: {1788.968000}
Label: {122} -> Mean: {749.687721} Size: {1979.048000}
Label: {123} -> Mean: {0.000000} Size: {1314.408000}
Label: {124} -> Mean: {0.000000} Size: {1525.384000}
Label: {125} -> Mean: {0.000000} Size: {1546.360000}
Label: {126} -> Mean: {4.117650} Size: {1808.216000}
Label: {127} -> Mean: {1164.579878} Size: {2739.072000}
Label: {128} -> Mean: {0.603684} Size: {335.328000}
Label: {129} -> Mean: {1146.404821} Size: {2498.248000}
Label: {130} -> Mean: {44.142602} Size: {1071.960000}
Label: {131} -> Mean: {1149.141517} Size: {2929.448000}
Label: {132} -> Mean: {0.070144} Size: {799.960000}
Label: {133} -> Mean: {1526.197683} Size: {1364.080000}
Label: {134} -> Mean: {0.000000} Size: {1890.400000}
Label: {135} -> Mean: {812.220559} Size: {1869.104000}
Label: {136} -> Mean: {2164.437493} Size: {1284.920000}
Label: {137} -> Mean: {0.008543} Size: {338.992000}
Label: {138} -> Mean: {1099.524489} Size: {2371.176000}
Label: {139} -> Mean: {1139.302470} Size: {3310.896000}
Label: {142} -> Mean: {931.524617} Size: {1739.600000}
Label: {144} -> Mean: {299.822941} Size: {323.824000}
Label: {146} -> Mean: {1770.032962} Size: {860.392000}
Label: {148} -> Mean: {36.471044} Size: {491.504000}
Label: {149} -> Mean: {1128.391914} Size: {1520.272000}
Label: {150} -> Mean: {2217.467941} Size: {864.656000}
Label: {152} -> Mean: {720.425645} Size: {2535.632000}
Label: {153} -> Mean: {4.892183} Size: {374.784000}
Label: {154} -> Mean: {594.911283} Size: {2620.728000}
Label: {155} -> Mean: {786.821474} Size: {1048.048000}
Label: {156} -> Mean: {2235.781010} Size: {1053.600000}
Label: {157} -> Mean: {1245.490110} Size: {1402.608000}
Label: {158} -> Mean: {2509.458932} Size: {603.488000}
Label: {159} -> Mean: {871.754826} Size: {1427.592000}
Label: {160} -> Mean: {9.758531} Size: {488.808000}
Label: {161} -> Mean: {0.000000} Size: {389.744000}
Label: {162} -> Mean: {0.400833} Size: {1269.256000}
Label: {163} -> Mean: {678.296187} Size: {1754.432000}
Label: {164} -> Mean: {3.292190} Size: {630.904000}
Label: {166} -> Mean: {881.339035} Size: {1436.760000}
Label: {168} -> Mean: {571.658143} Size: {1437.184000}
Label: {170} -> Mean: {68.545898} Size: {635.328000}
Label: {171} -> Mean: {0.000000} Size: {1653.208000}
Label: {172} -> Mean: {0.951615} Size: {1614.712000}
Label: {173} -> Mean: {617.069781} Size: {1543.680000}
Label: {175} -> Mean: {765.048832} Size: {2217.896000}
Label: {177} -> Mean: {688.089716} Size: {1644.648000}
Label: {178} -> Mean: {0.532532} Size: {1554.296000}
Label: {179} -> Mean: {0.000000} Size: {2098.216000}
Label: {180} -> Mean: {0.000000} Size: {1554.752000}
Label: {181} -> Mean: {0.000000} Size: {1639.272000}
Label: {182} -> Mean: {0.494761} Size: {1355.096000}
Label: {183} -> Mean: {671.728335} Size: {2137.576000}
Label: {184} -> Mean: {2.433458} Size: {332.840000}
Label: {185} -> Mean: {644.002123} Size: {1303.624000}
Label: {186} -> Mean: {0.000000} Size: {1262.864000}
Label: {187} -> Mean: {0.000000} Size: {1661.632000}
Label: {188} -> Mean: {0.000000} Size: {1576.472000}
Label: {189} -> Mean: {2.432324} Size: {509.544000}
Label: {190} -> Mean: {418.095877} Size: {678.200000}
Label: {191} -> Mean: {383.437455} Size: {650.160000}
Label: {192} -> Mean: {2221.795102} Size: {769.240000}
Label: {193} -> Mean: {1093.841956} Size: {1537.240000}
Label: {194} -> Mean: {1040.406918} Size: {1287.120000}
Label: {195} -> Mean: {785.872931} Size: {1179.456000}
Label: {196} -> Mean: {673.430588} Size: {2443.392000}
Label: {197} -> Mean: {712.819455} Size: {1407.872000}
Label: {198} -> Mean: {1222.519153} Size: {2343.664000}
Label: {199} -> Mean: {490.345090} Size: {638.280000}
Label: {200} -> Mean: {475.100847} Size: {662.704000}
Label: {201} -> Mean: {496.719702} Size: {1401.480000}
Label: {202} -> Mean: {696.450730} Size: {1590.840000}
Label: {203} -> Mean: {1109.066118} Size: {1179.352000}
Label: {204} -> Mean: {2424.302509} Size: {389.040000}
Label: {205} -> Mean: {661.414440} Size: {2436.288000}
Label: {206} -> Mean: {742.483240} Size: {1631.552000}
Label: {207} -> Mean: {923.733392} Size: {537.688000}
Label: {208} -> Mean: {1080.068644} Size: {410.816000}
Label: {209} -> Mean: {643.469645} Size: {258.800000}

So what you have are super-pixels, and that is only the first step in some ways to do bone segmentation, which is what you want. In order to “remove” the bone, you need to segment it first. And image segmentation, in general form, is an unsolved problem. Perhaps start a new topic with the description of your problem and constraints, if you need some advice.

Yes, you had right, I figured out after I trace the variables from statistics.GetLabels().

" One way of “removing” the bone (assuming label 1 is bone) is to mask it away. First apply Not filter, then Mask filter."

You gave me a hope :slight_smile: I hope I make it. Perhaps I’ll address some questions here …