internal static double CalculateStandardDeviationForStack(double mean,
                                                                  List <ImageCalculationInfo> imageCalculationInfoStack)
        {
            double sum        = 0;
            int    pixelCount = 0;

            for (int imageCount = 0; imageCount < imageCalculationInfoStack.Count; imageCount++)
            {
                PixelData            pixelData            = imageCalculationInfoStack[imageCount].PixelData;
                SegFrameImageGraphic segFrameImageGraphic = imageCalculationInfoStack[imageCount].SegFrameImageGraphic;
                RectangleF           roiBoundingBox       = imageCalculationInfoStack[imageCount].RoiBoundingBox;
                IModalityLut         modalityLut          = imageCalculationInfoStack[imageCount].ModalityLut;

                Rectangle boundingBox =
                    RectangleUtilities.RoundInflate(RectangleUtilities.ConvertToPositiveRectangle(roiBoundingBox));

                int left = boundingBox.Left;
                if (left < 0)
                {
                    left = 0;
                }
                if (left >= segFrameImageGraphic.Columns)
                {
                    left = segFrameImageGraphic.Columns - 1;
                }
                int right = boundingBox.Right;
                if (right < 0)
                {
                    right = 0;
                }
                if (right >= segFrameImageGraphic.Columns)
                {
                    right = segFrameImageGraphic.Columns - 1;
                }
                int top = boundingBox.Top;
                if (top < 0)
                {
                    top = 0;
                }
                if (top >= segFrameImageGraphic.Rows)
                {
                    top = segFrameImageGraphic.Rows - 1;
                }
                int bottom = boundingBox.Bottom;
                if (bottom < 0)
                {
                    bottom = 0;
                }
                if (bottom >= segFrameImageGraphic.Rows)
                {
                    bottom = segFrameImageGraphic.Rows - 1;
                }

                pixelData.ForEachPixel(
                    left,
                    top,
                    right,
                    bottom,
                    delegate(int i, int x, int y, int pixelIndex)
                {
                    if (segFrameImageGraphic[x, y])
                    {
                        ++pixelCount;
                        int storedValue  = pixelData.GetPixel(pixelIndex);
                        double realValue = modalityLut != null ? modalityLut[storedValue] : storedValue;

                        double deviation = realValue - mean;
                        sum += deviation * deviation;
                    }
                });
            }

            if (pixelCount == 0)
            {
                return(0);
            }

            return(Math.Sqrt(sum / pixelCount));
        }
        internal static double CalculateMeanForStack(List <ImageCalculationInfo> imageCalculationInfoStack)
        {
            double sum        = 0;
            int    pixelCount = 0;

            for (int imageCount = 0; imageCount < imageCalculationInfoStack.Count; imageCount++)
            {
                PixelData            pixelData            = imageCalculationInfoStack[imageCount].PixelData;
                SegFrameImageGraphic segFrameImageGraphic = imageCalculationInfoStack[imageCount].SegFrameImageGraphic;
                RectangleF           roiBoundingBox       = imageCalculationInfoStack[imageCount].RoiBoundingBox;
                IModalityLut         modalityLut          = imageCalculationInfoStack[imageCount].ModalityLut;

                Rectangle boundingBox =
                    RectangleUtilities.RoundInflate(RectangleUtilities.ConvertToPositiveRectangle(roiBoundingBox));

                int left = boundingBox.Left;
                if (left < 0)
                {
                    left = 0;
                }
                if (left >= segFrameImageGraphic.Columns)
                {
                    left = segFrameImageGraphic.Columns - 1;
                }
                int right = boundingBox.Right;
                if (right < 0)
                {
                    right = 0;
                }
                if (right >= segFrameImageGraphic.Columns)
                {
                    right = segFrameImageGraphic.Columns - 1;
                }
                int top = boundingBox.Top;
                if (top < 0)
                {
                    top = 0;
                }
                if (top >= segFrameImageGraphic.Rows)
                {
                    top = segFrameImageGraphic.Rows - 1;
                }
                int bottom = boundingBox.Bottom;
                if (bottom < 0)
                {
                    bottom = 0;
                }
                if (bottom >= segFrameImageGraphic.Rows)
                {
                    bottom = segFrameImageGraphic.Rows - 1;
                }

                pixelData.ForEachPixel(
                    left,
                    top,
                    right,
                    bottom,
                    delegate(int i, int x, int y, int pixelIndex)
                {
                    //if (x >= 0 && x < segFrameImageGraphic.Columns && y >= 0 && y < segFrameImageGraphic.Rows)
                    if (segFrameImageGraphic[x, y])
                    {
                        ++pixelCount;
                        // Make sure we run the raw pixel through the modality LUT
                        // when doing the calculation. Note that the modality LUT
                        // can be something other than a rescale intercept, so we can't
                        // just run the mean through the LUT.
                        int storedValue  = pixelData.GetPixel(pixelIndex);
                        double realValue = modalityLut != null ? modalityLut[storedValue] : storedValue;
                        sum += realValue;
                    }
                });
            }

            if (pixelCount == 0)
            {
                return(0);
            }

            return(sum / pixelCount);
        }