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]);
        }
        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]);
        }
示例#4
0
        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);
                }
            });
        }
示例#5
0
        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]);
            });
        }
示例#6
0
        //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]));
                }
            });
        }