/// <summary>
        /// Get the coefficient map of inner structures of tissue for the transform rigidity penalty term.
        /// </summary>
        /// <param name="filename">image filename</param>
        /// <returns>return coefficient map filename</returns>
        private string GetInnerStructureSegmentationsAsCoefficientMap(string filename)
        {
            InnerTissueSegmentation innerSegImage = GetInnerStructureSegmentation(filename);

            string filenameCoefficientMap = ReadWriteUtils.GetOutputDirectory(_parameters, _parameters.Iteration) + Constants.cCoefficientFilename;

            ReadWriteUtils.WriteUMatToFile(filenameCoefficientMap, innerSegImage.GetOutput().FirstOrDefault());
            innerSegImage.Dispose();

            // rescale image
            sitk.Image           img        = ReadWriteUtils.ReadITKImageFromFile(filenameCoefficientMap);
            sitk.CastImageFilter castFilter = new sitk.CastImageFilter();
            castFilter.SetOutputPixelType(sitk.PixelIDValueEnum.sitkFloat32);
            img = castFilter.Execute(img);
            sitk.RescaleIntensityImageFilter filter = new sitk.RescaleIntensityImageFilter();
            filter.SetOutputMinimum(0.0);
            filter.SetOutputMaximum(1.0);
            sitk.Image coefficientMap = filter.Execute(img);

            // save as mhd
            filenameCoefficientMap = ReadWriteUtils.GetOutputDirectory(_parameters, _parameters.Iteration) + Constants.cCoefficientFilename;
            ReadWriteUtils.WriteSitkImage(coefficientMap, filenameCoefficientMap);
            coefficientMap.Dispose();
            return(filenameCoefficientMap);
        }
        private string DoWholeTissueSegmentation(Image <Bgr, byte> img, out Image <Gray, byte> mask, string filename)
        {
            WholeTissueSegmentation seg = new WholeTissueSegmentation(img, registrationParameters.WholeTissueSegParams);

            seg.Execute();
            mask = seg.GetOutput().Clone();
            seg.Dispose();
            string filepathMask = registrationParameters.OutputDirectory + filename;

            ReadWriteUtils.WriteUMatToFile(filepathMask, mask.ToUMat());
            return(filepathMask);
        }
        private string DoInnerTissueSegmentation(Image <Bgr, byte> img, Image <Gray, byte> mask, string filename)
        {
            InnerTissueSegmentation seg = new InnerTissueSegmentation(img.Clone(), mask.Clone(), registrationParameters.InnerStructuresSegParams);

            seg.Execute();
            UMat innerMask = seg.GetOutput()[0].Clone();

            seg.Dispose();
            string filepathResult = registrationParameters.OutputDirectory + filename;

            ReadWriteUtils.WriteUMatToFile(filepathResult, innerMask);
            return(filepathResult);
        }
        /// <summary>
        /// Kmeans segmentation of given image.
        /// 1. Do a kmeans clustering
        /// 2. Find contours
        /// 3. Classification of clusters (?)
        /// </summary>
        /// <param name="uMatChannel"></param>
        private void KmeansSegmentation(UMat uMatChannel)
        {
            if (false)
            {
                Image <Bgr, byte> newImage       = new Image <Bgr, byte>(uMatChannel.Bitmap);
                Image <Bgr, byte> clusteredImage = SegmentationUtils.KMeansClustering <Bgr, byte, byte>(newImage.Clone(), 3, SegmentationUtils._clusterColors);

                ReadWriteUtils.WriteUMatToFile(@"D:\testdata\_temp\kmeans.png", clusteredImage.ToUMat());

                // TODO
                // Find contours
                // Classificate contours
            }

            throw new NotImplementedException();
        }
Ejemplo n.º 5
0
        /// <summary>
        /// Generic method to apply k-Means-Clustering on an opencv image of unknown type.
        /// </summary>
        /// <typeparam name="T">color space type of the image</typeparam>
        /// <typeparam name="V">pixel data type</typeparam>
        /// <typeparam name="K">color / intensities datatype</typeparam>
        /// <param name="image">input image</param>
        /// <param name="k">amount of clusters k</param>
        /// <param name="colors">array of colors used to represent the clusters</param>
        /// <returns>clustered image</returns>
        public static Image <T, V> KMeansClustering <T, V, K>(Image <T, K> image, int k, T[] colors) where T : struct, IColor where K : new() where V : new()
        {
            if (k > 4)
            {
                return(null);
            }

            Image <Bgr, float> src = image.Convert <Bgr, float>();

            Matrix <float> samples       = new Matrix <float>(src.Rows * src.Cols, 1, 3);
            Matrix <int>   finalClusters = new Matrix <int>(src.Rows * src.Cols, 1);

            for (int y = 0; y < src.Rows; y++)
            {
                for (int x = 0; x < src.Cols; x++)
                {
                    samples.Data[y + x * src.Rows, 0] = (float)src[y, x].Blue;
                    samples.Data[y + x * src.Rows, 1] = (float)src[y, x].Green;
                    samples.Data[y + x * src.Rows, 2] = (float)src[y, x].Red;
                }
            }

            MCvTermCriteria criteria = new MCvTermCriteria(50, 0.5);

            criteria.Type = TermCritType.Iter | TermCritType.Eps;

            Matrix <Single> centers     = new Matrix <Single>(k, src.Rows * src.Cols);
            double          compactness = CvInvoke.Kmeans(samples, k, finalClusters, criteria, 3, KMeansInitType.RandomCenters);

            Image <T, V> kMeansImage = new Image <T, V>(src.Size);

            for (int y = 0; y < src.Rows; y++)
            {
                for (int x = 0; x < src.Cols; x++)
                {
                    PointF p = new PointF(x, y);
                    kMeansImage.Draw(new CircleF(p, 1.0f), colors[finalClusters[y + x * src.Rows, 0]], 1);
                }
            }
            // Debug
            ReadWriteUtils.WriteUMatToFile(ApplicationContext.OutputPath + "kmeans_new.png", kMeansImage.ToUMat());
            return(kMeansImage);
        }