Beispiel #1
0
        public static Bitmap ConvertToBitmapUInt16(IImageRaster <IRaster3DInteger, uint> image, int index_z)
        {
            Bitmap bitmap = new Bitmap(image.Raster.Size0, image.Raster.Size1, System.Drawing.Imaging.PixelFormat.Format16bppGrayScale);
            //Get a reference to the images pixel data
            Rectangle  dimension           = new Rectangle(0, 0, image.Raster.Size0, image.Raster.Size1);
            BitmapData bitmap_data         = bitmap.LockBits(dimension, ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format16bppGrayScale);
            IntPtr     pixel_start_address = bitmap_data.Scan0;

            byte[] pixel_values = new byte[image.Raster.Size0 * image.Raster.Size1 * 2];
            //Copy the pixel data into the bitmap structure
            int byte_index = 0;

            for (int index_y = 0; index_y < image.Raster.Size1; index_y++)
            {
                for (int index_x = 0; index_x < image.Raster.Size0; index_x++)
                {
                    byte[] bytes = BitConverter.GetBytes(image.GetElementValue(image.Raster.GetElementIndex(index_x, index_y, index_z)));
                    pixel_values[byte_index] = bytes[0];
                    byte_index++;
                    pixel_values[byte_index] = bytes[1];
                    byte_index++;
                }
            }
            System.Runtime.InteropServices.Marshal.Copy(pixel_values, 0, pixel_start_address, pixel_values.Length);
            bitmap.UnlockBits(bitmap_data);
            return(bitmap);
        }
Beispiel #2
0
        public static void SaveImageRaster3DAsTIFF3D2(string save_path, IImageRaster <IRaster3DInteger, uint> image)
        {
            Stream            destination_stream = new FileStream(save_path, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
            TiffBitmapEncoder encoder            = new TiffBitmapEncoder();

            encoder.Compression = TiffCompressOption.None;
            for (int index_z = 0; index_z < image.Raster.Size2; index_z++)
            {
                Bitmap       bitmap        = ConvertToBitmapUInt16(image, index_z);
                BitmapSource bitmap_source = ToolsRendering.CreateBitmapSourceFromBitmap16Bit(bitmap);
                encoder.Frames.Add(BitmapFrame.Create(bitmap_source));
            }
            encoder.Save(destination_stream);
            //TiffBitmapEncoder encoder = new TiffBitmapEncoder(destination_stream_, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
            //Stream imageStreamSource = new FileStream(load_path, FileMode.Open, FileAccess.Read, FileShare.Read);
            //TiffBitmapDecoder decoder = new TiffBitmapDecoder(imageStreamSource, BitmapCreateOptions.PreservePixelFormat, BitmapCacheOption.Default);
            //BitmapSource test_frame = decoder.Frames[0];
            //int size_x = test_frame.PixelWidth;
            //int size_y = test_frame.PixelHeight;
            //int size_z = decoder.Frames.Count;
            //int bits_per_pixel = test_frame.Format.BitsPerPixel;

            //for (int index_z = 0; index_z < size_z; index_z++)
            //{
            //    // save each frame to a bytestream
            //    BitmapSource frame = decoder.Frames[index_z];
            //    //  img.CopyPixels(pixels, stride, 0)
            //    MemoryStream byte_stream = new MemoryStream();
            //    // bitmap.Save(byte_stream, ImageFormat.Tiff);

            //    // and then create a new Image from it
            //    System.Drawing.Image image = System.Drawing.Image.FromStream(byte_stream);
            //    // d
            //}
        }
Beispiel #3
0
 public ImageRaster2DWrapperSlice3D(IImageRaster <IRaster3DInteger, RangeType> image, int index_2)
 {
     Debug.Assert(index_2 < image.Raster.Size2);
     Raster             = new Raster2DInteger(image.Raster.Size0, image.Raster.Size1);
     this.wrapped_image = image;
     this.index_2       = index_2;
 }
Beispiel #4
0
        public static void CreateMultipageTiff1(string save_path, IImageRaster <IRaster3DInteger, ushort> image)
        {
            int size_x = image.Raster.Size0;
            int size_y = image.Raster.Size1;
            int size_z = image.Raster.Size2;

            const int samplesPerPixel = 1;
            const int bitsPerSample   = 16;

            byte[][] firstPageBuffer = new byte[size_y][];
            for (int j = 0; j < size_y; j++)
            {
                firstPageBuffer[j] = new byte[size_x];
                for (int i = 0; i < size_x; i++)
                {
                    firstPageBuffer[j][i] = (byte)(j * i);
                }
            }

            using (Tiff output = Tiff.Open(save_path, "w"))
            {
                for (int page = 0; page < size_z; ++page)
                {
                    output.SetField(TiffTag.IMAGEWIDTH, size_x / samplesPerPixel);
                    output.SetField(TiffTag.IMAGELENGTH, size_y);

                    output.SetField(TiffTag.SAMPLESPERPIXEL, samplesPerPixel);
                    output.SetField(TiffTag.BITSPERSAMPLE, bitsPerSample);
                    output.SetField(TiffTag.ORIENTATION, Orientation.TOPLEFT);
                    output.SetField(TiffTag.PLANARCONFIG, PlanarConfig.CONTIG);

                    if (page % 2 == 0)
                    {
                        output.SetField(TiffTag.PHOTOMETRIC, Photometric.MINISBLACK);
                    }
                    else
                    {
                        output.SetField(TiffTag.PHOTOMETRIC, Photometric.MINISWHITE);
                    }

                    output.SetField(TiffTag.ROWSPERSTRIP, 1);
                    output.SetField(TiffTag.XRESOLUTION, 100.0);
                    output.SetField(TiffTag.YRESOLUTION, 100.0);
                    output.SetField(TiffTag.RESOLUTIONUNIT, ResUnit.INCH);

                    // specify that it's a page within the multipage file
                    output.SetField(TiffTag.SUBFILETYPE, FileType.PAGE);
                    // specify the page number
                    output.SetField(TiffTag.PAGENUMBER, page, size_z);

                    for (int j = 0; j < size_y; ++j)
                    {
                        output.WriteEncodedStrip(j, firstPageBuffer[j], firstPageBuffer.Length);
                    }
                    output.WriteDirectory();
                }
            }
        }
Beispiel #5
0
 public ImageSpace3DFloatLinear(IImageRaster <IRaster3DInteger, float> image)
 {
     this.image            = image;
     this.algebra          = new AlgebraRealFloat32();
     this.space_size_0     = 1;
     this.space_size_1     = 1;
     this.space_size_2     = 1;
     this.background_value = 0;
 }
Beispiel #6
0
 public AlphaPartitionTree3D(
     IAlphaPartitionTreeBuilder <ElementValueType, EdgeValueType> builder,
     ITopologyElementEdgeRaster <IRaster3DInteger> topology,
     IFunctionDissimilarity <ElementValueType, EdgeValueType> edge_function,
     IImageRaster <IRaster3DInteger, ElementValueType> image)
 {
     //TODO make sure topology matches image
     ElementValueType[] element_values = image.GetElementValues(false);
     tree = builder.BuildAlphaPartitionTree(topology, edge_function, element_values);
 }
Beispiel #7
0
        public Bitmap Render(IImageRaster <IRaster3DInteger, bool> source_image)
        {
            IRaster3DInteger source_raster = source_image.Raster;
            List <int>       elements_true = source_image.GetElementIndexesWithValue(true);

            float[,] coordinates_image   = new float[elements_true.Count, 3];
            float[,] elements_projection = new float[elements_true.Count, 3];
            int [] coordinates = new int [3];

            for (int element_index_index = 0; element_index_index < elements_true.Count; element_index_index++)
            {
                source_raster.GetElementCoordinatesRBA(elements_true[element_index_index], coordinates);
                coordinates_image[element_index_index, 0] = coordinates[0];
                coordinates_image[element_index_index, 1] = coordinates[1];
                coordinates_image[element_index_index, 2] = coordinates[2];
            }

            for (int element_index = 0; element_index < elements_true.Count; element_index++)
            {
                elements_projection[element_index, 0] = ((coordinates_image[element_index, 0] - image_focus[0]) * projection_vector_x[0]) +
                                                        ((coordinates_image[element_index, 1] - image_focus[1]) * projection_vector_x[1]) +
                                                        ((coordinates_image[element_index, 2] - image_focus[2]) * projection_vector_x[2]);

                elements_projection[element_index, 1] = ((coordinates_image[element_index, 0] - image_focus[0]) * projection_vector_y[0]) +
                                                        ((coordinates_image[element_index, 1] - image_focus[1]) * projection_vector_y[1]) +
                                                        ((coordinates_image[element_index, 2] - image_focus[2]) * projection_vector_y[2]);

                elements_projection[element_index, 2] = ((coordinates_image[element_index, 0] - image_focus[0]) * projection_vector_z[0]) +
                                                        ((coordinates_image[element_index, 1] - image_focus[1]) * projection_vector_z[1]) +
                                                        ((coordinates_image[element_index, 2] - image_focus[2]) * projection_vector_z[2]);
            }

            Raster2DInteger bitmap_raster     = new Raster2DInteger(bitmap_size_x, bitmap_size_y);
            Bitmap          destination_image = new Bitmap(bitmap_size_x, bitmap_size_y);

            for (int index_y = 0; index_y < bitmap_raster.Size1; index_y++)
            {
                for (int index_x = 0; index_x < bitmap_raster.Size0; index_x++)
                {
                    destination_image.SetPixel(index_x, index_y, Color.Black);
                }
            }

            for (int element_index = 0; element_index < elements_true.Count; element_index++)
            {
                int x_target = (int)elements_projection[element_index, 0] + (bitmap_raster.Size0 / 2);
                int y_target = (int)elements_projection[element_index, 1] + (bitmap_raster.Size1 / 2);

                if (bitmap_raster.ContainsCoordinates(x_target, y_target))
                {
                    destination_image.SetPixel(x_target, y_target, Color.White);
                }
            }
            return(destination_image);
        }
Beispiel #8
0
        public void ComputeCentroids(
            IImageRaster <IRasterType, int> image_labeling,
            IImageRaster <IRasterType, DomainType> image_data,
            IList <int[]> cluster_spatial_centroids,
            IList <DomainType> cluster_feature_centroids)
        {
            //Use the cluster raster for spacing the clusters in the image
            IList <IList <DomainType> > clusters = new List <IList <DomainType> >();
            int dimension_count = image_labeling.Raster.DimensionCount;
            int element_count   = image_labeling.Raster.ElementCount;

            int [] element_coordinates = new int[dimension_count];
            //clear clusters
            // TODO make paralel
            for (int cluster_index = 0; cluster_index < cluster_spatial_centroids.Count; cluster_index++)
            {
                clusters.Add(new List <DomainType>());
                for (int dimension_index = 0; dimension_index < image_labeling.Raster.DimensionCount; dimension_index++)
                {
                    cluster_spatial_centroids[cluster_index][dimension_index] = 0;
                }
            }

            // Aggregate cluster.
            // TODO make paralel
            for (int element_index = 0; element_index < element_count; element_index++)
            {
                int cluster_index = image_labeling.GetElementValue(element_index);
                image_data.Raster.GetElementCoordinatesRBA(element_index, element_coordinates);
                clusters[cluster_index].Add(image_data.GetElementValue(element_index));
                ToolsMathCollectionInteger.AddFill(cluster_spatial_centroids[cluster_index], element_coordinates, cluster_spatial_centroids[cluster_index]);
            }

            // Compute new cluster centers.
            // TODO make paralel
            for (int cluster_index = 0; cluster_index < clusters.Count; cluster_index++)
            {
                if (clusters[cluster_index].Count != 0)
                {
                    for (int dimension_index = 0; dimension_index < image_labeling.Raster.DimensionCount; dimension_index++)
                    {
                        cluster_spatial_centroids[cluster_index][dimension_index] /= clusters[cluster_index].Count;
                    }
                    cluster_feature_centroids[cluster_index] = d_centroid_feature_calculator.Compute(clusters[cluster_index]);
                }
                else
                {
                    // reduce cluster count
                    cluster_feature_centroids.RemoveAt(cluster_index);
                    cluster_spatial_centroids.RemoveAt(cluster_index);
                    clusters.RemoveAt(cluster_index);
                    cluster_index--;
                }
            }
        }
Beispiel #9
0
        public Bitmap Render(IImageRaster <IRaster2DInteger, ElementType> source_image)
        {
            IRaster2DInteger raster            = source_image.Raster;
            Bitmap           destination_image = new Bitmap(raster.Size0, raster.Size1);

            for (int index_y = 0; index_y < raster.Size1; index_y++)
            {
                for (int index_x = 0; index_x < raster.Size0; index_x++)
                {
                    destination_image.SetPixel(index_x, index_y, converter.Compute(source_image.GetElementValue(raster.GetElementIndex(index_x, index_y))));
                }
            }
            return(destination_image);
        }
Beispiel #10
0
        public void AssignElements(
            IImageRaster <IRasterType, int> image_labeling,
            IImageRaster <IRasterType, float> image_distance,
            IImageRaster <IRasterType, DomainType> image_data,
            IList <int[]> cluster_spatial_centroids,
            IList <DomainType> cluster_feature_centroids,
            int[] neigbourhood_element_indexes)
        {
            int dimension_count = image_labeling.Raster.DimensionCount;

            int[] element_coordinates = new int[dimension_count];

            for (int element_index = 0; element_index < image_distance.Raster.ElementCount; element_index++)
            {
                image_distance.SetElementValue(element_index, Single.MaxValue);
            }

            //for each cluster
            // TODO make paralel (Beware race conditoin in setting labeling and distance RB ordering might help or make it image driven)
            for (int index_cluster = 0; index_cluster < cluster_spatial_centroids.Count; index_cluster++)
            {
                DomainType centroid            = cluster_feature_centroids[index_cluster];
                int[]      coordinates_cluster = cluster_spatial_centroids[index_cluster];

                //for each pixel in a region around the cluster neigbourhood
                image_data.Raster.GetNeigbourhoodElementIndexesRBA(coordinates_cluster, this.d_cluster_dimensions, neigbourhood_element_indexes);
                for (int element_index_index = 0; element_index_index < neigbourhood_element_indexes.Length; element_index_index++)
                {
                    int index_element = neigbourhood_element_indexes[element_index_index];
                    if (index_element != -1)
                    {
                        image_data.Raster.GetElementCoordinatesRBA(index_element, element_coordinates);
                        //Compute the distance D between the element and the cluster.
                        float distance = 0;
                        for (int dimension_index = 0; dimension_index < dimension_count; dimension_index++)
                        {
                            float dimension_distance = ((float)Math.Abs(coordinates_cluster[dimension_index] - element_coordinates[dimension_index])) / this.d_cluster_dimensions[dimension_index];
                            distance += dimension_distance * dimension_distance;
                        }
                        distance += d_distance_features.Compute(centroid, image_data.GetElementValue(index_element)) * this.feature_weight;
                        //Compute if it is less than the current distance update cluster membership
                        if (distance < image_distance.GetElementValue(index_element))
                        {
                            image_labeling.SetElementValue(index_element, index_cluster);
                            image_distance.SetElementValue(index_element, distance);
                        }
                    }
                }
            }
        }
Beispiel #11
0
 public MaxTreeFloat3DFeaturesDouble(IImageRaster <IRaster3DInteger, float> image, FeatureGeneratorElementNode3DDouble generator, IProgressReporter reporter)
 {
     this.raster         = image.Raster;
     this.inner_max_tree = new MaxTreeBuilderSingleQueue <float>().BuildMaxTree(image.GetElementValues(false), new ComparerNatural <float>(), new TopologyElementRaster3D6Connectivity(image.Raster), image.Raster.ElementCount);
     if (generator != null)
     {
         this.features = new double[inner_max_tree.NodeCount, generator.FeatureCount];
         generator.GenerateFeaturesTree(raster, inner_max_tree.BottomLevelNode, features);
     }
     else
     {
         this.features = null;
     }
 }
Beispiel #12
0
        public void TestInitialize()
        {
            int iteration_count = 1;

            int [] desired_cluster_dimensions = new int [] { 3, 3, 3 };
            SLIC3D slick = new SLIC3D(iteration_count, desired_cluster_dimensions, 1);

            Tuple <IImageRaster <IRaster3DInteger, int>, IImageRaster <IRaster3DInteger, float>, IList <int[]>, IList <float[]> > initialization = slick.Initialize(new Raster3DInteger(9, 9, 9));
            IImageRaster <IRaster3DInteger, int>   image_labeling = initialization.Item1;
            IImageRaster <IRaster3DInteger, float> image_distance = initialization.Item2;
            IList <int[]>   cluster_spatial_centroids             = initialization.Item3;
            IList <float[]> cluster_feature_centroids             = initialization.Item4;

            Assert.AreEqual(27, cluster_spatial_centroids.Count);
        }
Beispiel #13
0
        public static ImageRaster <CommonRasterType, ToElementType> Convert <CommonRasterType, FromElementType, ToElementType>(
            IImageRaster <CommonRasterType, FromElementType> source,
            IFunction <FromElementType, ToElementType> converter)
            where CommonRasterType : IRasterInteger
        {
            CommonRasterType raster = source.Raster;
            int element_count       = raster.ElementCount;

            ToElementType [] image = new ToElementType[element_count];
            Parallel.For(0, element_count, element_index =>
            {
                image[element_index] = converter.Compute(source.GetElementValue(element_index));
            });
            return(new ImageRaster <CommonRasterType, ToElementType>(raster, image, false));
        }
Beispiel #14
0
        public BitmapSource Render(RenderSourceType render_source)
        {
            IImageRaster <IRaster2DInteger, ElementValueType> rendered_image = inner_renderer.Render(render_source);
            BitmapFast bitmap_fast = new BitmapFast(rendered_image.Raster.Size0, rendered_image.Raster.Size1);

            bitmap_fast.Lock();
            for (int y_index = 0; y_index < rendered_image.Raster.Size1; y_index++)
            {
                for (int x_index = 0; x_index < rendered_image.Raster.Size0; x_index++)
                {
                    bitmap_fast.SetPixel(x_index, y_index, converter.Compute(rendered_image.GetElementValue(rendered_image.Raster.GetElementIndex(x_index, y_index))));
                }
            }
            bitmap_fast.Unlock();
            return(ToolsRendering.CreateBitmapSourceFromBitmap(bitmap_fast.Bitmap));
        }
Beispiel #15
0
        public ImageRaster2DWrapperSlice4D(IImageRaster <IRaster4DInteger, RangeType> image, int index_2, int index_3)
        {
            if (image.Raster.Size2 <= index_2)
            {
                throw new Exception("index_2 with value " + index_2 + " out of bounds size_2 = " + image.Raster.Size2);
            }

            if (image.Raster.Size3 <= index_3)
            {
                throw new Exception("index_3 with value " + index_3 + " out of bounds size_3 = " + image.Raster.Size3);
            }

            Raster             = new Raster2DInteger(image.Raster.Size0, image.Raster.Size1);
            this.wrapped_image = image;
            this.index_2       = index_2;
            this.index_3       = index_3;
        }
Beispiel #16
0
        public BitmapFast Render(RenderSourceType render_source)
        {
            IImageRaster <IRaster2DInteger, ElementValueType> rendered_image = inner_renderer.Render(render_source);
            BitmapFast bitmap_fast = new BitmapFast(rendered_image.Raster.Size0, rendered_image.Raster.Size1);

            bitmap_fast.Lock();
            //for (int y_index = 0; y_index < rendered_image.Raster.SizeY; y_index++)
            //{
            Parallel.For(0, rendered_image.Raster.Size1, y_index =>
            {
                for (int x_index = 0; x_index < rendered_image.Raster.Size0; x_index++)
                {
                    bitmap_fast.SetPixel(x_index, y_index, converter.Compute(rendered_image.GetElementValue(rendered_image.Raster.GetElementIndex(x_index, y_index))));
                }
            });
            bitmap_fast.Unlock();
            return(bitmap_fast);
        }
        public IImageRaster <IRaster3DInteger, float> Predict(IImageRaster <IRaster3DInteger, float []> feature_image)
        {
            AlphaPartitionTree3D <float[], float> tree = new AlphaPartitionTree3D <float[], float>(
                builder,
                topology,
                edge_model,
                feature_image);


            IList <IElementTreeNode <float> > nodes = tree.GetSmallestSatisfiers(node_detector);

            IImageRaster <IRaster3DInteger, float> image = new ImageRaster3D <float>(feature_image.Raster, Single.MaxValue);

            for (int element_index = 0; element_index < nodes.Count; element_index++)
            {
            }
            return(image);
        }
        public static void SetOverlay <RasterType, DomainType> (
            IImageRaster <RasterType, DomainType> destination,
            IImageRaster <RasterType, bool> overlay_mask,
            DomainType overlay_value)
            where RasterType : IRasterInteger
        {
            if (!destination.Raster.Equals(overlay_mask.Raster))
            {
                throw new Exception("Raster mismatch");
            }

            Parallel.For(0, overlay_mask.Raster.ElementCount, element_index =>
            {
                if (overlay_mask.GetElementValue(element_index))
                {
                    destination.SetElementValue(element_index, overlay_value);
                }
            });
        }
Beispiel #19
0
        public Bitmap Render(IImageRaster <IRaster3DInteger, ElementType> source_image)
        {
            IRaster3DInteger raster            = source_image.Raster;
            Bitmap           destination_image = new Bitmap(raster.Size0, raster.Size1);

            for (int index_y = 0; index_y < raster.Size1; index_y++)
            {
                for (int index_x = 0; index_x < raster.Size0; index_x++)
                {
                    ElementType mean_value = source_image.GetElementValue(raster.GetElementIndex(index_x, index_y, 0));
                    for (int index_z = 1; index_z < raster.Size2; index_z++)
                    {
                        mean_value = this.algebra.Add(mean_value, source_image.GetElementValue(raster.GetElementIndex(index_x, index_y, index_z)));
                    }
                    mean_value = algebra.Divide(mean_value, this.algebra.ToDomain(raster.Size2));
                    destination_image.SetPixel(index_x, index_y, this.converter.Compute(mean_value));
                }
            }
            return(destination_image);
        }
Beispiel #20
0
        public void TestComputeCentroids()
        {
            int iteration_count = 1;

            int[]            desired_cluster_dimensions = new int[] { 5, 5, 1 };
            SLIC3D           slick  = new SLIC3D(iteration_count, desired_cluster_dimensions, 1);
            IRaster3DInteger raster = new Raster3DInteger(100, 100, 1);

            IList <IImageRaster <IRaster3DInteger, float> > stack = new List <IImageRaster <IRaster3DInteger, float> >();

            stack.Add(new ImageRaster3D <float>(raster, 0));
            stack.Add(new ImageRaster3D <float>(raster, 0));
            stack.Add(new ImageRaster3D <float>(raster, 0));
            IImageRaster <IRaster3DInteger, float[]> image_features = new ImageRasterWrapperStack <IRaster3DInteger, float>(stack);
            Tuple <IImageRaster <IRaster3DInteger, int>, IImageRaster <IRaster3DInteger, float>, IList <int[]>, IList <float[]> > initialization = slick.Initialize(raster);
            IImageRaster <IRaster3DInteger, int>   image_labeling = initialization.Item1;
            IImageRaster <IRaster3DInteger, float> image_distance = initialization.Item2;
            IList <int[]>   cluster_spatial_centroids             = initialization.Item3;
            IList <float[]> cluster_feature_centroids             = initialization.Item4;

            slick.ComputeCentroids(image_labeling, image_features, cluster_spatial_centroids, cluster_feature_centroids);
        }
Beispiel #21
0
        public static void GetShellRBA <RasterType>(IImageRaster <RasterType, bool> source, ITopologyElement topology, IImageRaster <RasterType, bool> target, bool border_is_shell)
            where RasterType : IRasterInteger
        {
            int[] element_neigbour_array = new int[topology.MaximumConnectivity];


            for (int element_index = 0; element_index < source.Raster.ElementCount; element_index++)
            {
                if (source.GetElementValue(element_index))
                {
                    bool is_shell = false;
                    topology.ElementNeighboursRBA(element_index, element_neigbour_array);

                    foreach (int other_element_index in element_neigbour_array)
                    {
                        if (other_element_index != -1)
                        {
                            if (!source.GetElementValue(other_element_index))
                            {
                                is_shell = true;
                            }
                        }
                        else
                        {
                            if (border_is_shell)
                            {
                                is_shell = true;
                            }
                        }
                    }

                    if (is_shell)
                    {
                        target.SetElementValue(element_index, true);
                    }
                }
            }
        }
        public void ImproveLabeling(
            IImageRaster <RasterType, int> initial_labeling,
            IImageRaster <RasterType, int> new_labeling,
            IList <IImageRaster <RasterType, FeatureType> > feature_images)
        {
            int element_count     = initial_labeling.Raster.ElementCount;
            int neighborhood_size = 0;

            int []         element_coordinates          = new int [initial_labeling.Raster.DimensionCount];
            int []         element_neighborhood_indexes = new int [this.neighborhood_element_count];
            int []         state_vector = new int [this.neighborhood_element_count];
            FeatureType [] feature_neighborhood_values = new FeatureType [neighborhood_size];
            FeatureType [] feature_vector = new FeatureType [neighborhood_size];

            for (int element_index = 0; element_index < element_count; element_index++)
            {
                initial_labeling.Raster.GetNeigbourhoodElementIndexesRBA(element_coordinates, this.neighborhood_size, element_neighborhood_indexes);
                // initial_labeling.GetElementValuesFill(element_neighborhood_indexes, this.background_label, state_vector);

                for (int feature_image_index = 0; feature_image_index < feature_image_count; feature_image_index++)
                {
                    //feature_images[feature_image_index].GetElementValuesFill(element_neighborhood_indexes, this.background_feature, feature_neighborhood_values);
                    Array.Copy(feature_neighborhood_values, 0, feature_vector, this.neighborhood_element_count * feature_image_index, this.neighborhood_element_count);
                }

                float best_likelyhood = Single.MinValue;

                for (int label_index = 0; label_index < this.label_count; label_index++)
                {
                    float likelyhood = label_likelyhood_models[label_index].Compute(state_vector) * feature_likelyhood_models[label_index].Compute(feature_vector);
                    if (best_likelyhood < likelyhood)
                    {
                        best_likelyhood = likelyhood;
                        new_labeling.SetElementValue(label_index, label_index);
                    }
                }
            }
        }
Beispiel #23
0
        public void TestSlick3Cluster()
        {
            int iteration_count = 5;

            int[]            desired_cluster_dimensions = new int[] { 5, 1, 1 };
            SLIC3D           slick  = new SLIC3D(iteration_count, desired_cluster_dimensions, 1);
            IRaster3DInteger raster = new Raster3DInteger(15, 1, 1);

            IList <IImageRaster <IRaster3DInteger, float> > stack         = new List <IImageRaster <IRaster3DInteger, float> >();
            IImageRaster <IRaster3DInteger, float>          feature_image = new ImageRaster3D <float>(raster, 0);

            for (int element_index = 3; element_index < 12; element_index++)
            {
                feature_image.SetElementValue(element_index, 1);
            }
            stack.Add(feature_image);
            IImageRaster <IRaster3DInteger, float[]> image_features = new ImageRasterWrapperStack <IRaster3DInteger, float>(stack);
            Tuple <IImageRaster <IRaster3DInteger, int>, IImageRaster <IRaster3DInteger, float>, IList <int[]>, IList <float[]> > initialization = slick.Initialize(raster);
            IImageRaster <IRaster3DInteger, int>   image_labeling = initialization.Item1;
            IImageRaster <IRaster3DInteger, float> image_distance = initialization.Item2;
            IList <int[]>   cluster_spatial_centroids             = initialization.Item3;
            IList <float[]> cluster_feature_centroids             = initialization.Item4;

            Assert.AreEqual(3, cluster_spatial_centroids.Count);
            slick.ComputeCentroids(image_labeling, image_features, cluster_spatial_centroids, cluster_feature_centroids);

            int [] neigbourhood_element_indexes = new int [99];
            for (int iteration_index = 0; iteration_index < iteration_count; iteration_index++)
            {
                slick.AssignElements(image_labeling, image_distance, image_features, cluster_spatial_centroids, cluster_feature_centroids, neigbourhood_element_indexes);
                slick.ComputeCentroids(image_labeling, image_features, cluster_spatial_centroids, cluster_feature_centroids);
            }
            Assert.AreEqual(3, cluster_spatial_centroids.Count);
            for (int element_index = 3; element_index < 12; element_index++)
            {
                Assert.AreEqual(1, feature_image.GetElementValue(element_index));
            }
        }
Beispiel #24
0
        public Bitmap Render(IImageRaster <IRaster3DInteger, ElementType> source_image)
        {
            IRaster3DInteger raster            = source_image.Raster;
            Bitmap           destination_image = new Bitmap(raster.Size0, raster.Size1);

            for (int index_y = 0; index_y < raster.Size1; index_y++)
            {
                for (int index_x = 0; index_x < raster.Size0; index_x++)
                {
                    ElementType max_value = source_image.GetElementValue(raster.GetElementIndex(index_x, index_y, 0));
                    for (int index_z = 1; index_z < raster.Size2; index_z++)
                    {
                        ElementType value = source_image.GetElementValue(raster.GetElementIndex(index_x, index_y, index_z));
                        if (this.comparer.Compare(max_value, value) == -1)
                        {
                            max_value = value;
                        }
                    }
                    destination_image.SetPixel(index_x, index_y, this.converter.Compute(max_value));
                }
            }
            return(destination_image);
        }
Beispiel #25
0
        public Tuple <IImageRaster <IRasterType, int>, IList <int[]>, IList <DomainType> > Cluster(IImageRaster <IRasterType, DomainType> image_features)
        {
            Debug.Assert(image_features.Raster.DimensionCount == d_cluster_dimensions.Length);

            //Initialize cluster spatial centress at regular grid steps S.
            Tuple <IImageRaster <IRasterType, int>, IImageRaster <IRasterType, float>, IList <int[]>, IList <DomainType> > initialization = Initialize(image_features.Raster);
            IImageRaster <IRasterType, int>   image_labeling = initialization.Item1;
            IImageRaster <IRasterType, float> image_distance = initialization.Item2;
            IList <int[]>      cluster_spatial_centroids     = initialization.Item3;
            IList <DomainType> cluster_feature_centroids     = initialization.Item4;

            int dimensions_count = image_features.Raster.DimensionCount;
            int cluster_count    = cluster_spatial_centroids.Count;

            // Find out how big the neighborhood should be
            int count_neigbourhood_elements = 1;

            for (int index_dimension = 0; index_dimension < dimensions_count; index_dimension++)
            {
                count_neigbourhood_elements *= (d_cluster_dimensions[index_dimension] * 2) + 1;
            }
            int[] neigbourhood_element_indexes = new int[count_neigbourhood_elements];


            //Initialize cluster feature centroids at their suposed positions
            ComputeCentroids(image_labeling, image_features, cluster_spatial_centroids, cluster_feature_centroids);

            for (int iteration_index = 0; iteration_index < d_iteration_count; iteration_index++)
            {
                AssignElements(image_labeling, image_distance, image_features, cluster_spatial_centroids, cluster_feature_centroids, neigbourhood_element_indexes);
                ComputeCentroids(image_labeling, image_features, cluster_spatial_centroids, cluster_feature_centroids);
            }


            //TODO postprocessing Remove small isolated components
            return(new Tuple <IImageRaster <IRasterType, int>, IList <int[]>, IList <DomainType> >(image_labeling, cluster_spatial_centroids, cluster_feature_centroids));
        }
Beispiel #26
0
 public ImageSpace3DCast(IImageRaster <IRaster3DInteger, RangeType> image, float[] space_size, RangeType background_value)
 {
     this.image            = image;
     this.space_size       = space_size;
     this.background_value = background_value;
 }
Beispiel #27
0
 public MaxTreeFloat3DFeaturesDouble(IImageRaster <IRaster3DInteger, float> image, FeatureGeneratorElementNode3DDouble generator)
     : this(image, generator, null)
 {
 }
Beispiel #28
0
 public MaxTreeFloat3DFeaturesDouble(IImageRaster <IRaster3DInteger, float> image, IProgressReporter reporter)
     : this(image, null, reporter)
 {
 }
Beispiel #29
0
 public ImageRaster3D(IImageRaster <IRaster2DInteger, RangeType> other, bool copy_values)
     : this(other.Raster.Size0, other.Raster.Size1, 1, other.GetElementValues(copy_values), false)
 {
 }
Beispiel #30
0
 public ImageRaster3D(IImageRaster <IRaster3DInteger, RangeType> other)
     : this(other.Raster, other.GetElementValues(true), false)
 {
 }