Example #1
0
    // Krijon Pathin
    private void CreatePath(int x, int y, Contour pathToSave, float[,] matrix)
    {
        // Vendosim ngjyren e kuqe
        matrix[x, y] = -1f;
        // Shtojm kete pike tek konturi
        pathToSave.AddPoint(x, y);
        // Check per posicionin te kesaj pike
        if (x < ImageWidth - 1 && y < ImageHeight - 1 && x > 0 && y > 0)
        {
            // Zgjedhim drejtimin per te vazhduar konturin (djathtas)
            if (matrix[x + 1, y] == 1)
            {
                // Check nqs eshte bordure
                if (isBorder(x + 1, y, matrix))
                {
                    // Therret ne rekursiv
                    CreatePath(x + 1, y, pathToSave, matrix);
                }
                else
                {
                    // Check nqs eshte nje pike e brendshme afer nje konture dhe drejtimin e tij
                    int nextBorderValue = IsABorderContinued(x + 1, y, matrix);
                    // ka nje drejtim
                    if (nextBorderValue != 0)
                    {
                        // Zgjedhim ku te vazhdojm te kerkojm per ndonje pike te konturit
                        if (nextBorderValue == 1)
                        {
                            CreatePath(x + 2, y, pathToSave, matrix);
                        }
                        if (nextBorderValue == 2)
                        {
                            CreatePath(x + 1, y + 1, pathToSave, matrix);
                        }
                        if (nextBorderValue == -2)
                        {
                            CreatePath(x + 1, y - 1, pathToSave, matrix);
                        }
                    }
                }
            }
            // vazhdojm dhe me pikat e tjera ne drejtimet e mbetura (majtas)
            if (matrix[x - 1, y] == 1)
            {
                if (isBorder(x - 1, y, matrix))
                {
                    CreatePath(x - 1, y, pathToSave, matrix);
                }
                else
                {
                    int nextBorderValue = IsABorderContinued(x - 1, y, matrix);

                    if (nextBorderValue != 0)
                    {
                        if (nextBorderValue == -1)
                        {
                            CreatePath(x - 2, y, pathToSave, matrix);
                        }
                        if (nextBorderValue == 2)
                        {
                            CreatePath(x - 1, y + 1, pathToSave, matrix);
                        }
                        if (nextBorderValue == -2)
                        {
                            CreatePath(x - 1, y - 1, pathToSave, matrix);
                        }
                    }
                }
            }
            // (siper)
            if (matrix[x, y + 1] == 1)
            {
                if (isBorder(x, y + 1, matrix))
                {
                    CreatePath(x, y + 1, pathToSave, matrix);
                }
                else
                {
                    int nextBorderValue = IsABorderContinued(x, y + 1, matrix);

                    if (nextBorderValue != 0)
                    {
                        if (nextBorderValue == 1)
                        {
                            CreatePath(x + 1, y + 1, pathToSave, matrix);
                        }
                        if (nextBorderValue == -1)
                        {
                            CreatePath(x - 1, y + 1, pathToSave, matrix);
                        }
                        if (nextBorderValue == 2)
                        {
                            CreatePath(x, y + 2, pathToSave, matrix);
                        }
                    }
                }
            }
            // (posht)
            if (matrix[x, y - 1] == 1)
            {
                if (isBorder(x, y - 1, matrix))
                {
                    CreatePath(x, y - 1, pathToSave, matrix);
                }
                else
                {
                    int nextBorderValue = IsABorderContinued(x, y - 1, matrix);

                    if (nextBorderValue != 0)
                    {
                        if (nextBorderValue == 1)
                        {
                            CreatePath(x + 1, y - 1, pathToSave, matrix);
                        }
                        if (nextBorderValue == -1)
                        {
                            CreatePath(x - 1, y - 1, pathToSave, matrix);
                        }
                        if (nextBorderValue == -2)
                        {
                            CreatePath(x, y - 2, pathToSave, matrix);
                        }
                    }
                }
            }
        }
    }
Example #2
0
    private void CreatePath(int x, int y, Contour pathToSave)
    {
        matrix[x, y] = -1f;

        pathToSave.AddPoint(x, y);

        if (x < width - 1 && y < height - 1 && x > 0 && y > 0)
        {
            if (matrix[x + 1, y] == 1)
            {
                if (isBorder(x + 1, y))
                {
                    CreatePath(x + 1, y, pathToSave);
                }
                else
                {
                    int nextBorderValue = IsABorderContinued(x + 1, y);

                    if (nextBorderValue != 0)
                    {
                        if (nextBorderValue == 1)
                        {
                            CreatePath(x + 2, y, pathToSave);
                        }
                        if (nextBorderValue == 2)
                        {
                            CreatePath(x + 1, y + 1, pathToSave);
                        }
                        if (nextBorderValue == -2)
                        {
                            CreatePath(x + 1, y - 1, pathToSave);
                        }
                    }
                }
            }

            if (matrix[x - 1, y] == 1)
            {
                if (isBorder(x - 1, y))
                {
                    CreatePath(x - 1, y, pathToSave);
                }
                else
                {
                    int nextBorderValue = IsABorderContinued(x - 1, y);

                    if (nextBorderValue != 0)
                    {
                        if (nextBorderValue == -1)
                        {
                            CreatePath(x - 2, y, pathToSave);
                        }
                        if (nextBorderValue == 2)
                        {
                            CreatePath(x - 1, y + 1, pathToSave);
                        }
                        if (nextBorderValue == -2)
                        {
                            CreatePath(x - 1, y - 1, pathToSave);
                        }
                    }
                }
            }

            if (matrix[x, y + 1] == 1)
            {
                if (isBorder(x, y + 1))
                {
                    CreatePath(x, y + 1, pathToSave);
                }
                else
                {
                    int nextBorderValue = IsABorderContinued(x, y + 1);

                    if (nextBorderValue != 0)
                    {
                        if (nextBorderValue == 1)
                        {
                            CreatePath(x + 1, y + 1, pathToSave);
                        }
                        if (nextBorderValue == -1)
                        {
                            CreatePath(x - 1, y + 1, pathToSave);
                        }
                        if (nextBorderValue == 2)
                        {
                            CreatePath(x, y + 2, pathToSave);
                        }
                    }
                }
            }

            if (matrix[x, y - 1] == 1)
            {
                if (isBorder(x, y - 1))
                {
                    CreatePath(x, y - 1, pathToSave);
                }
                else
                {
                    int nextBorderValue = IsABorderContinued(x, y - 1);

                    if (nextBorderValue != 0)
                    {
                        if (nextBorderValue == 1)
                        {
                            CreatePath(x + 1, y - 1, pathToSave);
                        }
                        if (nextBorderValue == -1)
                        {
                            CreatePath(x - 1, y - 1, pathToSave);
                        }
                        if (nextBorderValue == -2)
                        {
                            CreatePath(x, y - 2, pathToSave);
                        }
                    }
                }
            }
        }
    }
Example #3
0
        private static RtStructureDicom ReadFile(string sDicomFile)
        {
            DicomFile df      = DicomFile.Open(sDicomFile);
            var       dataset = df.Dataset;

            var modality = dataset.Get <string>(DicomTag.Modality);

            if (!modality.ToUpper().Contains("RTSTR"))
            {
                throw new ArgumentException("No RT Structure file provided.");
            }

            string ssName = String.Empty;

            try
            {
                ssName = dataset.Get <string>(DicomTag.StructureSetName);
            }
            catch (Exception e)
            {
                Trace.WriteLine(e);
            }

            var ssObject = new StructureSet(ssName);

            RtStructureDicom rtStructureDicom = new RtStructureDicom(ssObject);

            var ssFrameOfReferenceSequence = dataset.Get <DicomSequence>(DicomTag.ReferencedFrameOfReferenceSequence);

            foreach (DicomDataset item in ssFrameOfReferenceSequence.Items)
            {
                try
                {
                    var id = item.Get <string>(DicomTag.FrameOfReferenceUID);
                    ssObject.AddFrameOfReferenceUid(id);
                }
                catch (Exception e)
                {
                    Trace.WriteLine(e);
                }
            }

            var structureSetRoiSequence = dataset.Get <DicomSequence>(DicomTag.StructureSetROISequence);

            foreach (DicomDataset item in structureSetRoiSequence.Items)
            {
                var name  = item.Get <string>(DicomTag.ROIName);
                var roiId = item.Get <int>(DicomTag.ROINumber);
                //Trace.WriteLine(item.Get<string>(DicomTag.ROIName));
                Structure structure = new Structure(name, roiId);
                ssObject.Add(structure);
            }

            var roiContourSequence = dataset.Get <DicomSequence>(DicomTag.ROIContourSequence);

            foreach (DicomDataset item in roiContourSequence.Items)
            {
                try
                {
                    var       roiColor        = item.Get <int[]>(DicomTag.ROIDisplayColor);
                    var       contourSequence = item.Get <DicomSequence>(DicomTag.ContourSequence);
                    var       roiId           = item.Get <int>(DicomTag.ReferencedROINumber);
                    Structure structure       = ssObject.GetStructureById(roiId);
                    structure.Color = Color.FromRgb((byte)roiColor[0], (byte)roiColor[1], (byte)roiColor[2]);
                    foreach (DicomDataset contourItem in contourSequence.Items)
                    {
                        Contour contourOnAPlane      = new Contour();
                        int     numerOfContourPoints = contourItem.Get <int>(DicomTag.NumberOfContourPoints);
                        var     contourData          = contourItem.Get <float[]>(DicomTag.ContourData);
                        Debug.Assert(contourData.Length == numerOfContourPoints * 3);

                        contourOnAPlane.SetZPosition(contourData[2]);
                        for (int i = 0; i < contourData.Length; i = i + 3)
                        {
                            float x = contourData[i];
                            float y = contourData[i + 1];
                            float z = contourData[i + 2];

                            contourOnAPlane.AddPoint(x, y, z);
                        }
                        structure.AddContour(contourOnAPlane);
                    }
                }
                catch (Exception e)
                {
                    Trace.WriteLine(e);
                }
            }
            return(rtStructureDicom);
        }