private static void FinalizeFrame(Tomogram tom) { int numberOfBackgroundClasses = tom.DataClasses.Where(n => n != 0).Count(); float[] classKey = new float[numberOfBackgroundClasses]; for (int c = 0; c < classKey.Length; c++) { float v = (float)RandomNormal.Next(tom.Random, 85, 15); classKey[c] = v * tom.MRCScaler; } for (int y = 0, i = 0; y < tom.Height; y++) { for (int x = 0; x < tom.Width; x++, i++) { int classNumber = tom.DataClasses[i]; if (classNumber > 0 && classNumber <= tom.BackgroundDensity) { tom.Data[i] = classKey[classNumber]; } } } GaussianBlur blur = GaussianBlur.BuildBlur(2.0f, 4); tom.Data = blur.BlurData(tom.Data, tom.Width, tom.Height); for (int y = 0, i = 0; y < tom.Height; y++) { for (int x = 0; x < tom.Width; x++, i++) { int classNumber = tom.DataClasses[i]; if (classNumber == -1) { float v = tom.Random.Next(50, 60); tom.Data[i] = v * tom.MRCScaler; } } } tom.Data = blur.BlurData(tom.Data, tom.Width, tom.Height); }
private static void FinalizeFrame(Tomogram tom, Random rand, MRCFile file, string serializedSamplerPath) { float minValue = file.MinPixelValue; float maxValue = file.MaxPixelValue; tom.MRCScaler = 255.0f / (maxValue - minValue); tom.MinimumTomogramValue = minValue; int[] classes = tom.DataClasses.Where(n => n != -1).Distinct().ToArray(); Dictionary <int, float> classValues = new Dictionary <int, float>(); for (int c = 0; c < classes.Length; c++) { MRCFrame frame = file.Frames[145];// rand.Next(0, file.Frames.Count - 1)]; int randomX = rand.Next(511, 611); int randomY = rand.Next(292, 392); int randomIndex = randomY * frame.Width + randomX; float value = frame.Data[randomIndex]; classValues.Add(classes[c], value); } for (int y = 0, i = 0; y < tom.Height; y++) { for (int x = 0; x < tom.Width; x++, i++) { int classNumber = tom.DataClasses[i]; if (classNumber >= 0) { tom.Data[i] = classValues[classNumber]; } } } List <float> distribution = null; BinaryFormatter bf = new BinaryFormatter(); using (FileStream fin = File.OpenRead(serializedSamplerPath)) { distribution = bf.Deserialize(fin) as List <float>; } int[] mask = new int[tom.Width * tom.Height]; for (int y = 0, i = 0; y < tom.Height; y++) { for (int x = 0; x < tom.Width; x++, i++) { int classNumber = tom.DataClasses[i]; if (classNumber == -1) { tom.Data[i] = distribution[rand.Next(0, distribution.Count - 1)]; mask[i] = 0; } else { mask[i] = 1; } } } // tom.Data = blur.BlurData(tom.Data, tom.Width, tom.Height); GaussianBlur blur = GaussianBlur.BuildBlur(.65f, tom.BlurRadius); tom.Data = blur.SelectivelyBlurData(tom.Data, mask, tom.Width, tom.Height, 1f, rand); float[] finalData = new float[tom.FinalHeight * tom.FinalWidth]; int[] finalDataClasses = new int[tom.FinalHeight * tom.FinalWidth]; for (int y = 0, dstIndex = 0; y < tom.FinalHeight; y++) { for (int x = 0; x < tom.FinalHeight; x++, dstIndex++) { int srcIndex = (y + tom.BlurRadius) * tom.Width + (x + tom.BlurRadius); finalData[dstIndex] = tom.Data[srcIndex]; finalDataClasses[dstIndex] = tom.DataClasses[srcIndex]; } } // hack. clean this up. tom.Data = finalData; tom.DataClasses = finalDataClasses; tom.Width = tom.FinalWidth; tom.Height = tom.FinalHeight; }