Exemple #1
0
        static void Main(string[] args)
        {
            string file = "C:\\Users\\nydzi\\Downloads\\database\\DATA_DICOM\\drsprg_053\\drsprg_053_ANT.dcm";

            gdcm.PixmapReader reader = new gdcm.PixmapReader();
            reader.SetFileName(file);
            if (!reader.Read())
            {
                Console.WriteLine("pomijam: {0}", file);
            }

            var dcm       = DICOMObject.Read(file);
            var pxSpacing = dcm.FindFirst(TagHelper.PixelSpacing);

            Console.WriteLine("Rozmiar piksela: {0}mm x {1}mm", pxSpacing.DData_[0], pxSpacing.DData_[1]);

            gdcm.Bitmap bmjpeg2000 = pxmap2jpeg2000(reader.GetPixmap());
            Bitmap[]    X          = gdcmBitmap2Bitmap(bmjpeg2000);

            for (int i = 0; i < X.Length; i++)
            {
                String name = String.Format("{0}_warstwa{1}.bmp", file, i);
                X[i].Save(name);
                Console.WriteLine("konwersja do: {0}", name);
            }
        }
Exemple #2
0
    public static Texture2D[] ConvertToTexture(string file)
    {
        gdcm.PixmapReader reader = new gdcm.PixmapReader();
        reader.SetFileName(file);
        if (!reader.Read())
        {
            Console.WriteLine("pomijam: {0}", file);
        }

        gdcm.Bitmap bmjpeg2000 = pxmap2jpeg2000(reader.GetPixmap());
        Bitmap[]    X          = gdcmBitmap2Bitmap(bmjpeg2000);


        Texture2D[] textures = new Texture2D[X.Length];
        for (int i = 0; i < X.Length; i++)
        {
            MemoryStream ms = new MemoryStream();
            X[i].Save(ms, ImageFormat.Png);
            var buffer = new byte[ms.Length];
            ms.Position = 0;
            ms.Read(buffer, 0, buffer.Length);
            Texture2D t = new Texture2D(1, 1);
            t.LoadImage(buffer);
            textures[i] = t;
        }
        return(textures);
    }
Exemple #3
0
        public List <string> GetImages(string patientId)
        {
            var    images       = new List <string>();
            string resultString = string.Empty;

            gdcm.ERootType typ = gdcm.ERootType.ePatientRootType;

            gdcm.EQueryLevel poziom = gdcm.EQueryLevel.ePatient;

            gdcm.KeyValuePairArrayType klucze = new gdcm.KeyValuePairArrayType();
            gdcm.KeyValuePairType      klucz1 = new gdcm.KeyValuePairType(new gdcm.Tag(0x0010, 0x0020), patientId);
            klucze.Add(klucz1);

            gdcm.BaseRootQuery zapytanie = gdcm.CompositeNetworkFunctions.ConstructQuery(typ, poziom, klucze, true);

            string odebrane = System.IO.Path.Combine(".", "odebrane");

            if (!System.IO.Directory.Exists(odebrane))
            {
                System.IO.Directory.CreateDirectory(odebrane);
            }
            string dane = System.IO.Path.Combine(odebrane, System.IO.Path.GetRandomFileName());

            System.IO.Directory.CreateDirectory(dane);

            bool stan = gdcm.CompositeNetworkFunctions.CMove(this.pacsConfiguration.ipPACS,
                                                             this.pacsConfiguration.portPACS,
                                                             zapytanie, this.pacsConfiguration.portMove,
                                                             this.pacsConfiguration.myAET,
                                                             this.pacsConfiguration.callAET,
                                                             dane);


            List <string> pliki = new List <string>(System.IO.Directory.EnumerateFiles(dane));

            foreach (string plik in pliki)
            {
                gdcm.PixmapReader reader = new gdcm.PixmapReader();
                reader.SetFileName(plik);
                if (!reader.Read())
                {
                    // do not remove!!!!
                    continue;
                }

                gdcm.Bitmap bmjpeg2000 = BitmapCoder.pxmap2jpeg2000(reader.GetPixmap());
                Bitmap[]    X          = BitmapCoder.gdcmBitmap2Bitmap(bmjpeg2000);
                for (int i = 0; i < X.Length; i++)
                {
                    string name = String.Format("{0}_warstwa{1}.jpg", plik, i);
                    X[i].Save(name);
                    images.Add(name);
                }
            }
            return(images);
        }
Exemple #4
0
        public static Bitmap[] gdcmBitmap2Bitmap(gdcm.Bitmap bmjpeg2000)
        {
            // przekonwertuj teraz na bitmapę C#
            uint cols = bmjpeg2000.GetColumns();
            uint rows = bmjpeg2000.GetRows();

            // wartość zwracana - tyle obrazków, ile warstw
            Bitmap[] ret = new Bitmap[1];


            // bufor
            byte[] bufor = new byte[bmjpeg2000.GetBufferLength()];
            if (!bmjpeg2000.GetBuffer(bufor))
            {
                throw new Exception("błąd pobrania bufora");
            }

            // w strumieniu na każdy piksel 2 bajty; tutaj LittleEndian (mnie znaczący bajt wcześniej)

            Bitmap X = new Bitmap((int)rows / 3, (int)cols / 3);

            double[,] Y = new double[rows, cols];
            double m      = 0;
            int    stride = (int)cols * 4;
            int    size   = (int)rows * stride;

            for (int r = 0; r < bufor.Length - 1; r++)
            {
                // przeskalujemy potem do wartości max.
                if (bufor[r] * 256 + bufor[r + 1] > m)
                {
                    m = bufor[r] * 256 + bufor[r + 1];
                }
            }

            // wolniejsza metoda tworzenia bitmapy
            for (int r = 0; r < rows / 3; r++)
            {
                for (int c = 0; c < cols / 3; c++)
                {
                    int index = c * 3 * stride + 3 * r * 3;

                    if (index + 2 < bufor.Length)
                    {
                        int red   = (int)(255 * (bufor[index] * 256 + bufor[index + 1]) / m);
                        int green = (int)(255 * (bufor[index + 1] * 256 + bufor[index + 2]) / m);
                        int blue  = (int)(255 * (bufor[index + 2] * 256 + bufor[index + 3]) / m);
                        X.SetPixel(r, c, Color.FromArgb(red, green, blue));
                    }
                }
            }
            // kolejna bitmapa
            ret[0] = X;

            return(ret);
        }
Exemple #5
0
        public static Bitmap[] gdcmBitmap2Bitmap(gdcm.Bitmap bmjpeg2000)
        {
            // przekonwertuj teraz na bitmapę C#
            uint cols   = bmjpeg2000.GetDimension(0);
            uint rows   = bmjpeg2000.GetDimension(1);
            uint layers = bmjpeg2000.GetDimension(2);

            // wartość zwracana - tyle obrazków, ile warstw
            Bitmap[] ret = new Bitmap[layers];


            // bufor
            byte[] bufor = new byte[bmjpeg2000.GetBufferLength()];
            if (!bmjpeg2000.GetBuffer(bufor))
            {
                throw new Exception("błąd pobrania bufora");
            }

            // w strumieniu na każdy piksel 2 bajty; tutaj LittleEndian (mnie znaczący bajt wcześniej)
            for (uint l = 0; l < layers; l++)
            {
                Bitmap X = new Bitmap((int)cols, (int)rows);
                double[,] Y = new double[cols, rows];
                double m = 0;

                for (int r = 0; r < rows; r++)
                {
                    for (int c = 0; c < cols; c++)
                    {
                        // współrzędne w strumieniu
                        int j = ((int)(l * rows * cols) + (int)(r * cols) + (int)c) * 2;
                        Y[r, c] = (double)bufor[j + 1] * 256 + (double)bufor[j];
                        // przeskalujemy potem do wartości max.
                        if (Y[r, c] > m)
                        {
                            m = Y[r, c];
                        }
                    }
                }

                // wolniejsza metoda tworzenia bitmapy
                for (int r = 0; r < rows; r++)
                {
                    for (int c = 0; c < cols; c++)
                    {
                        int f = (int)(255 * (Y[r, c] / m));
                        X.SetPixel(c, r, Color.FromArgb(f, f, f));
                    }
                }
                // kolejna bitmapa
                ret[l] = X;
            }
            return(ret);
        }
Exemple #6
0
        // przekonwertuj do formatu bezstratnego JPEG2000
        // na podstawie: http://gdcm.sourceforge.net/html/StandardizeFiles_8cs-example.html
        public static gdcm.Bitmap pxmap2jpeg2000(gdcm.Pixmap px)
        {
            gdcm.ImageChangeTransferSyntax change = new gdcm.ImageChangeTransferSyntax();
            change.SetForce(false);
            change.SetCompressIconImage(false);
            change.SetTransferSyntax(new gdcm.TransferSyntax(gdcm.TransferSyntax.TSType.JPEG2000Lossless));

            change.SetInput(px);
            if (!change.Change())
            {
                throw new Exception("Nie przekonwertowano typu bitmapy na jpeg2000");
            }

            gdcm.Bitmap outimg = change.GetOutputAsBitmap(); // dla GDCM.3.0.4

            return(outimg);                                  //change.GetOutput(); // tak było w starszych wersjach
        }
Exemple #7
0
        public static System.Drawing.Bitmap[] gdcmBitmap2Bitmap(gdcm.Bitmap bmjpeg2000)
        {
            uint columns = bmjpeg2000.GetDimension(0);
            uint rows    = bmjpeg2000.GetDimension(1);

            uint layers = bmjpeg2000.GetDimension(2);

            System.Drawing.Bitmap[] ret = new System.Drawing.Bitmap[layers];

            byte[] bufor = new byte[bmjpeg2000.GetBufferLength()];
            if (!bmjpeg2000.GetBuffer(bufor))
            {
                throw new Exception("Błąd pobrania bufora");
            }

            for (uint l = 0; l < layers; l++)
            {
                System.Drawing.Bitmap X = new System.Drawing.Bitmap((int)columns, (int)rows);
                double[,] Y = new double[columns, rows];
                double m = 0;

                for (int r = 0; r < rows; r++)
                {
                    for (int c = 0; c < columns; c++)
                    {
                        int j = ((int)(l * rows * columns) + (int)(r * columns) + (int)c) * 2;
                        Y[r, c] = (double)bufor[j + 1] * 256 + (double)bufor[j];
                        if (Y[r, c] > m)
                        {
                            m = Y[r, c];
                        }
                    }
                }

                for (int r = 0; r < rows; r++)
                {
                    for (int c = 0; c < columns; c++)
                    {
                        int f = (int)(255 * (Y[r, c] / m));
                        X.SetPixel(c, r, System.Drawing.Color.FromArgb(f, f, f));
                    }
                }
                ret[l] = X;
            }
            return(ret);
        }
Exemple #8
0
        private void OnGetImages(object obj)
        {
            ImageList.Clear();
            // typ wyszukiwania (rozpoczynamy od pacjenta)
            gdcm.ERootType typ = gdcm.ERootType.ePatientRootType;

            // do jakiego poziomu wyszukujemy
            gdcm.EQueryLevel poziom = gdcm.EQueryLevel.ePatient; // zobacz inne

            // klucze (filtrowanie lub okreœlenie, które dane s¹ potrzebne)
            gdcm.KeyValuePairArrayType klucze = new gdcm.KeyValuePairArrayType();
            if (SelectedPatient == null)
            {
                State = "Wybierz pacjenta";
                return;
            }

            gdcm.KeyValuePairType klucz1 = new gdcm.KeyValuePairType(new gdcm.Tag(0x0010, 0x0020), SelectedPatient); // NIE WOLNO TU STOSOWAC *; tutaj PatientID="01"
            klucze.Add(klucz1);

            // skonstruuj zapytanie
            gdcm.BaseRootQuery zapytanie = gdcm.CompositeNetworkFunctions.ConstructQuery(typ, poziom, klucze, true);

            // sprawdŸ, czy zapytanie spe³nia kryteria
            if (!zapytanie.ValidateQuery())
            {
                State = "MOVE b³êdne zapytanie!";
                return;
            }

            // przygotuj katalog na wyniki
            String odebrane = System.IO.Path.Combine(".", "odebrane");                          // podkatalog odebrane w bie¿¹cym katalogu

            if (!System.IO.Directory.Exists(odebrane))                                          // jeœli nie istnieje
            {
                System.IO.Directory.CreateDirectory(odebrane);                                  // utwórz go
            }
            String dane = System.IO.Path.Combine(odebrane, System.IO.Path.GetRandomFileName()); // wygeneruj losow¹ nazwê podkatalogu

            System.IO.Directory.CreateDirectory(dane);                                          // i go utwórz

            // wykonaj zapytanie - pobierz do katalogu jak w zmiennej 'dane'
            bool stan = gdcm.CompositeNetworkFunctions.CMove(IP, ushort.Parse(Port), zapytanie, portMove, AET, aec, dane);

            // sprawdŸ stan
            if (!stan)
            {
                State = "MOVE nie dzia³a!";
                return;
            }


            List <string> pliki = new List <string>(System.IO.Directory.EnumerateFiles(dane));

            foreach (String plik in pliki)
            {
                // MOVE + konwersja
                // przeczytaj pixele
                gdcm.PixmapReader reader = new gdcm.PixmapReader();
                reader.SetFileName(plik);
                if (!reader.Read())
                {
                    // najpewniej nie jest to obraz
                    continue;
                }

                // przekonwertuj na "znany format"
                gdcm.Bitmap bmjpeg2000 = pxmap2jpeg2000(reader.GetPixmap());
                // przekonwertuj na .NET bitmapê
                Bitmap[] X = gdcmBitmap2Bitmap(bmjpeg2000);
                // zapisz
                for (int i = 0; i < X.Length; i++)
                {
                    String name = String.Format("{0}_warstwa{1}.jpg", plik, i);
                    X[i].Save(name);
                    ImageList.Add(name);
                }
            }
        }
        public static string Move()
        {
            string resultString = string.Empty;

            // typ wyszukiwania (rozpoczynamy od pacjenta)
            gdcm.ERootType typ = gdcm.ERootType.ePatientRootType;

            // do jakiego poziomu wyszukujemy
            gdcm.EQueryLevel poziom = gdcm.EQueryLevel.ePatient; // zobacz inne

            // klucze (filtrowanie lub określenie, które dane są potrzebne)
            gdcm.KeyValuePairArrayType klucze = new gdcm.KeyValuePairArrayType();
            gdcm.KeyValuePairType      klucz1 = new gdcm.KeyValuePairType(new gdcm.Tag(0x0010, 0x0020), "01"); // NIE WOLNO TU STOSOWAC *; tutaj PatientID="01"
            klucze.Add(klucz1);

            // skonstruuj zapytanie
            gdcm.BaseRootQuery zapytanie = gdcm.CompositeNetworkFunctions.ConstructQuery(typ, poziom, klucze, gdcm.EQueryType.eMove);

            // sprawdź, czy zapytanie spełnia kryteria
            if (!zapytanie.ValidateQuery())
            {
                return("MOVE błędne zapytanie!");
            }

            // przygotuj katalog na wyniki
            string odebrane = System.IO.Path.Combine(".", "odebrane");                          // podkatalog odebrane w bieżącym katalogu

            if (!System.IO.Directory.Exists(odebrane))                                          // jeśli nie istnieje
            {
                System.IO.Directory.CreateDirectory(odebrane);                                  // utwórz go
            }
            string dane = System.IO.Path.Combine(odebrane, System.IO.Path.GetRandomFileName()); // wygeneruj losową nazwę podkatalogu

            System.IO.Directory.CreateDirectory(dane);                                          // i go utwórz

            // wykonaj zapytanie - pobierz do katalogu jak w zmiennej 'dane'
            bool stan = gdcm.CompositeNetworkFunctions.CMove(ipPACS, portPACS, zapytanie, portMove, myAET, callAET, dane);

            // sprawdź stan
            if (!stan)
            {
                return("MOVE nie działa!");
            }

            resultString += "MOVE działa.";

            List <string> pliki = new List <string>(System.IO.Directory.EnumerateFiles(dane));

            foreach (string plik in pliki)
            {
                resultString += "pobrano: {0}" + plik;

                // MOVE + konwersja
                // przeczytaj pixele
                gdcm.PixmapReader reader = new gdcm.PixmapReader();
                reader.SetFileName(plik);
                if (!reader.Read())
                {
                    // najpewniej nie jest to obraz
                    resultString += "pomijam: {0}" + plik;
                    continue;
                }

                // przekonwertuj na "znany format"
                gdcm.Bitmap bmjpeg2000 = BitmapCoder.pxmap2jpeg2000(reader.GetPixmap());
                // przekonwertuj na .NET bitmapę
                Bitmap[] X = BitmapCoder.gdcmBitmap2Bitmap(bmjpeg2000);
                // zapisz
                for (int i = 0; i < X.Length; i++)
                {
                    string name = String.Format("{0}_warstwa{1}.jpg", plik, i);
                    X[i].Save(name);
                    resultString += "konwersja do: {0}" + name;
                }
            }
            return(resultString);
        }