public static void MorphologicalDilationRBA <RangeType>(ImageRaster3D <RangeType> source, StructuringElement3D structure, RangeType default_value, ImageRaster3D <RangeType> target) where RangeType : IComparable <RangeType> { if (!source.Raster.Equals(target.Raster)) { throw new Exception("Raster Mismatch"); } IRaster3DInteger raster = source.Raster; //Parallel.For(0, source.ElementCount, source_element_index => for (int source_element_index = 0; source_element_index < source.ElementCount; source_element_index++) { int[] coordinates = raster.GetElementCoordinates(source_element_index); RangeType value = default_value; bool found = false; for (int offset_index = 0; offset_index < structure.FlippedOffsets.Count; offset_index++) { int[] offset = structure.FlippedOffsets[offset_index]; if (raster.ContainsCoordinates(coordinates[0] + offset[0], coordinates[1] + offset[1], coordinates[2] + offset[2])) { if (found) { value = ToolsMath.Max(value, source.GetElementValue(coordinates[0] + offset[0], coordinates[1] + offset[1], coordinates[2] + offset[2])); } else { value = source.GetElementValue(coordinates[0] + offset[0], coordinates[1] + offset[1], coordinates[2] + offset[2]); found = true; } } } target.SetElementValue(source_element_index, value); }//); }
public static double TestStatic(IList <IList <double> > samples) { double sample_count = samples.Count; double total_count = ToolsCollection.CountElements(samples); double total_mean = ToolsMathStatistics.MeanAll(samples); IList <double> sample_means = ToolsMathStatistics.Means0(samples); double sstr = 0.0; for (int sample_index = 0; sample_index < samples.Count; sample_index++) { sstr += samples[sample_index].Count * ToolsMath.Sqr(sample_means[sample_index] - total_mean); } double sse = 0.0; for (int sample_index = 0; sample_index < samples.Count; sample_index++) { for (int measurement_index = 0; measurement_index < samples[sample_index].Count; measurement_index++) { sse += ToolsMath.Sqr(samples[sample_index][measurement_index] - sample_means[sample_index]); } } //FTransform double degrees_of_freedom_0 = (sample_count - 1.0); double degrees_of_freedom_1 = (total_count - sample_count); double summed_variance = sstr / degrees_of_freedom_0; double total_varaiance = sse / degrees_of_freedom_1; double f_statistic = summed_variance / total_varaiance; return(FisherSnedecor.CDF(degrees_of_freedom_0, degrees_of_freedom_1, f_statistic)); }
//TODO implement 3 mean variants //http://www.itl.nist.gov/div898/handbook/eda/section3/eda35a.htm public static double TestStatic(IList <IList <double> > samples) { double total_count = ToolsCollection.CountElements(samples); double total_mean = ToolsMathStatistics.MeanAll(samples); //double[] sample_means = ToolsMathStatistics.Means0(samples); //double total_mean = ToolsMathStatistics.MedianAll(samples); double[] sample_means = ToolsMathStatistics.Medians0(samples); double summed_varriance = 0.0; for (int sample_index = 0; sample_index < samples.Count; sample_index++) { summed_varriance += samples[sample_index].Count * ToolsMath.Sqr(sample_means[sample_index] - total_mean); } double total_variance = 0.0; for (int sample_index = 0; sample_index < samples.Count; sample_index++) { for (int measurement_index = 0; measurement_index < samples[sample_index].Count; measurement_index++) { total_variance += ToolsMath.Sqr(samples[sample_index][measurement_index] - sample_means[sample_index]); } } double degrees_of_freedom_0 = samples.Count - 1; double degrees_of_freedom_1 = total_count - samples.Count; double f_statistic = (degrees_of_freedom_1 * summed_varriance) / (degrees_of_freedom_0 * total_variance); return(FisherSnedecor.CDF(degrees_of_freedom_0, degrees_of_freedom_1, f_statistic)); }
public static Tuple <double, double> TestStatic(IList <IList <double> > samples) { int sample_size = samples[0].Count; foreach (IList <double> sample in samples) { if (sample.Count != sample_size) { throw new Exception("samples must be of equal size"); } } // Larsen Marx 4Th editiopn P779 double sample_count = samples.Count; double total_count = ToolsCollection.CountElements(samples); double total_mean = ToolsMathStatistics.MeanAll(samples); double sum_squared_all = ToolsMathStatistics.SumSquaredAll(samples); IList <double> sample_sums = ToolsMathCollection.Sums0(samples); IList <double> measurement_sums = ToolsMathCollection.Sums1(samples); // compute C double c = ToolsMath.Sqr(total_mean * total_count) / (sample_size * sample_count); double sstot = sum_squared_all - c; double ssb = 0; for (int measurement_index = 0; measurement_index < sample_size; measurement_index++) { ssb += ToolsMath.Sqr(measurement_sums[measurement_index]) / sample_count; } ssb -= c; double sstr = 0.0; for (int sample_index = 0; sample_index < samples.Count; sample_index++) { sstr += ToolsMath.Sqr(sample_sums[sample_index]) / sample_size; } sstr -= c; double sse = sstot - ssb - sstr; double degrees_of_freedom_0_samples = (sample_count - 1.0); double degrees_of_freedom_0_measurements = (sample_size - 1.0); double degrees_of_freedom_1 = degrees_of_freedom_0_samples * degrees_of_freedom_0_measurements; //F-Transform samples double f_statistic_samples = (sstr / degrees_of_freedom_0_samples) / (sse / degrees_of_freedom_1); //F-Transform measurements double f_statistic_measurements = (ssb / degrees_of_freedom_0_measurements) / (sse / degrees_of_freedom_1); return(new Tuple <double, double>( FisherSnedecor.CDF(degrees_of_freedom_0_samples, degrees_of_freedom_1, f_statistic_samples), FisherSnedecor.CDF(degrees_of_freedom_0_measurements, degrees_of_freedom_1, f_statistic_measurements))); }
public static double TestStatic(IList <IList <double> > samples) { int sample_size = samples[0].Count; foreach (IList <double> sample in samples) { if (sample.Count != sample_size) { throw new Exception("samples must be of equal size"); } } double[,] measurement_ranks = ToolsMathStatistics.Ranks1(samples); double[] rank_sums = ToolsMathCollection.Sums0(measurement_ranks); double chi_square_statistic_part = 0.0; for (int treatment_index = 0; treatment_index < rank_sums.Length; treatment_index++) { chi_square_statistic_part += ToolsMath.Sqr(rank_sums[treatment_index]); } double treatment_count = samples.Count; double block_count = sample_size; double chi_square_statistic = ((12.0 / (block_count * treatment_count * (treatment_count + 1.0))) * chi_square_statistic_part) - (3 * block_count * (treatment_count + 1.0)); return(ChiSquared.CDF(rank_sums.Length, chi_square_statistic)); }
//Select a sample from a range of images and organise it into and array public RangeType[][] Sample(IList <IImageRaster <IRaster4DInteger, RangeType> > images, IImageRaster <IRaster4DInteger, bool> mask) { List <int> selected_indexes = mask.GetElementIndexesWithValue(true); if (this.random) { ToolsMathCollection.ShuffleIP(selected_indexes); } int sample_size = count; if (!this.fixed_count) { sample_size = (int)(selected_indexes.Count * fraction); } RangeType[][] sample = new RangeType[this.count][]; for (int sample_index = 0; sample_index < this.count; sample_index++) { sample[sample_index] = new RangeType[images.Count]; for (int feature_index = 0; feature_index < images.Count; feature_index++) { int sample_value_index = selected_indexes[(int)((selected_indexes.Count / ((double)sample_size)) * sample_index)]; sample_value_index = ToolsMath.Clamp(sample_value_index, 0, selected_indexes.Count - 1); sample[sample_index][feature_index] = images[feature_index].GetElementValue(sample_value_index); } } return(sample); }
public DataType this[int index] { get { return(inner_list[ToolsMath.Wrap(index, inner_list.Count)]); } set { inner_list[ToolsMath.Wrap(index, inner_list.Count)] = value; } }
public void TestDistance3DMedium() { ImageRaster3D <bool> mask = CreateTestMask3D0(); ImageRaster3D <float> distance = new ImageRaster3D <float>(mask.Raster); ToolsDistance.DistanceTransform3DMediumRBA(mask, new float[] { 1.0f, 1.0f, 1.0f }, distance); Assert.AreEqual(0.0f, distance[0]); Assert.AreEqual(1.0f, distance[1]); Assert.AreEqual(ToolsMath.Sqrt(2.0f), distance[2]); Assert.AreEqual(2.0f, distance[3]); Assert.AreEqual(ToolsMath.Sqrt(5.0f), distance[4]); Assert.AreEqual(ToolsMath.Sqrt(8.0f), distance[20]); Assert.AreEqual(ToolsMath.Sqrt(24.0f), distance[120]); Assert.AreEqual(ToolsMath.Sqrt(17.0f), distance[124]); }
private void Calc_Click(object sender, EventArgs e) { try { double X = double.Parse(InputX.Text); double E = double.Parse(InputE.Text); ToolsMath toolsMath = new ToolsMath(X, E); OutputX1.Text = toolsMath.Left().ToString(); OutputX2.Text = toolsMath.Right(out int n).ToString(); OutputN.Text = n.ToString(); } catch (Exception) { MessageBox.Show("ошибка ввода", "ошибка"); } }
public void TestDistance2D() { ImageRaster3D <bool> mask = new ImageRaster3D <bool>(5, 5, 1); mask[0] = true; mask[6] = true; mask[12] = true; mask[13] = true; mask[19] = true; ImageRaster3D <float> distance = ToolsDistance.DistanceTransform3D(mask, new float[] { 1.0f, 1.0f, 1.0f }); Assert.AreEqual(0.0f, distance[0]); Assert.AreEqual(1.0f, distance[1]); Assert.AreEqual(ToolsMath.Sqrt(2.0f), distance[2]); Assert.AreEqual(2.0f, distance[3]); Assert.AreEqual(ToolsMath.Sqrt(5.0f), distance[4]); Assert.AreEqual(ToolsMath.Sqrt(8.0f), distance[20]); }
public void TestDistance2DOosterbroek() { ImageRaster2D <bool> mask = new ImageRaster2D <bool>(5, 5); mask[0] = true; mask[6] = true; mask[12] = true; mask[13] = true; mask[19] = true; ImageRaster2D <float> distance = new ImageRaster2D <float>(mask.Raster); ToolsDistance.DistanceTransform2DOosterbroekRBA(mask, new float[] { 1.0f, 1.0f, 1.0f }, distance); Assert.AreEqual(0.0f, distance[0]); Assert.AreEqual(1.0f, distance[1]); Assert.AreEqual(ToolsMath.Sqrt(2.0f), distance[2]); Assert.AreEqual(2.0f, distance[3]); Assert.AreEqual(ToolsMath.Sqrt(5.0f), distance[4]); Assert.AreEqual(ToolsMath.Sqrt(8.0f), distance[20]); }
public override bool ComputeRBA(IMarketModelIndicator market_model, double[] result) { if (previous_1 == 0) { previous_1 = market_model.CurrentBid; previous_2 = market_model.CurrentBid; } double momentum = previous_1 - previous_2; double error_now = market_model.CurrentBid - (previous_1 + (momentum_weight * momentum)); error_now_array_sqr_sum = (error_now_array_sqr_sum + ToolsMath.Sqr(error_now) - ToolsMath.Sqr(this.error_now_array[index_error_now % this.error_now_array.Length])) / this.error_now_array.Length; error_now_array_sum = error_now_array_sum + error_now - this.error_now_array[index_error_now % this.error_now_array.Length]; this.error_now_array[index_error_now % this.error_now_array.Length] = error_now; index_error_now++; double error_now_sdev = Math.Sqrt(error_now_array_sqr_sum); if (error_now_sdev == 0) { error_now_sdev = double.Epsilon; } double error_now_z = error_now / error_now_sdev; double error_now_weight = max_error_weight * (1 - Math.Exp(-ToolsMath.Sqr(error_now_z) / expected_error_z)); result[0] = previous_1 + (momentum * momentum_weight) + (error_now * error_now_weight); result[1] = result[0] + (error_now_sdev * 2); result[2] = result[0] - (error_now_sdev * 2); result[3] = error_now; result[4] = error_now_sdev; result[5] = error_now_array_sum; result[6] = error_now_z; result[7] = error_now_weight; result[8] = momentum; previous_2 = previous_1; previous_1 = result[0]; return(this.error_now_array.Length < market_model.Second1.HistoryCount); }
public override double Test(IList <double> sample_0, IList <double> sample_1) { double mean_0 = ToolsMathStatistics.Mean(sample_0); double mean_1 = ToolsMathStatistics.Mean(sample_1); double variance_0 = ToolsMathStatistics.Variance(sample_0, mean_0); double variance_1 = ToolsMathStatistics.Variance(sample_0, mean_1); double t_statistic = (mean_0 - mean_1) / Math.Sqrt((variance_0 / sample_0.Count) + (variance_1 / sample_1.Count)); //Welch–Satterthwaite equation: double dof_nominator = ToolsMath.Sqr((variance_0 / sample_0.Count) + (variance_1 / sample_1.Count)); double dof_denominator = (Math.Pow(variance_0, 4) / (sample_0.Count * sample_0.Count * (sample_0.Count - 1))) + (Math.Pow(variance_1, 4) / (sample_1.Count * sample_1.Count * (sample_1.Count - 1))); double degrees_of_freedom = dof_nominator / dof_denominator; StudentT distribution = new StudentT(0.1, 1.0, degrees_of_freedom); return(distribution.CumulativeDistribution(-t_statistic)); }
public static double TestStatic(IList <IList <double> > samples, IList <double> limits) { // Create to blocks according to limits double[,] table = new double[samples.Count, limits.Count + 1]; for (int index_0 = 0; index_0 < samples.Count; index_0++) { for (int index_1 = 0; index_1 < samples[index_0].Count; index_1++) { double value = samples[index_0][index_1]; //for any of the other bins int limit_index = 0; while ((limit_index < limits.Count) && (limits[limit_index] <= value)) { limit_index++; } table[index_0, limit_index]++; } } // Compute test for independance double[] sums_0 = ToolsMathCollection.Sums0(table); double[] sums_1 = ToolsMathCollection.Sums1(table); double total = ToolsMathCollection.Sum(sums_0); double chi_square_statistic = 0; for (int index_0 = 0; index_0 < sums_0.Length; index_0++) { for (int index_1 = 0; index_1 < sums_1.Length; index_1++) { double expectation = (sums_0[index_0] * sums_1[index_1]) / total; chi_square_statistic += (ToolsMath.Sqr(table[index_0, index_1] - expectation) / expectation); } } double degrees_of_freedom = (sums_0.Length - 1) * (sums_1.Length - 1); return(ChiSquared.CDF(degrees_of_freedom, chi_square_statistic)); }
private void calc_Click(object sender, EventArgs e) { try { double X = double.Parse(InputX.Text); if (Math.Abs(X) > 1) { throw new Exception(); } double E = double.Parse(InputE.Text); ToolsMath toolsMath = new ToolsMath(X, E); double y = toolsMath.GetLeft(); OutY1.Text = "y=" + y; y = toolsMath.GetRight(out int n); OutY2.Text = "y=" + y; OutN.Text = "n=" + n; } catch (Exception) { MessageBox.Show("ошибка ввода", "ошибка"); } }
public static void DistanceTransform2DSlowRBA(ImageRaster2D <bool> mask_image, float[] voxel_size, ImageRaster2D <float> distance_image) { IRaster2DInteger raster = mask_image.Raster; int size_0 = mask_image.Raster.Size0; int size_1 = mask_image.Raster.Size1; // Apply the transform in the x-direction //for (int plane_index = 0; plane_index < size_z * size_y; plane_index++) //{ Parallel.For(0, size_0, index_0 => { for (int index_1 = 0; index_1 < size_1; index_1++) { float best_distance = float.MaxValue; if (mask_image.GetElementValue(index_0, index_1)) { best_distance = 0; } else { for (int index_0_1 = 0; index_0_1 < size_0; index_0_1++) { for (int index_1_1 = 0; index_1_1 < size_1; index_1_1++) { if (mask_image.GetElementValue(index_0_1, index_1_1)) { float distance = ToolsMath.Sqrt(ToolsMath.Sqr((index_0_1 - index_0) * voxel_size[0]) + ToolsMath.Sqr((index_1_1 - index_1) * voxel_size[0])); if (distance < best_distance) { best_distance = distance; } } } } } distance_image.SetElementValue(index_0, index_1, best_distance); } }); }
public override bool ComputeRBA(IMarketModelIndicator market_model, double[] result) { if (previous_1 == 0) { previous_1 = market_model.CurrentBid; previous_2 = market_model.CurrentBid; } double momentum = previous_1 - previous_2; double error_now = market_model.CurrentBid - (previous_1 + (momentum_weight * momentum)); indicator_error_now[index_indicator_error_now % indicator_error_now.Length] = error_now; index_indicator_error_now++; double error_now_s = ToolsMathStatistics.StandardDeviation(indicator_error_now); if (error_now_s == 0) { error_now_s = double.Epsilon; } double error_now_z = error_now / error_now_s; double error_now_weight = max_error_weight * (1 - Math.Exp(-ToolsMath.Sqr(error_now_z) / expected_error_z)); result[0] = previous_1 + (momentum * momentum_weight) + (error_now * error_now_weight); result[1] = result[0] + (error_now_s * 2); result[2] = result[0] - (error_now_s * 2); result[3] = error_now; result[4] = error_now_s; result[5] = error_now_z; result[6] = error_now_weight; result[7] = momentum; previous_2 = previous_1; previous_1 = result[0]; return(indicator_error_now.Length < market_model.Second1.HistoryCount); }
public void TestExtendedEuclideanAlgorithm0() { Tuple <int, int, int> tuple_0 = ToolsMath.ExtendedEuclideanAlgorithm(4864, 3458); Assert.AreEqual(32, tuple_0.Item1); Assert.AreEqual(-45, tuple_0.Item2); Assert.AreEqual(38, tuple_0.Item3); //GCD Tuple <int, int, int> tuple_1 = ToolsMath.ExtendedEuclideanAlgorithm(6, 3); Assert.AreEqual(3, tuple_1.Item3); Tuple <int, int, int> tuple_2 = ToolsMath.ExtendedEuclideanAlgorithm(39, 26); Assert.AreEqual(13, tuple_2.Item3); Tuple <int, int, int> tuple_3 = ToolsMath.ExtendedEuclideanAlgorithm(7, 23); Assert.AreEqual(10, tuple_3.Item1); Tuple <int, int, int> tuple_4 = ToolsMath.ExtendedEuclideanAlgorithm(2, 23); Assert.AreEqual(-11, tuple_4.Item1); }
public void TestExtendedEuclideanAlgorithm1() { Tuple <BigInteger, BigInteger, BigInteger> tuple_0 = ToolsMath.ExtendedEuclideanAlgorithm(new AlgebraSymbolBigInteger(), 4864, 3458); Assert.AreEqual(32, tuple_0.Item1); Assert.AreEqual(-45, tuple_0.Item2); Assert.AreEqual(38, tuple_0.Item3); //GCD Tuple <BigInteger, BigInteger, BigInteger> tuple_1 = ToolsMath.ExtendedEuclideanAlgorithm(new AlgebraSymbolBigInteger(), 6, 3); Assert.AreEqual(3, tuple_1.Item3); Tuple <BigInteger, BigInteger, BigInteger> tuple_2 = ToolsMath.ExtendedEuclideanAlgorithm(new AlgebraSymbolBigInteger(), 39, 26); Assert.AreEqual(13, tuple_2.Item3); Tuple <BigInteger, BigInteger, BigInteger> tuple_3 = ToolsMath.ExtendedEuclideanAlgorithm(new AlgebraSymbolBigInteger(), 7, 23); Assert.AreEqual(10, tuple_3.Item1); Tuple <BigInteger, BigInteger, BigInteger> tuple_4 = ToolsMath.ExtendedEuclideanAlgorithm(new AlgebraSymbolBigInteger(), 2, 23); Assert.AreEqual(-11, tuple_4.Item1); }
private static float Collide(float offset_0, float offset_1, float location_0, float location_1) { return((ToolsMath.Sqr(location_1) - ToolsMath.Sqr(location_0) + offset_1 - offset_0) / (2.0f * (location_1 - location_0))); }
private static int ComputeDistance(ImageRaster3D <float> distance_image_sqr, int index_0, int index_1, int index_2, int index_axis, float[] locations_axis, float[] start, float[] locations, float[] offsets, int curve_index) { float location = locations_axis[index_axis]; while ((curve_index != 0) && (location < start[curve_index])) { curve_index--; } distance_image_sqr.SetElementValue(index_0, index_1, index_2, offsets[curve_index] + ToolsMath.Sqr(location - locations[curve_index])); return(curve_index); }
//public static void DistanceTransformRBA(ImageRaster3D<bool> mask_image, ImageRaster3D<float> distance_image, float[] voxel_size) //{ // int size_x = mask_image.Raster.SizeX; // int size_y = mask_image.Raster.SizeY; // int size_z = mask_image.Raster.SizeZ; // /* Apply the transform in the x-direction. */ // Parallel.For(0, size_z * size_y, plane_index => // { // int z_index = plane_index / size_y; // int y_index = plane_index - z_index * size_y; // int element_index = size_x * ((size_y * z_index) + y_index); // /* Project distances forward. */ // float distance = float.MaxValue; // for (int x_index = 0; x_index < size_x; x_index++) // { // distance += voxel_size[0]; // /* The voxel value is true; the distance should be 0. */ // if (mask_image[element_index + x_index]) // { // distance = 0.0f; // } // distance_image[element_index + x_index] = distance; // } // /* Project distances backward. From this point on we don't have to // * read the source data anymore, since all object voxels now have a // * distance assigned. */ // distance = float.MaxValue; // for (int x_index = size_x - 1; x_index >= 0; x_index--) // { // distance += voxel_size[0]; // /* The voxel value is 0; the distance should be 0 too. */ // if (distance_image[element_index + x_index] == 0.0f) // { // distance = 0.0f; // } // /* Calculate the shortest distance in the row. */ // distance_image[element_index + x_index] = Math.Min(distance_image[element_index + x_index], distance); // } // }); // /* Apply the transform in the y-direction. */ // float size_1_sqr = ToolsMath.Sqr(voxel_size[1]); // float size_1_inv = 1.0f / voxel_size[1]; // Parallel.For(0, size_z * size_x, plane_index => // { // int z_index = plane_index / size_x; // int x_index = plane_index - z_index * size_x; // int element_index = z_index * size_y * size_x + x_index; // float[] temp_1 = new float[size_y]; // /* Copy the column and square the distances. */ // for (int y_index = 0; y_index < size_y; y_index++) // { // temp_1[y_index] = ToolsMath.Sqr(distance_image[element_index + y_index * size_x]); // } // /* Calculate the smallest squared distance in 2D. */ // for (int y_index = 0; y_index < size_y; y_index++) // { // /* Calculate the smallest search range, i.e. y-im to y+im. */ // float distance = temp_1[y_index]; // if (distance == 0) // { // continue; // } // int im = (int)(size_1_inv * ToolsMath.Sqrt(distance)); // if (im == 0) // { // continue; // } // for (int j = Math.Max(0, y_index - im); j < Math.Min(size_y, y_index + im); j++) // { // if (temp_1[j] < distance) distance = Math.Min(distance, temp_1[j] + size_1_sqr * ToolsMath.Sqr(j - y_index)); // } // distance_image[element_index + y_index * size_x] = distance; // } // }); // /* Apply the transform in the z-direction. */ // float size_2_sqr = ToolsMath.Sqr(voxel_size[2]); // float size_2_inv = 1.0f / voxel_size[2]; // Parallel.For(0, size_y * size_x, plane_index => // { // float[] temp_2 = new float[size_z]; // int y_index = plane_index / size_x; // int x_index = plane_index - (y_index * size_x); // int element_index = y_index * size_x + x_index; // /* Copy the column. */ // for (int z_index = 0; z_index < size_z; z_index++) // { // temp_2[z_index] = distance_image[element_index + z_index * size_y * size_x]; // } // /* Calculate the smallest squared distance in 3D. */ // for (int z_index = 0; z_index < size_z; z_index++) // { // /* Calculate smallest search range, i.e. z-im to z+im. */ // float distance = temp_2[z_index]; // if (distance == 0) // { // continue; // } // int im = (int)(size_2_inv * ToolsMath.Sqrt(distance)); // if (im == 0) // { // continue; // } // for (int j = Math.Max(0, z_index - im); j < Math.Min(size_z, z_index + im); j++) // { // if (temp_2[j] < distance) distance = Math.Min(distance, temp_2[j] + size_2_sqr * ToolsMath.Sqr(j - z_index)); // } // distance_image[element_index + z_index * size_x * size_y] = distance; // } // }); //} public static void DistanceTransform3DMediumRBA(ImageRaster3D <bool> mask_image, float[] voxel_size, ImageRaster3D <float> distance_image) { IRaster3DInteger raster = mask_image.Raster; int size_0 = mask_image.Raster.Size0; int size_1 = mask_image.Raster.Size1; int size_2 = mask_image.Raster.Size2; float[] locations_0 = new float[size_0]; float[] locations_1 = new float[size_1]; float[] locations_2 = new float[size_2]; Parallel.For(0, size_0, index_0 => { locations_0[index_0] = index_0 * voxel_size[0]; }); Parallel.For(0, size_1, index_1 => { locations_1[index_1] = index_1 * voxel_size[1]; }); Parallel.For(0, size_2, index_2 => { locations_2[index_2] = index_2 * voxel_size[2]; }); // Apply the transform in the x-direction //for (int plane_index = 0; plane_index < size_z * size_y; plane_index++) //{ Parallel.For(0, size_2 * size_1, plane_index => { int index_2 = plane_index / size_1; int index_1 = plane_index % size_1; // Upwards pass float added_distance = float.PositiveInfinity; for (int index_0 = 0; index_0 < size_0; index_0++) { int element_index = raster.GetElementIndex(index_0, index_1, index_2); added_distance += voxel_size[0]; /* The voxel value is true; the distance should be 0. */ if (mask_image[element_index]) { added_distance = 0.0f; } distance_image[element_index] = ToolsMath.Sqr(added_distance); } if (distance_image.GetElementValue(size_0 - 1, index_1, index_2) < float.PositiveInfinity) { // Downwards pass added_distance = float.PositiveInfinity; for (int index_0 = size_0 - 1; index_0 >= 0; index_0--) { int element_index = raster.GetElementIndex(index_0, index_1, index_2); added_distance += voxel_size[0]; /* The voxel value is 0; the distance should be 0 too. */ if (distance_image[element_index] == 0.0f) { added_distance = 0.0f; } /* Calculate the shortest distance in the row. */ distance_image[element_index] = Math.Min(distance_image[element_index], ToolsMath.Sqr(added_distance)); } } }); // Apply the transform in the y-direction //for (int plane_index = 0; plane_index < size_0 * size_2; plane_index++) //{ Parallel.For(0, size_2 * size_0, plane_index => { int index_0 = plane_index % size_0; int index_2 = plane_index / size_0; float[] temp_1 = new float[size_1]; for (int index_1 = 0; index_1 < size_1; index_1++) { temp_1[index_1] = distance_image.GetElementValue(index_0, index_1, index_2); } // Upwards pass for (int index_1 = 0; index_1 < size_1; index_1++) { int element_index = raster.GetElementIndex(index_0, index_1, index_2); for (int index_1_inner = index_1 + 1; index_1_inner < size_1; index_1_inner++) { if (distance_image.GetElementValue(index_0, index_1_inner, index_2) <= distance_image[element_index]) { break; } float distance = distance_image[element_index] + ToolsMath.Sqr((index_1_inner - index_1) * voxel_size[1]); if (distance < temp_1[index_1_inner]) { temp_1[index_1_inner] = distance; } } } //Downwards pass for (int index_1 = size_1 - 1; index_1 >= 0; index_1--) { int element_index = raster.GetElementIndex(index_0, index_1, index_2); for (int index_1_inner = index_1 - 1; index_1_inner >= 0; index_1_inner--) { if (distance_image.GetElementValue(index_0, index_1_inner, index_2) <= distance_image[element_index]) { break; } float distance = distance_image[element_index] + ToolsMath.Sqr((index_1 - index_1_inner) * voxel_size[1]); if (distance < temp_1[index_1_inner]) { temp_1[index_1_inner] = distance; } } } for (int index_1 = 0; index_1 < size_1; index_1++) { distance_image.SetElementValue(index_0, index_1, index_2, temp_1[index_1]); } }); // Apply the transform in the z-direction. */ //for (int plane_index = 0; plane_index < size_x * size_y; plane_index++) //{ Parallel.For(0, size_1 * size_0, plane_index => { int index_0 = plane_index % size_0; int index_1 = plane_index / size_0; float[] temp_2 = new float[size_2]; for (int index_2 = 0; index_2 < size_2; index_2++) { temp_2[index_2] = distance_image.GetElementValue(index_0, index_1, index_2); } // Upwards_pass for (int index_2 = 0; index_2 < size_2; index_2++) { int element_index = raster.GetElementIndex(index_0, index_1, index_2); for (int index_2_inner = index_2 + 1; index_2_inner < size_2; index_2_inner++) { if (distance_image.GetElementValue(index_0, index_1, index_2_inner) <= distance_image[element_index]) { break; } float distance = distance_image[element_index] + ToolsMath.Sqr((index_2_inner - index_2) * voxel_size[2]); if (distance < temp_2[index_2_inner]) { temp_2[index_2_inner] = distance; } } } // Downwards pass for (int index_2 = size_2 - 1; index_2 >= 0; index_2--) { int element_index = raster.GetElementIndex(index_0, index_1, index_2); for (int index_2_inner = index_2 - 1; index_2_inner >= 0; index_2_inner--) { if (distance_image.GetElementValue(index_0, index_1, index_2_inner) <= distance_image[element_index]) { break; } float distance = distance_image[element_index] + ToolsMath.Sqr((index_2 - index_2_inner) * voxel_size[2]); if (distance < temp_2[index_2_inner]) { temp_2[index_2_inner] = distance; } } } //Fill and root for (int index_2 = 0; index_2 < size_2; index_2++) { distance_image.SetElementValue(index_0, index_1, index_2, ToolsMath.Sqrt(temp_2[index_2])); } }); }
public static void DistanceTransform3DOosterbroekRBA(ImageRaster3D <bool> mask_image, float[] voxel_size, ImageRaster3D <float> distance_image_sqr) { // fetch some initial data IRaster3DInteger raster = mask_image.Raster; int size_0 = mask_image.Raster.Size0; int size_1 = mask_image.Raster.Size1; int size_2 = mask_image.Raster.Size2; float [] locations_0 = new float[size_0]; float [] locations_1 = new float[size_1]; float [] locations_2 = new float[size_2]; Parallel.For(0, size_0, index_0 => { locations_0[index_0] = index_0 * voxel_size[0]; }); Parallel.For(0, size_1, index_1 => { locations_1[index_1] = index_1 * voxel_size[1]; }); Parallel.For(0, size_2, index_2 => { locations_2[index_2] = index_2 * voxel_size[2]; }); //Initials pass for zeros and infs Parallel.For(0, distance_image_sqr.ElementCount, element_index => { if (mask_image[element_index]) { distance_image_sqr[element_index] = 0; } else { distance_image_sqr[element_index] = float.PositiveInfinity; } }); // Apply the transform in the x-direction Parallel.For(0, size_2 * size_1, plane_index => { int index_1 = plane_index % size_1; int index_2 = plane_index / size_1; float[] start = new float[size_0]; float[] locations = new float[size_0]; float[] offsets = new float[size_0]; int curve_index = -1; //Curve pass for (int index_0 = 0; index_0 < size_0; index_0++) { curve_index = ComputeCurves(distance_image_sqr, index_0, index_1, index_2, index_0, locations_0, start, locations, offsets, curve_index); } if (curve_index != -1) { //if index == -1 then no curves were build and all are positive infinity for (int index_0 = size_0 - 1; index_0 >= 0; index_0--) { // Compute pass (backwards) curve_index = ComputeDistance(distance_image_sqr, index_0, index_1, index_2, index_0, locations_0, start, locations, offsets, curve_index); } } }); // Apply the transform in the y-direction Parallel.For(0, size_2 * size_0, plane_index => { int index_0 = plane_index % size_0; int index_2 = plane_index / size_0; float[] start = new float[size_1]; float[] locations = new float[size_1]; float[] offsets = new float[size_1]; int curve_index = -1; //Curve pass for (int index_1 = 0; index_1 < size_1; index_1++) { curve_index = ComputeCurves(distance_image_sqr, index_0, index_1, index_2, index_1, locations_1, start, locations, offsets, curve_index); } if (curve_index != -1) { //if index == -1 then no curves were build and all are positive infinity for (int index_1 = size_1 - 1; index_1 >= 0; index_1--) { // Compute pass (backwards) curve_index = ComputeDistance(distance_image_sqr, index_0, index_1, index_2, index_1, locations_1, start, locations, offsets, curve_index); } } }); // Apply the transform in the z-direction. */ Parallel.For(0, size_1 * size_0, plane_index => { int index_0 = plane_index % size_0; int index_1 = plane_index / size_0; float[] start = new float[size_2]; float[] locations = new float[size_2]; float[] offsets = new float[size_2]; int curve_index = -1; //Curve pass for (int index_2 = 0; index_2 < size_2; index_2++) { curve_index = ComputeCurves(distance_image_sqr, index_0, index_1, index_2, index_2, locations_2, start, locations, offsets, curve_index); } if (curve_index != -1) { //if index == -1 then no curves were build and all are positive infinity // Compute pass (backwards) for (int index_2 = size_2 - 1; index_2 >= 0; index_2--) { // Compute pass (backwards) curve_index = ComputeDistance(distance_image_sqr, index_0, index_1, index_2, index_2, locations_2, start, locations, offsets, curve_index); } } }); //Faniak pass for rooting Parallel.For(0, distance_image_sqr.ElementCount, element_index => { distance_image_sqr[element_index] = ToolsMath.Sqrt(distance_image_sqr[element_index]); }); }
private FiniteFieldElement <IntegerType> MutiplicativeInverse(FiniteFieldElement <IntegerType> element_0) { Tuple <IntegerType, IntegerType, IntegerType> xyd = ToolsMath.ExtendedEuclideanAlgorithm(Algebra, element_0.Value, Prime); return(new FiniteFieldElement <IntegerType>(this, Algebra.Modulo(xyd.Item1, Prime))); }