2323
2424#include < algorithm> // For generate.
2525#include < iostream>
26+ #include < random> // For mt19937.
2627
2728// type alias for all functions
2829using PixelType = itk::RGBPixel<unsigned char >;
30+ using PixelComponentType = PixelType::ComponentType;
2931using ImageType = itk::Image<PixelType, 2 >;
3032using SegmentationType = itk::Image<unsigned char , 2 >;
3133using ReaderType = itk::ImageFileReader<ImageType>;
@@ -69,19 +71,25 @@ SetUpInputImage()
6971 inputImage->SetRegions (region);
7072 inputImage->Allocate ();
7173
74+ std::mt19937 randomNumberEngine{};
75+
7276 // add background random field
77+ std::uniform_int_distribution<> backgroundRandomNumberDistribution (bgMean - bgStd, bgMean + bgStd);
78+
7379 itk::ImageRegionIterator<ImageType> iter (inputImage, region);
7480 while (!iter.IsAtEnd ())
7581 {
7682 PixelType px;
77- std::generate (px.begin (), px.end (), [] {
78- return static_cast <unsigned char >( vnl_sample_uniform (bgMean - bgStd, bgMean + bgStd ));
83+ std::generate (px.begin (), px.end (), [&randomNumberEngine, &backgroundRandomNumberDistribution ] {
84+ return static_cast <PixelComponentType>( backgroundRandomNumberDistribution (randomNumberEngine ));
7985 });
8086 iter.Set (px);
8187 ++iter;
8288 }
8389
8490 // add objects to image
91+ std::uniform_int_distribution<> forgroundRandomNumberDistribution (fgMean - fgStd, fgMean + fgStd);
92+
8593 for (unsigned int x = objAStartX; x < objAEndX; ++x)
8694 {
8795 for (unsigned int y = objAStartY; y < objAEndY; ++y)
@@ -91,8 +99,8 @@ SetUpInputImage()
9199 idx[1 ] = y;
92100
93101 PixelType px;
94- std::generate (px.begin (), px.end (), [] {
95- return static_cast <unsigned char >( vnl_sample_uniform (fgMean - fgStd, fgMean + fgStd ));
102+ std::generate (px.begin (), px.end (), [&randomNumberEngine, &forgroundRandomNumberDistribution ] {
103+ return static_cast <PixelComponentType>( forgroundRandomNumberDistribution (randomNumberEngine ));
96104 });
97105 inputImage->SetPixel (idx, px);
98106 }
@@ -106,8 +114,8 @@ SetUpInputImage()
106114 idx[1 ] = y;
107115
108116 PixelType px;
109- std::generate (px.begin (), px.end (), [] {
110- return static_cast <unsigned char >( vnl_sample_uniform (fgMean - fgStd, fgMean + fgStd ));
117+ std::generate (px.begin (), px.end (), [&randomNumberEngine, &forgroundRandomNumberDistribution ] {
118+ return static_cast <PixelComponentType>( forgroundRandomNumberDistribution (randomNumberEngine ));
111119 });
112120 inputImage->SetPixel (idx, px);
113121 }
0 commit comments