예제 #1
0
        public RangeType GetElementValue(int element_index)
        {
            int[] coordinates_x_y     = Raster.GetElementCoordinates(element_index);
            int   element_index_inner = wrapped_image.Raster.GetElementIndex(coordinates_x_y[0], coordinates_x_y[1], index_2, index_3);

            return(wrapped_image.GetElementValue(element_index_inner));
        }
예제 #2
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);
        }
예제 #3
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--;
                }
            }
        }
예제 #4
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);
        }
예제 #5
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);
                    }
                }
            }
        }
예제 #6
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);
        }
예제 #7
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);
                        }
                    }
                }
            }
        }
예제 #8
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);
        }
예제 #9
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));
        }
예제 #10
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));
        }
예제 #11
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 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);
                }
            });
        }
예제 #13
0
        public static void SaveImageRaster3DAsTIFF163D(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[][] page_buffer = new byte[size_y][];
            for (int index_y = 0; index_y < size_y; index_y++)
            {
                page_buffer[index_y] = new byte[size_x * 2];
            }

            using (Tiff output = Tiff.Open(save_path, "w"))
            {
                for (int index_z = 0; index_z < size_z; ++index_z)
                {
                    output.SetField(TiffTag.IMAGEWIDTH, size_x / samplesPerPixel);
                    output.SetField(TiffTag.IMAGELENGTH, size_y);
                    output.SetField(TiffTag.COMPRESSION, Compression.NONE);
                    output.SetField(TiffTag.SAMPLESPERPIXEL, samplesPerPixel);
                    output.SetField(TiffTag.BITSPERSAMPLE, bitsPerSample);
                    output.SetField(TiffTag.ORIENTATION, Orientation.TOPLEFT);
                    output.SetField(TiffTag.PLANARCONFIG, PlanarConfig.CONTIG);

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

                    output.SetField(TiffTag.ROWSPERSTRIP, 1);
                    output.SetField(TiffTag.XRESOLUTION, 59.0);
                    output.SetField(TiffTag.YRESOLUTION, 59.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, index_z, size_z);


                    for (int index_y = 0; index_y < size_y; index_y++)
                    {
                        for (int index_x = 0; index_x < size_x; index_x++)
                        {
                            byte[] bytes = BitConverter.GetBytes(image.GetElementValue(image.Raster.GetElementIndex(index_x, index_y, 0)));
                            page_buffer[index_y][(index_x * 2)]     = bytes[1];
                            page_buffer[index_y][(index_x * 2) + 1] = bytes[0];
                        }
                    }
                    for (int index_y = 0; index_y < size_y; ++index_y)
                    {
                        output.WriteEncodedStrip(index_y, page_buffer[index_y], page_buffer[index_y].Length);
                    }
                    output.WriteDirectory();
                }
            }
        }