public static SemiAutomaticPreviewDTO CannyWithoutStatistics(SemiAutomaticPreviewDTO points)
        {
            List <Point> pixels;

            pixels = CannyAlgorithm.CannyWithoutStatistics(points.dicomid, points.lines.First().points, points.width, points.height);

            List <LinePointsAndPixels> lines = new List <LinePointsAndPixels>();
            LinePointsAndPixels        line  = new LinePointsAndPixels();

            line.points     = new List <Point>(points.lines.First().points);
            line.pixels     = new List <Point>(pixels);
            line.brushColor = points.lines.First().brushColor;

            lines.Add(line);

            SemiAutomaticPreviewDTO contour = new SemiAutomaticPreviewDTO(points.guid,
                                                                          points.dicomid, points.tag, lines, points.width, points.height, points.pixelSpacing, false);

            return(contour);
        }
        public static SemiAutomaticContourDTO Canny(SemiAutomaticPointsDTO points)
        {
            List <Point>     pixels;
            StatisticsResult statisticsResult;

            (pixels, statisticsResult) = CannyAlgorithm.Canny(points.dicomid, points.lines.First().points, points.width, points.height,
                                                              points.centralPoints, points.pixelSpacing);

            List <LinePointsAndPixels> lines = new List <LinePointsAndPixels>();
            LinePointsAndPixels        line  = new LinePointsAndPixels();

            line.points     = new List <Point>(points.lines.First().points);
            line.pixels     = new List <Point>(pixels);
            line.brushColor = points.lines.First().brushColor;

            lines.Add(line);

            SemiAutomaticContourDTO contour = new SemiAutomaticContourDTO(points.guid,
                                                                          points.dicomid, points.tag, lines, points.width, points.height, statisticsResult, points.centralPoints, points.pixelSpacing);

            return(contour);
        }
        public static SemiAutomaticContourDTO TrivialContour(SemiAutomaticPointsDTO points)
        {
            List <Point> pixels = new List <Point>();
            int          count  = points.lines.First().points.Count;

            for (int i = 0; i < points.lines.First().points.Count; i++)
            {
                int          x1 = points.lines.First().points[i].x;
                int          y1 = points.lines.First().points[i].y;
                int          x2 = points.lines.First().points[(i + 1) % count].x;
                int          y2 = points.lines.First().points[(i + 1) % count].y;
                List <Point> pixelsBresenham = new List <Point>();
                BresenhamClass.Bresenham(pixelsBresenham, x1, y1, x2, y2);
                pixels = pixels.Concat(pixelsBresenham).ToList();
            }

            List <LinePointsAndPixels> lines = new List <LinePointsAndPixels>();
            LinePointsAndPixels        line  = new LinePointsAndPixels();

            line.points     = new List <Point>(points.lines.First().points);
            line.pixels     = new List <Point>(pixels);
            line.brushColor = points.lines.First().brushColor;

            lines.Add(line);

            System.Drawing.Bitmap bitmap = OrthancConnection.GetBitmapByInstanceId(points.dicomid);
            int[,] matrixWithContour = CannyAlgorithm.MakeMatrixFromPoints(bitmap.Width, bitmap.Height, pixels);
            int[,] image             = CannyAlgorithm.ReadMatrixFromBitmap(bitmap);

            StatisticsResult statisticsResult = Statistics.GenerateStatistics(pixels, matrixWithContour, image, 0, bitmap.Width, 0, bitmap.Height,
                                                                              0, 0, points.centralPoints.First());

            SemiAutomaticContourDTO contour = new SemiAutomaticContourDTO(points.guid,
                                                                          points.dicomid, points.tag, lines, points.width, points.height, statisticsResult, points.centralPoints, points.pixelSpacing);

            return(contour);
        }
Exemplo n.º 4
0
        public (bool, SemiAutomaticPreviewDTO) Edit(SemiAutomaticPreviewDTO contour)
        {
            SemiAutomaticPreviewDTO old = repository.Load(contour.guid);

            if (old == null)
            {
                return(false, null);
            }

            List <Point> newListOfPoints = new List <Point>();

            int i        = 0;
            int j        = 0;
            int countOld = old.lines.First().points.Count;
            int countNew = contour.lines.First().points.Count;

            Point currentInOld = old.lines.First().points[i];
            Point currentInNew = contour.lines.First().points[j];

            while (i < countOld)
            {
                currentInOld = old.lines.First().points[i];
                currentInNew = contour.lines.First().points[j];
                while (currentInOld.x != currentInNew.x || currentInOld.y != currentInNew.y)
                {
                    i++;
                    if (i == countOld)
                    {
                        break;
                    }
                    currentInOld = old.lines.First().points[i];
                }
                newListOfPoints.Add(currentInOld);
                i++;
                j++;
                if (j == countNew)
                {
                    break;
                }
            }
            while (j < countNew)
            {
                currentInNew = contour.lines.First().points[j];
                double minDistance = double.MaxValue;
                int    index       = 0;

                for (int k = 0; k < newListOfPoints.Count; k++)
                {
                    Point point1 = newListOfPoints[k];
                    Point point2 = newListOfPoints[(k + 1) % newListOfPoints.Count];

                    double A = point2.y - point1.y;
                    double B = point1.x - point2.x;
                    double C = point2.x * point1.y - point1.x * point2.y;

                    double m        = Math.Sqrt(A * A + B * B);
                    double distance = Math.Abs(A * currentInNew.x + B * currentInNew.y + C) / m;

                    double dy = Math.Abs(point2.y - point1.y);
                    double dx = Math.Abs(point2.x - point1.x);

                    bool inside = true;

                    if (currentInNew.x > Math.Max(point1.x, point2.x) + (dx + dy) / 2)
                    {
                        inside = false;
                    }
                    if (currentInNew.x < Math.Min(point1.x, point2.x) - (dx + dy) / 2)
                    {
                        inside = false;
                    }
                    if (currentInNew.y > Math.Max(point1.y, point2.y) + (dy + dx) / 2)
                    {
                        inside = false;
                    }
                    if (currentInNew.y < Math.Min(point1.y, point2.y) - (dy + dx) / 2)
                    {
                        inside = false;
                    }

                    if (distance < minDistance && inside)
                    {
                        minDistance = distance;
                        index       = k + 1;
                    }
                }

                newListOfPoints.Insert(index, currentInNew);
                j++;
            }

            contour.lines.First().points = new List <Point>(newListOfPoints);

            List <LinePointsAndPixels> list = new List <LinePointsAndPixels>();
            LinePointsAndPixels        line = new LinePointsAndPixels();

            line.points     = new List <Point>(newListOfPoints);
            line.pixels     = old.lines.First().pixels;
            line.brushColor = contour.lines.First().brushColor;
            list.Add(line);

            SemiAutomaticPreviewDTO contourPointsDTO = new SemiAutomaticPreviewDTO(contour.guid, contour.dicomid, contour.tag, list, contour.width, contour.height,
                                                                                   contour.pixelSpacing, contour.disablePreviewCalculations);
            SemiAutomaticPreviewDTO result = contourPointsDTO;

            if (!contour.disablePreviewCalculations)
            {
                result = SemiAutomatic.Default(contourPointsDTO);
            }

            if (repository.Edit(result))
            {
                return(true, result);
            }
            else
            {
                return(false, result);
            }
        }
Exemplo n.º 5
0
        public SemiAutomaticContourDTO Load(Guid guid)
        {
            using (var db = new ContourContext())
            {
                if (db.Contours.Where(c => c.ContourEntityId == guid).ToList().Count == 0)
                {
                    return(null);
                }
            }

            string DICOMid;
            string tag;
            List <LinePointsAndPixels> lines = new List <LinePointsAndPixels>();
            int width;
            int height;

            string buffor;

            string       filename = "../data/semiautomatic/" + guid.ToString() + ".csv";
            StreamReader sr       = null;

            try
            {
                sr = new StreamReader(filename);
            }
            catch (Exception)
            {
                return(null);
            }
            if (sr.EndOfStream)
            {
                throw new Exception($"Unexpected end of file {filename}");
            }

            buffor = sr.ReadLine();
            if (Guid.Parse(buffor) != guid)
            {
                throw new Exception($"Guid in file diffrent that in name of file {filename}");
            }
            if (sr.EndOfStream)
            {
                throw new Exception($"Unexpected end of file {filename}");
            }

            DICOMid = sr.ReadLine();
            if (sr.EndOfStream)
            {
                throw new Exception($"Unexpected end of file {filename}");
            }

            tag = sr.ReadLine();
            if (sr.EndOfStream)
            {
                throw new Exception($"Unexpected end of file {filename}");
            }

            LinePointsAndPixels line = new LinePointsAndPixels();

            line.pixels = new List <Point>();
            line.points = new List <Point>();

            buffor = sr.ReadLine();
            List <int> points = buffor.Split(',').Select(s => int.Parse(s)).ToList();
            int        i      = 0;

            while (i + 1 < points.Count)
            {
                line.points.Add(new Point(points[i++], points[i++]));
            }

            //It the same as:
            // while(i + 1 < points.Count)
            // {
            //     line.pixels.Add(new Point(points[i], points[i+1]));
            //     i += 2;
            // }
            // But it's look more funny

            buffor = sr.ReadLine();
            points = buffor.Split(',').Select(s => int.Parse(s)).ToList();

            while (i + 1 < points.Count)
            {
                line.pixels.Add(new Point(points[i++], points[i++]));
            }

            line.brushColor = sr.ReadLine();
            if (sr.EndOfStream)
            {
                throw new Exception($"Unexpected end of file {filename}");
            }

            lines.Add(line);

            buffor = sr.ReadLine();
            width  = int.Parse(buffor);
            if (sr.EndOfStream)
            {
                throw new Exception($"Unexpected end of file {filename}");
            }

            buffor = sr.ReadLine();
            height = int.Parse(buffor);

            if (sr.EndOfStream)
            {
                throw new Exception($"Unexpected end of file {filename}");
            }

            string pixelSpacing = sr.ReadLine();

            if (sr.EndOfStream)
            {
                throw new Exception($"Unexpected end of file {filename}");
            }

            StatisticsResult statisticsResult = new StatisticsResult();
            List <Point>     centralPoints    = new List <Point>();

            buffor = sr.ReadLine();
            if (buffor != "")
            {
                List <int> list = buffor.Split(',').Select(s => int.Parse(s)).ToList();
                statisticsResult.CenterOfMass = new Point(list[0], list[1]);
                if (sr.EndOfStream)
                {
                    throw new Exception($"Unexpected end of file {filename}");
                }

                buffor = sr.ReadLine();
                statisticsResult.Histogram = buffor.Split(',').Select(s => int.Parse(s)).ToArray();
                if (sr.EndOfStream)
                {
                    throw new Exception($"Unexpected end of file {filename}");
                }

                buffor = sr.ReadLine();
                statisticsResult.HistogramMin = int.Parse(buffor);
                if (sr.EndOfStream)
                {
                    throw new Exception($"Unexpected end of file {filename}");
                }

                buffor = sr.ReadLine();
                statisticsResult.HistogramMax = int.Parse(buffor);
                if (sr.EndOfStream)
                {
                    throw new Exception($"Unexpected end of file {filename}");
                }

                buffor = sr.ReadLine();
                statisticsResult.HistogramMean = double.Parse(buffor);
                if (sr.EndOfStream)
                {
                    throw new Exception($"Unexpected end of file {filename}");
                }

                buffor = sr.ReadLine();
                statisticsResult.Area = double.Parse(buffor);
                if (sr.EndOfStream)
                {
                    throw new Exception($"Unexpected end of file {filename}");
                }

                buffor = sr.ReadLine();
                statisticsResult.Permieter = double.Parse(buffor);
                if (sr.EndOfStream)
                {
                    throw new Exception($"Unexpected end of file {filename}");
                }

                buffor = sr.ReadLine();
                statisticsResult.NumberOfPixelsInsideContour = int.Parse(buffor);
                if (sr.EndOfStream)
                {
                    throw new Exception($"Unexpected end of file {filename}");
                }

                buffor = sr.ReadLine();
                statisticsResult.NumberOfPixelsOfContour = int.Parse(buffor);
                if (sr.EndOfStream)
                {
                    throw new Exception($"Unexpected end of file {filename}");
                }

                buffor = sr.ReadLine();
                points = buffor.Split(',').Select(s => int.Parse(s)).ToList();
                i      = 0;

                while (i + 1 < points.Count)
                {
                    centralPoints.Add(new Point(points[i++], points[i++]));
                }
            }

            sr.Close();

            SemiAutomaticContourDTO contour = new SemiAutomaticContourDTO(guid, DICOMid, tag, lines, width, height, statisticsResult, centralPoints, pixelSpacing);

            return(contour);
        }
        public SemiAutomaticPreviewDTO Load(Guid guid)
        {
            using (var db = new ContourContext())
            {
                if (db.Contours.Where(c => c.ContourEntityId == guid).ToList().Count == 0)
                {
                    return(null);
                }
            }

            string DICOMid;
            string tag;
            List <LinePointsAndPixels> lines = new List <LinePointsAndPixels>();
            int width;
            int height;

            string buffor;

            string filename = "../data/preview/" + guid.ToString() + ".csv";

            StreamReader sr = null;

            try
            {
                sr = new StreamReader(filename);
            }
            catch (Exception)
            {
                return(null);
            }

            if (sr.EndOfStream)
            {
                throw new Exception($"Unexpected end of file {filename}");
            }

            buffor = sr.ReadLine();
            if (Guid.Parse(buffor) != guid)
            {
                throw new Exception($"Guid in file diffrent that in name of file {filename}");
            }
            if (sr.EndOfStream)
            {
                throw new Exception($"Unexpected end of file {filename}");
            }

            DICOMid = sr.ReadLine();
            if (sr.EndOfStream)
            {
                throw new Exception($"Unexpected end of file {filename}");
            }

            tag = sr.ReadLine();
            if (sr.EndOfStream)
            {
                throw new Exception($"Unexpected end of file {filename}");
            }

            LinePointsAndPixels line = new LinePointsAndPixels();

            line.pixels = new List <Point>();
            line.points = new List <Point>();

            buffor = sr.ReadLine();
            List <int> points = buffor.Split(',').Select(s => int.Parse(s)).ToList();
            int        i      = 0;

            while (i + 1 < points.Count)
            {
                line.points.Add(new Point(points[i++], points[i++]));
            }

            //It the same as:
            // while(i + 1 < points.Count)
            // {
            //     line.pixels.Add(new Point(points[i], points[i+1]));
            //     i += 2;
            // }
            // But it's look more funny

            buffor = sr.ReadLine();
            points = buffor.Split(',').Select(s => int.Parse(s)).ToList();

            while (i + 1 < points.Count)
            {
                line.pixels.Add(new Point(points[i++], points[i++]));
            }

            line.brushColor = sr.ReadLine();
            if (sr.EndOfStream)
            {
                throw new Exception($"Unexpected end of file {filename}");
            }

            lines.Add(line);

            buffor = sr.ReadLine();
            width  = int.Parse(buffor);
            if (sr.EndOfStream)
            {
                throw new Exception($"Unexpected end of file {filename}");
            }

            buffor = sr.ReadLine();
            height = int.Parse(buffor);
            if (sr.EndOfStream)
            {
                throw new Exception($"Unexpected end of file {filename}");
            }

            string pixelSpacing = sr.ReadLine();

            sr.Close();

            SemiAutomaticPreviewDTO contour = new SemiAutomaticPreviewDTO(guid, DICOMid, tag, lines, width, height, pixelSpacing, true);

            return(contour);
        }