示例#1
0
        public static Image <Gray, UInt16> ReadFITSFile(String ImagePath)
        {
            try
            {
                Fits     f = new Fits(ImagePath);
                ImageHDU h = (ImageHDU)f.ReadHDU();

                System.Array[] img = (System.Array[])h.Kernel;
                f.Close();
                int Width  = img.Count();
                int Height = img.GetLength(0);

                UInt16[][]           ImgConverted   = new UInt16[Width][];
                Image <Gray, UInt16> LastestImageCV = new Image <Gray, UInt16>(Width, Height);
                Int16 MaxNumber = Int16.MaxValue;

                for (int i = 0; i < Width; i++)
                {
                    ImgConverted[i] = new UInt16[Height];
                    for (int j = 0; j < Height; j++)
                    {
                        int Data = MaxNumber + (Int16)img[i].GetValue(j) + 1;
                        ImgConverted[i][j]           = (UInt16)Data;
                        LastestImageCV.Data[i, j, 0] = (UInt16)Data;
                    }
                }

                return(LastestImageCV);
            }
            catch
            {
                return(null);
            }
        }
        public static double[] ReadFrequencies(string file)
        {
            Fits     f = new Fits(file);
            ImageHDU h = (ImageHDU)f.ReadHDU();

            return((double[])h.Kernel);
        }
示例#3
0
        public void LoadImage(string path)
        {
            Fits     fits     = new Fits(path);
            ImageHDU hdu      = GetImageHDU(fits);
            int      axis_num = 0;

            if (hdu.Axes.Length == 2)
            {
                axis_num          = 2;
                this.rowsCount    = hdu.Axes[0];
                this.columnsCount = hdu.Axes[1];
            }
            if (hdu.Axes.Length == 3)
            {
                axis_num          = 3;
                this.rowsCount    = hdu.Axes[1];
                this.columnsCount = hdu.Axes[2];
            }
            this.bscale = hdu.BScale;
            this.bzero  = hdu.BZero;
            this.vals   = new double[rowsCount][];
            for (int i = 0; i < rowsCount; i++)
            {
                this.vals[i] = new double[columnsCount];
            }
            GetImageData(hdu, axis_num);
            fits.Stream.Close();
        }
示例#4
0
        public void InitImage(ImageHDU hdu, string imagePath)
        {
            Array[] data   = (Array[])hdu.Kernel;
            int     width  = data.Length;
            int     height = data[0].Length;
            //List<short[]> newData = new List<short[]>();
            Bitmap b16test;

            b16test = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);

            int threshold = 254;
            List <Coordinants> brightObjects = new List <Coordinants>();

            //var test = data[0];
            for (int i = 0; i < data.Length; i++)
            {
                if (width == 0)
                {
                    width = data.Length;
                }
                short[] tempArray = (short[])data[i];

                for (int j = 0; j < tempArray.Length; j++)
                {
                    if (height == 0)
                    {
                        height = tempArray.Length;
                    }
                    //tempArray[j] = (short)((hdu.BZero + hdu.BScale * tempArray[j]) / 255);

                    tempArray[j] = (short)((hdu.BZero + hdu.BScale * tempArray[j]) / 256);
                    //short trueValue = (short)(hdu.BZero + hdu.BScale * tempArray[j]);

                    //tempArray[j] = (short)((Math.Log(trueValue) * 28.8) - 65); - This is to bring out the image
                    Color newColor;
                    if (tempArray[j] > threshold)
                    {
                        Coordinants saveCorods = new Coordinants {
                            x = i, y = j, value = tempArray[j]
                        };
                        brightObjects.Add(saveCorods);
                        newColor = Color.FromArgb(255, 0, 0);
                    }
                    else
                    {
                        newColor = Color.FromArgb(tempArray[j], tempArray[j], tempArray[j]);
                    }
                    b16test.SetPixel(i, j, newColor);
                }

                //newData.Add(tempArray);
                data[i] = tempArray;
            }

            Bitmap imageWithLine = DrawConnectingLines(b16test, brightObjects);

            SaveImage(imageWithLine);
            //SaveImage(b16test);
        }
示例#5
0
        private static double[,] GetImage(String fileName)
        {
            Fits     fits = new Fits(fileName);
            ImageHDU hdu  = GetImageHDU(fits);

            double[,] pixelValues = GetImageData(hdu);
            return(pixelValues);
        }
示例#6
0
        private static void readImage(string fileName, out Int16[][] img, out Header hdr)
        {
            Fits f = new Fits(fileName);

            ImageHDU h = (ImageHDU)f.ReadHDU();

            hdr = h.Header;

            Int32 height = h.Axes[0];
            Int32 width  = h.Axes[1];

            Int16 [][] inImg = new Int16[height][];
            int        x, y;

            img = new Int16[height][];

            object[] rows = (System.Array[])h.Kernel;

            for (y = 0; y < height / 2; y++)
            {
                inImg[y] = new Int16[2 * width];
                for (x = 0; x < width; x++)
                {
                    inImg[y][x]         = (Int16)((Int16[])rows[2 * y])[x];
                    inImg[y][x + width] = (Int16)((Int16[])rows[2 * y + 1])[x];
                }
            }

            for (y = 0; y < height; y++)
            {
                img[y] = new Int16[width];
                for (x = 0; x < width; x++)
                {
                    img[y][x] = (Int16)((Int16[])rows[y])[x];
                }
            }

#if false
            for (y = 0; y < height; y += 2)
            {
                int inX = 0;
                int inY = y / 2;

                img[y]     = new Int16[width];
                img[y + 1] = new Int16[width];

                for (x = 0; x < width; x += 2)
                {
                    img[y][x]         = inImg[inY][inX++];
                    img[y + 1][x]     = inImg[inY][inX++];
                    img[y + 1][x + 1] = inImg[inY][inX++];
                    img[y][x + 1]     = inImg[inY][inX++];
                }
            }
#endif
        }
示例#7
0
        private static void xreadImage(string fileName, out Int16[][] img, out Header hdr)
        {
            Fits f = new Fits(fileName);

            ImageHDU h = (ImageHDU)f.ReadHDU();

            hdr = h.Header;

            Int32 height = h.Axes[0];
            Int32 width  = h.Axes[1];

            object[] rows  = (System.Array[])h.Kernel;
            Int16[]  inImg = new Int16[height * width];

            int x, y;
            int idx = 0;

            for (y = 0; y < height; y++)
            {
                Int16[] row = (Int16[])rows[y];

                for (x = 0; x < width; x++)
                {
                    inImg[idx++] = row[x];
                }
            }

            Int16 [] outImg = new Int16[height * width];

            int srcIdx, destIdx;

            for (srcIdx = 0, destIdx = 0, y = 0; y < height / 2; y++)
            {
                for (x = 0; x < width / 2; x++)
                {
                    outImg[destIdx]           = inImg[srcIdx++];
                    outImg[width + destIdx++] = inImg[srcIdx++];
                    outImg[width + destIdx]   = inImg[srcIdx++];
                    outImg[destIdx++]         = inImg[srcIdx++];
                }
                destIdx += width;
            }

            img = new Int16[height][];
            for (srcIdx = 0, y = 0; y < height; y++)
            {
                img[y] = new Int16[width];

                for (x = 0; x < width; x++)
                {
                    img[y][x] = outImg[srcIdx++];
                }
            }
        }
示例#8
0
        private static void SaveFITSAsImage(String inputFilename, String outputFilename)
        {
            Fits     fits   = new Fits(inputFilename);
            ImageHDU hdu    = GetImageHDU(fits);
            int      bitpix = hdu.BitPix;
            double   bZero  = hdu.BZero;
            double   bScale = hdu.BScale;
            double   min    = hdu.MinimumValue;
            double   max    = hdu.MaximumValue;

            double[,] a = GetImageData(hdu);

            for (int x = 0; x < a.GetLength(0); ++x)
            {
                for (int y = 0; y < a.GetLength(1); ++y)
                {
                    min = Math.Min(min, a[x, y]);
                    max = Math.Max(max, a[x, y]);
                }
            }

            Console.Out.WriteLine("Bitpix = " + bitpix + " bzero = " + bZero + " bscale = " + bScale +
                                  " min = " + min + " max = " + max);
            Bitmap bmp = new Bitmap(a.GetLength(0), a.GetLength(1));

            double nBins             = Math.Pow(2.0, 16.0) - 1.0;
            double linearScaleFactor = nBins / (max - min);
            double logScaleFactor    = nBins / Math.Log10(nBins);
            double byteScaleFactor   = 255.0 / nBins;
            double val = Double.NaN;

            for (int x = 0; x < a.GetLength(0); ++x)
            {
                for (int y = 0; y < a.GetLength(1); ++y)
                {
                    val  = a[x, y] - min;
                    val  = Math.Max(0.0, Math.Min(val, max - min));
                    val  = val <= 0.0 ? 0.0 : (Math.Log10(val * linearScaleFactor) * logScaleFactor);
                    val *= byteScaleFactor;
                    bmp.SetPixel(x, y, Color.FromArgb((int)val, (int)val, (int)val));
                }
            }

            bmp.Save(outputFilename, System.Drawing.Imaging.ImageFormat.Png);
        }
        public static double[,] ReadImageDouble(string file)
        {
            Fits     f      = new Fits(file);
            ImageHDU h      = (ImageHDU)f.ReadHDU();
            var      imgRaw = (Array[])h.Kernel;

            var output = new double[imgRaw.Length, imgRaw[0].Length];

            for (int i = 0; i < imgRaw.Length; i++)
            {
                var row = (double[])imgRaw[i];
                for (int j = 0; j < row.Length; j++)
                {
                    output[i, j] = row[j];
                }
            }
            return(output);
        }
        private BasicHDU ReadFIT(string fileName)
        {
            Fits f = new Fits(fileName);

            try
            {
                BasicHDU hdus = f.ReadHDU();
                if (hdus != null)
                {
                    hdus.Info();
                    // TEST: NUM SHARP TEST
                    ImageHDU imgHdu = (ImageHDU)f.GetHDU(0);
                    numSharp.InitImage(imgHdu, fileName);
                }
                return(hdus);
            }
            catch (Exception e)
            {
                throw e;
            }
        }
示例#11
0
        // Read a FITS file
        public Bitmap ReadFile(string FileName)
        {
            Fits     f2          = new Fits(FileName);
            ImageHDU h2          = (ImageHDU)f2.ReadHDU();
            Header   hdr2        = h2.Header;
            int      BitPixLen   = hdr2.GetIntValue("BITPIX");
            int      PixelWidth  = hdr2.GetIntValue("NAXIS1");
            int      PixelHeight = hdr2.GetIntValue("NAXIS2");

            object[] img  = (object[])h2.Kernel;
            uint[][] img1 = new uint[PixelHeight][];
            byte[][] img2 = new byte[PixelHeight][];
            //for (int i = 0; i < PixelHeight; i++)
            //    img2[i] = new byte[PixelWidth];

            if (BitPixLen == 32)//int
            {
                //读出来又翻转,注意

                uint MaxPixelValue = 0x00000000;
                uint MinPixelValue = 0xFFFFFFFF;
                for (int i = 0; i < PixelHeight; i++)
                {
                    img1[i] = (uint[])img[i];            //img强制赋值给img1
                    img2[i] = new byte[PixelWidth];
                    for (int j = 0; j < PixelWidth; j++) //求最值
                    {
                        if (img1[i][j] > MaxPixelValue)
                        {
                            MaxPixelValue = img1[i][j];
                        }
                        else if ((img1[i][j] > 0) && (img1[i][j] < MinPixelValue))
                        {
                            MinPixelValue = img1[i][j];
                        }
                    }
                }
                //转换灰度图系数
                double m = (MaxPixelValue - MinPixelValue) * 1.0 / 255;

                for (int i = 0; i < PixelHeight; i++)
                {
                    for (int j = 0; j < PixelWidth; j++)
                    {
                        if (img1[i][j] > 0)
                        {
                            double tf = (img1[i][j] - MinPixelValue) * 1.0 / m;
                            if (tf >= 255.0)
                            {
                                img2[i][j] = 255;
                            }
                            else
                            {
                                img2[i][j] = Convert.ToByte(tf);
                            }
                        }
                    }
                }
            }

            //Bitmap bmp = ToGrayBitmap(img2, PixelWidth, PixelHeight);
            //pictureBox1.Image = bmp;
            return(ToGrayBitmap(img2, PixelWidth, PixelHeight));
        }
示例#12
0
 public static PixelFormat getPixelFormat(this ImageHDU imageHdu)
 {
     return(BitpixMediaPixelFormatDictionary[imageHdu.BitPix]);
 }
示例#13
0
文件: FITS.cs 项目: daleghent/NINA
        public static Task <IImageData> Load(Uri filePath, bool isBayered)
        {
            return(Task.Run <IImageData>(() => {
                Fits f = new Fits(filePath);
                ImageHDU hdu = (ImageHDU)f.ReadHDU();
                Array[] arr = (Array[])hdu.Data.DataArray;

                var dimensions = hdu.Header.GetIntValue("NAXIS");
                if (dimensions > 2)
                {
                    //Debayered Images are not supported. Take the first dimension instead to get at least a monochrome image
                    arr = (Array[])arr[0];
                }

                var width = hdu.Header.GetIntValue("NAXIS1");
                var height = hdu.Header.GetIntValue("NAXIS2");
                var bitPix = hdu.Header.GetIntValue("BITPIX");

                var converter = GetConverter(bitPix);
                ushort[] pixels = converter.Convert(arr, width, height);

                //Translate nom.tam.fits into N.I.N.A. FITSHeader
                FITSHeader header = new FITSHeader(width, height);
                var iterator = hdu.Header.GetCursor();
                while (iterator.MoveNext())
                {
                    HeaderCard card = (HeaderCard)((DictionaryEntry)iterator.Current).Value;
                    if (card.Value != null)
                    {
                        if (card.IsStringValue)
                        {
                            header.Add(card.Key, card.Value, card.Comment);
                        }
                        else
                        {
                            if (card.Value == "T")
                            {
                                header.Add(card.Key, true, card.Comment);
                            }
                            else if (card.Value.Contains("."))
                            {
                                if (double.TryParse(card.Value, NumberStyles.Number, CultureInfo.InvariantCulture, out var value))
                                {
                                    header.Add(card.Key, value, card.Comment);
                                }
                            }
                            else
                            {
                                if (int.TryParse(card.Value, NumberStyles.Number, CultureInfo.InvariantCulture, out var value))
                                {
                                    header.Add(card.Key, value, card.Comment);
                                }
                            }
                        }
                    }
                }

                var metaData = new ImageMetaData();
                try {
                    metaData = header.ExtractMetaData();
                } catch (Exception ex) {
                    Logger.Error(ex.Message);
                }
                return new Model.ImageData.ImageData(pixels, width, height, 16, isBayered, metaData);
            }));
        }
示例#14
0
        private FitsHeader  ReadFits(string Filename)
        {
/*            double RA;
 *          double DEC;
 *          float[][] imageData;
 *          string LocalDate;
 *          double SiteLong;
 *          double SiteLat; */
            FitsHeader fitsheader = new FitsHeader();


            try
            {
                Fits f = new Fits(Filename);

                ImageHDU h = (ImageHDU)f.ReadHDU();


                //other things we might want:
                // IMAGETYP= 'LIGHT'
                // NAXIS1  =                 7380 /
                // NAXIS2 = 4908
                // EXPTIME =                  6.0 / [s] Exposure duration
                // DATE-LOC= '2020-10-07T01:28:55.164' / Time of observation (local)
                //     GAIN    =                 1208 / Sensor gain
                //     XPIXSZ  =                 4.88 / [um] Pixel X axis size
                // YPIXSZ = 4.88 / [um] Pixel Y axis size
                //     SITELAT =     47.6077777777778 / [deg] Observation site latitude
                // SITELONG = -122.335 / [deg] Observation site longitude]

                fitsheader.ImageType = h.Header.GetStringValue("IMAGETYP");
                fitsheader.NAXIS1    = h.Header.GetIntValue("NAXIS1");
                fitsheader.NAXIS2    = h.Header.GetIntValue("NAXIS2");
                fitsheader.DecDeg    = h.Header.GetDoubleValue("DEC");
                fitsheader.RaDeg     = h.Header.GetDoubleValue("RA") / 15;
                fitsheader.Exposure  = h.Header.GetFloatValue("EXPOSURE");


                fitsheader.LocalDate = DateTime.ParseExact(h.Header.GetStringValue("DATE-LOC"), "yyyy-MM-ddTHH:mm:ss.fff", CultureInfo.InvariantCulture);
                fitsheader.UTCDate   = DateTime.ParseExact(h.Header.GetStringValue("DATE-OBS"), "yyyy-MM-ddTHH:mm:ss.fff", CultureInfo.InvariantCulture);

                fitsheader.SiteLat     = h.Header.GetDoubleValue("SITELAT");
                fitsheader.SiteLong    = h.Header.GetDoubleValue("SITELONG");
                fitsheader.PixelPitch  = h.Header.GetFloatValue("XPIXSZ");
                fitsheader.Gain        = h.Header.GetIntValue("GAIN");
                fitsheader.SensorTempC = h.Header.GetFloatValue("CCD-TEMP");
                fitsheader.FocalLength = h.Header.GetFloatValue("FOCALLEN");
                fitsheader.Object      = h.Header.GetStringValue("OBJECT");


                /*RA = h.Header.GetDoubleValue("RA") / 15;
                 * DEC = h.Header.GetDoubleValue("DEC");
                 * LocalDate = h.Header.GetStringValue("DATE-LOC");
                 * SiteLat = h.Header.GetDoubleValue("SITELAT");
                 * SiteLong = h.Header.GetDoubleValue("SITELAT");
                 *
                 *
                 * //imageData = (float[][])h.Kernel; */

                f.Close();
            }
            catch { Console.WriteLine("Error opening fits.."); return(fitsheader); }

            return(fitsheader);
        }
示例#15
0
        public void GetImageData(ImageHDU hdu, int axis_num)
        {
            //double[,] result = new double[hdu.Axes[1], hdu.Axes[0]];
            Array[] a = null;

            if (axis_num == 2)
            {
                a = (Array[])hdu.Data.DataArray;
            }
            if (axis_num == 3)
            {
                Array[] bb = (Array[])hdu.Data.DataArray;
                a = (Array[])bb[0];
            }

            switch (hdu.BitPix)
            {
            case 8:
            {
                byte[] b = new byte[columnsCount];
                for (int y = 0; y < rowsCount; ++y)
                {
                    a[y].CopyTo(b, 0);
                    for (int x = 0; x < columnsCount; ++x)
                    {
                        this.vals[y][x] = bzero + bscale * (double)b[x];
                    }
                }
            }
            break;

            case 16:
            {
                short[] b = new short[columnsCount];

                for (int y = 0; y < rowsCount; ++y)
                {
                    a[y].CopyTo(b, 0);

                    for (int x = 0; x < columnsCount; ++x)
                    {
                        this.vals[y][x] = bzero + bscale * (double)b[x];
                    }
                }
            }
            break;

            case 32:
            {
                int[] b = new int[columnsCount];
                for (int y = 0; y < rowsCount; ++y)
                {
                    a[y].CopyTo(b, 0);
                    for (int x = 0; x < columnsCount; ++x)
                    {
                        this.vals[y][x] = bzero + bscale * (double)b[x];
                    }
                }
            }
            break;

            case -32:
            {
                float[] b = new float[columnsCount];
                for (int y = 0; y < rowsCount; ++y)
                {
                    a[y].CopyTo(b, 0);
                    for (int x = 0; x < columnsCount; ++x)
                    {
                        this.vals[y][x] = bzero + bscale * (double)b[x];
                    }
                }
            }
            break;

            case -64:
            {
                double[] b = new double[columnsCount];
                for (int y = 0; y < rowsCount; ++y)
                {
                    a[y].CopyTo(b, 0);
                    for (int x = 0; x < columnsCount; ++x)
                    {
                        this.vals[y][x] = bzero + bscale * (double)b[x];
                    }
                }
            }
            break;

            default:
                throw new Exception("Data type is not supported.");
            }
        }
示例#16
0
        private static double[,] GetImageData(ImageHDU hdu)
        {
            double[,] result = new double[hdu.Axes[1], hdu.Axes[0]];
            double bZero  = hdu.BZero;
            double bScale = hdu.BScale;
            double min    = hdu.MinimumValue;
            double max    = hdu.MaximumValue;

            Console.Out.WriteLine("Starting image read at " + System.DateTime.Now);
            Array[] a = (Array[])hdu.Data.DataArray;
            Console.Out.WriteLine("Done image read at " + System.DateTime.Now);

            switch (hdu.BitPix)
            {
            case 8:
            {
                byte[] b = null;
                for (int y = 0; y < hdu.Axes[0]; ++y)
                {
                    b = (byte[])a[y];
                    for (int x = 0; x < hdu.Axes[1]; ++x)
                    {
                        result[x, y] = bZero + bScale * (double)b[x];
                    }
                }
            }
            break;

            case 16:
            {
                short[] b = null;
                for (int y = 0; y < hdu.Axes[0]; ++y)
                {
                    b = (short[])a[y];
                    for (int x = 0; x < hdu.Axes[1]; ++x)
                    {
                        result[x, y] = bZero + bScale * (double)b[x];
                    }
                }
            }
            break;

            case 32:
            {
                int[] b = null;
                for (int y = 0; y < hdu.Axes[0]; ++y)
                {
                    b = (int[])a[y];
                    for (int x = 0; x < hdu.Axes[1]; ++x)
                    {
                        result[x, y] = bZero + bScale * (double)b[x];
                    }
                }
            }
            break;

            case -32:
            {
                float[] b = null;
                for (int y = 0; y < hdu.Axes[0]; ++y)
                {
                    b = (float[])a[y];
                    for (int x = 0; x < hdu.Axes[1]; ++x)
                    {
                        result[x, y] = bZero + bScale * (double)b[x];
                    }
                }
            }
            break;

            case -64:
            {
                double[] b = null;
                for (int y = 0; y < hdu.Axes[0]; ++y)
                {
                    b = (double[])a[y];
                    for (int x = 0; x < hdu.Axes[1]; ++x)
                    {
                        result[x, y] = bZero + bScale * (double)b[x];
                    }
                }
            }
            break;

            default:
                throw new Exception("Data type not supported.");
            }

            return(result);
        }