Beispiel #1
0
        private bool GetFitInMatrix(ITrackedObjectPsfFit gaussian, ref int matirxSize, float preselectedAperture)
        {
            rbGuidingStar.Text = "Guiding/Comparison Star";
            m_IsBrightEnoughForAutoGuidingStar = false;

            if (m_Aperture == null)
            {
                if (gaussian != null && !double.IsNaN(gaussian.FWHM) && TangraConfig.Settings.Photometry.SignalApertureUnitDefault == TangraConfig.SignalApertureUnit.FWHM)
                {
                    m_Aperture = (float)(gaussian.FWHM * TangraConfig.Settings.Photometry.DefaultSignalAperture);
                }
                else
                {
                    m_Aperture = (float)(TangraConfig.Settings.Photometry.DefaultSignalAperture);
                }
            }
            else if (
                gaussian != null && !double.IsNaN(gaussian.FWHM) && TangraConfig.Settings.Photometry.SignalApertureUnitDefault == TangraConfig.SignalApertureUnit.FWHM &&
                m_Aperture < (float)(gaussian.FWHM * TangraConfig.Settings.Photometry.DefaultSignalAperture))
            {
                m_Aperture = (float)(gaussian.FWHM * TangraConfig.Settings.Photometry.DefaultSignalAperture);
            }

            nudFitMatrixSize.ValueChanged -= nudFitMatrixSize_ValueChanged;
            try
            {
                uint[,] autoStarsPixels = m_AstroImage.GetMeasurableAreaPixels(m_Center.X, m_Center.Y, 35);
                m_AutoStarsInLargerArea = StarFinder.GetStarsInArea(
                    ref autoStarsPixels,
                    m_AstroImage.Pixelmap.BitPixCamera,
                    m_AstroImage.Pixelmap.MaxSignalValue,
                    m_AstroImage.MedianNoise,
                    LightCurveReductionContext.Instance.DigitalFilter);

                m_ProcessingPixels = ImageFilters.CutArrayEdges(autoStarsPixels, 9);
                m_DisplayPixels    = m_AstroImage.GetMeasurableAreaDisplayBitmapPixels(m_Center.X, m_Center.Y, 17);

                m_AutoStarsInArea = new List <PSFFit>();
                foreach (PSFFit autoStar in m_AutoStarsInLargerArea)
                {
                    if (autoStar.XCenter > 9 && autoStar.XCenter < 9 + 17 &&
                        autoStar.YCenter > 9 && autoStar.YCenter < 9 + 17)
                    {
                        // Don't change original star so use a clone
                        PSFFit clone = autoStar.Clone();
                        clone.SetNewFieldCenterFrom35PixMatrix(8, 8);
                        m_AutoStarsInArea.Add(clone);
                    }
                }

                int oldMatirxSize = matirxSize;

                if (m_AutoStarsInArea.Count == 0)
                {
                    rbGuidingStar.Text = "Guiding/Comparison Star";

                    // There are no stars that are bright enough. Simply let the user do what they want, but still try to default to a sensible aperture size
                    MeasurementsHelper measurement = ReduceLightCurveOperation.DoConfiguredMeasurement(m_ProcessingPixels, m_Aperture.Value, m_AstroImage.Pixelmap.BitPixCamera, m_AstroImage.Pixelmap.MaxSignalValue, 3.0, ref matirxSize);

                    if (measurement.FoundBestPSFFit != null &&
                        measurement.FoundBestPSFFit.IsSolved &&
                        measurement.FoundBestPSFFit.Certainty > 0.1)
                    {
                        m_X0   = measurement.XCenter;
                        m_Y0   = measurement.YCenter;
                        m_FWHM = (float)measurement.FoundBestPSFFit.FWHM;
                    }
                    else
                    {
                        m_X0   = 8;
                        m_Y0   = 8;
                        m_FWHM = 6;
                    }

                    m_Gaussian = null;
                    nudFitMatrixSize.SetNUDValue(11);
                }
                else if (m_AutoStarsInArea.Count == 1)
                {
                    // There is exactly one good star found. Go and do a fit in a wider area
                    double bestFindTolerance = 3.0;

                    for (int i = 0; i < 2; i++)
                    {
                        MeasurementsHelper measurement = ReduceLightCurveOperation.DoConfiguredMeasurement(m_ProcessingPixels, m_Aperture.Value, m_AstroImage.Pixelmap.BitPixCamera, m_AstroImage.Pixelmap.MaxSignalValue, bestFindTolerance, ref matirxSize);
                        if (measurement != null && matirxSize != -1)
                        {
                            if (matirxSize < 5)
                            {
                                // Do a centroid in the full area, and get another matix centered at the centroid
                                ImagePixel centroid = new ImagePixel(m_Center.X, m_Center.Y);

                                m_ProcessingPixels = m_AstroImage.GetMeasurableAreaPixels(centroid);
                                m_DisplayPixels    = m_AstroImage.GetMeasurableAreaDisplayBitmapPixels(centroid);

                                m_X0       = centroid.X;
                                m_Y0       = centroid.Y;
                                m_FWHM     = 6;
                                m_Gaussian = null;

                                nudFitMatrixSize.SetNUDValue(11);
                            }
                            else
                            {
                                m_X0 = measurement.XCenter;
                                m_Y0 = measurement.YCenter;
                                if (measurement.FoundBestPSFFit != null)
                                {
                                    m_FWHM     = (float)measurement.FoundBestPSFFit.FWHM;
                                    m_Gaussian = measurement.FoundBestPSFFit;
                                }
                                else
                                {
                                    m_FWHM     = 6;
                                    m_Gaussian = null;
                                }
                                m_ProcessingPixels = measurement.PixelData;
                                nudFitMatrixSize.SetNUDValue(matirxSize);
                            }
                        }
                        else
                        {
                            matirxSize = oldMatirxSize;
                            return(false);
                        }

                        if (m_Gaussian != null)
                        {
                            if (IsBrightEnoughtForGuidingStar())
                            {
                                rbGuidingStar.Text = "Guiding/Comparison Star";
                                m_IsBrightEnoughForAutoGuidingStar = true;
                            }

                            break;
                        }
                    }
                }
                else if (m_AutoStarsInArea.Count > 1)
                {
                    rbGuidingStar.Text = "Guiding/Comparison Star";

                    // There are more stars found.
                    double xBest = m_Gaussian != null
                                      ? m_Gaussian.XCenter
                                      : m_IsEdit ? ObjectToAdd.ApertureMatrixX0
                                                 : 8.5;

                    double yBest = m_Gaussian != null
                                      ? m_Gaussian.YCenter
                                      : m_IsEdit ? ObjectToAdd.ApertureMatrixY0
                                                 : 8.5;

                    // by default use the one closest to the original location
                    PSFFit closestFit  = m_AutoStarsInArea[0];
                    double closestDist = double.MaxValue;
                    foreach (PSFFit star in m_AutoStarsInArea)
                    {
                        double dist =
                            Math.Sqrt((star.XCenter - xBest) * (star.XCenter - xBest) +
                                      (star.YCenter - yBest) * (star.YCenter - yBest));
                        if (closestDist > dist)
                        {
                            closestDist = dist;
                            closestFit  = star;
                        }
                    }


                    m_X0       = (float)closestFit.XCenter;
                    m_Y0       = (float)closestFit.YCenter;
                    m_FWHM     = (float)closestFit.FWHM;
                    m_Gaussian = closestFit;

                    nudFitMatrixSize.SetNUDValue(m_IsEdit ? ObjectToAdd.PsfFitMatrixSize : closestFit.MatrixSize);
                }

                //if (m_Gaussian == null && gaussian.Certainty > 0.1 && ImagePixel.ComputeDistance(gaussian.X0_Matrix, 8, gaussian.Y0_Matrix, 8) < 3)
                //{
                //	// Id we failed to locate a bright enough autostar, but the default Gaussian is still certain enough and close enought to the center, we present it as a starting point
                //	m_X0 = (float)gaussian.X0_Matrix;
                //	m_Y0 = (float)gaussian.Y0_Matrix;
                //	m_FWHM = (float)gaussian.FWHM;
                //	m_Gaussian = gaussian;
                //}

                decimal appVal;
                if (float.IsNaN(preselectedAperture))
                {
                    if (float.IsNaN(m_Aperture.Value))
                    {
                        appVal = Convert.ToDecimal(TangraConfig.Settings.Photometry.DefaultSignalAperture);
                    }
                    else
                    {
                        appVal = Convert.ToDecimal(m_Aperture.Value);
                        if (nudAperture1.Maximum < appVal)
                        {
                            nudAperture1.Maximum = appVal + 1;
                        }
                    }
                }
                else
                {
                    appVal = (decimal)preselectedAperture;
                }

                if ((float)appVal > m_Aperture)
                {
                    m_Aperture = (float)appVal;
                }

                nudAperture1.SetNUDValue(Math.Round(appVal, 2));

                PlotSingleTargetPixels();

                PlotGaussian();

                return(true);
            }
            finally
            {
                nudFitMatrixSize.ValueChanged += nudFitMatrixSize_ValueChanged;
            }
        }
Beispiel #2
0
 private bool GetFitInMatrix(ITrackedObjectPsfFit gaussian, ref int matirxSize)
 {
     return(GetFitInMatrix(gaussian, ref matirxSize, float.NaN));
 }
        private bool GetFitInMatrix(ITrackedObjectPsfFit gaussian, ref int matirxSize, float preselectedAperture)
        {
            rbGuidingStar.Text = "Guiding Star";
            m_IsBrightEnoughForAutoGuidingStar = false;

            if (m_Aperture == null)
            {
                if (gaussian != null && !double.IsNaN(gaussian.FWHM) && TangraConfig.Settings.Photometry.SignalApertureUnitDefault == TangraConfig.SignalApertureUnit.FWHM)
                    m_Aperture = (float)(gaussian.FWHM * TangraConfig.Settings.Photometry.DefaultSignalAperture);
                else
                    m_Aperture = (float)(TangraConfig.Settings.Photometry.DefaultSignalAperture);
            }
            else if (
                gaussian != null && !double.IsNaN(gaussian.FWHM) && TangraConfig.Settings.Photometry.SignalApertureUnitDefault == TangraConfig.SignalApertureUnit.FWHM &&
                m_Aperture < (float)(gaussian.FWHM * TangraConfig.Settings.Photometry.DefaultSignalAperture))
            {
                m_Aperture = (float) (gaussian.FWHM*TangraConfig.Settings.Photometry.DefaultSignalAperture);
            }

            nudFitMatrixSize.ValueChanged -= nudFitMatrixSize_ValueChanged;
            try
            {
                uint[,] autoStarsPixels = m_AstroImage.GetMeasurableAreaPixels(m_Center.X, m_Center.Y, 35);
                m_AutoStarsInLargerArea = StarFinder.GetStarsInArea(
                    ref autoStarsPixels,
                    m_AstroImage.Pixelmap.BitPixCamera,
                    m_AstroImage.Pixelmap.MaxSignalValue,
                    m_AstroImage.MedianNoise,
                    LightCurveReductionContext.Instance.DigitalFilter);

                m_ProcessingPixels = ImageFilters.CutArrayEdges(autoStarsPixels, 9);
                m_DisplayPixels = m_AstroImage.GetMeasurableAreaDisplayBitmapPixels(m_Center.X, m_Center.Y, 17);

                m_AutoStarsInArea = new List<PSFFit>();
                foreach (PSFFit autoStar in m_AutoStarsInLargerArea)
                {
                    if (autoStar.XCenter > 9 && autoStar.XCenter < 9 + 17 &&
                        autoStar.YCenter > 9 && autoStar.YCenter < 9 + 17)
                    {
                        // Don't change original star so use a clone
                        PSFFit clone = autoStar.Clone();
                        clone.SetNewFieldCenterFrom35PixMatrix(8, 8);
                        m_AutoStarsInArea.Add(clone);
                    }
                }

                int oldMatirxSize = matirxSize;

                if (m_AutoStarsInArea.Count == 0)
                {
                    rbGuidingStar.Text = "Guiding Star";

                    // There are no stars that are bright enough. Simply let the user do what they want, but still try to default to a sensible aperture size
                    MeasurementsHelper measurement = ReduceLightCurveOperation.DoConfiguredMeasurement(m_ProcessingPixels, m_Aperture.Value, m_AstroImage.Pixelmap.BitPixCamera, m_AstroImage.Pixelmap.MaxSignalValue, 3.0, ref matirxSize);

                    if (measurement.FoundBestPSFFit != null &&
                        measurement.FoundBestPSFFit.IsSolved &&
                        measurement.FoundBestPSFFit.Certainty > 0.1)
                    {
                        m_X0 = measurement.XCenter;
                        m_Y0 = measurement.YCenter;
                        m_FWHM = (float) measurement.FoundBestPSFFit.FWHM;
                    }
                    else
                    {
                        m_X0 = 8;
                        m_Y0 = 8;
                        m_FWHM = 6;
                    }

                    m_Gaussian = null;
                    nudFitMatrixSize.SetNUDValue(11);
                }
                else if (m_AutoStarsInArea.Count == 1)
                {
                    // There is exactly one good star found. Go and do a fit in a wider area
                    double bestFindTolerance = 3.0;

                    for (int i = 0; i < 2; i++)
                    {
                        MeasurementsHelper measurement = ReduceLightCurveOperation.DoConfiguredMeasurement(m_ProcessingPixels, m_Aperture.Value, m_AstroImage.Pixelmap.BitPixCamera, m_AstroImage.Pixelmap.MaxSignalValue, bestFindTolerance, ref matirxSize);
                        if (measurement != null && matirxSize != -1)
                        {
                            if (matirxSize < 5)
                            {
                                // Do a centroid in the full area, and get another matix centered at the centroid
                                ImagePixel centroid = new ImagePixel(m_Center.X, m_Center.Y);

                                m_ProcessingPixels = m_AstroImage.GetMeasurableAreaPixels(centroid);
                                m_DisplayPixels = m_AstroImage.GetMeasurableAreaDisplayBitmapPixels(centroid);

                                m_X0 = centroid.X;
                                m_Y0 = centroid.Y;
                                m_FWHM = 6;
                                m_Gaussian = null;

                                nudFitMatrixSize.SetNUDValue(11);
                            }
                            else
                            {
                                m_X0 = measurement.XCenter;
                                m_Y0 = measurement.YCenter;
                                if (measurement.FoundBestPSFFit != null)
                                {
                                    m_FWHM = (float)measurement.FoundBestPSFFit.FWHM;
                                    m_Gaussian = measurement.FoundBestPSFFit;
                                }
                                else
                                {
                                    m_FWHM = 6;
                                    m_Gaussian = null;
                                }
                                m_ProcessingPixels = measurement.PixelData;
                                nudFitMatrixSize.SetNUDValue(matirxSize);
                            }
                        }
                        else
                        {
                            matirxSize = oldMatirxSize;
                            return false;
                        }

                        if (m_Gaussian != null)
                        {
                            if (IsBrightEnoughtForGuidingStar())
                            {
                                rbGuidingStar.Text = "Guiding/Comparison Star";
                                m_IsBrightEnoughForAutoGuidingStar = true;
                            }

                            break;
                        }
                    }
                }
                else if (m_AutoStarsInArea.Count > 1)
                {
                    rbGuidingStar.Text = "Guiding Star";

                    // There are more stars found.
                    double xBest = m_Gaussian != null
                                      ? m_Gaussian.XCenter
                                      : m_IsEdit ? ObjectToAdd.ApertureMatrixX0
                                                 : 8.5;

                    double yBest = m_Gaussian != null
                                      ? m_Gaussian.YCenter
                                      : m_IsEdit ? ObjectToAdd.ApertureMatrixY0
                                                 : 8.5;

                    // by default use the one closest to the original location
                    PSFFit closestFit = m_AutoStarsInArea[0];
                    double closestDist = double.MaxValue;
                    foreach (PSFFit star in m_AutoStarsInArea)
                    {
                        double dist =
                            Math.Sqrt((star.XCenter - xBest) * (star.XCenter - xBest) +
                                      (star.YCenter - yBest) * (star.YCenter - yBest));
                        if (closestDist > dist)
                        {
                            closestDist = dist;
                            closestFit = star;
                        }
                    }

                    m_X0 = (float)closestFit.XCenter;
                    m_Y0 = (float)closestFit.YCenter;
                    m_FWHM = (float)closestFit.FWHM;
                    m_Gaussian = closestFit;

                    nudFitMatrixSize.SetNUDValue(m_IsEdit ? ObjectToAdd.PsfFitMatrixSize : closestFit.MatrixSize);
                }

                //if (m_Gaussian == null && gaussian.Certainty > 0.1 && ImagePixel.ComputeDistance(gaussian.X0_Matrix, 8, gaussian.Y0_Matrix, 8) < 3)
                //{
                //	// Id we failed to locate a bright enough autostar, but the default Gaussian is still certain enough and close enought to the center, we present it as a starting point
                //	m_X0 = (float)gaussian.X0_Matrix;
                //	m_Y0 = (float)gaussian.Y0_Matrix;
                //	m_FWHM = (float)gaussian.FWHM;
                //	m_Gaussian = gaussian;
                //}

                decimal appVal;
                if (float.IsNaN(preselectedAperture))
                {
                    if (float.IsNaN(m_Aperture.Value))
                    {
                        appVal = Convert.ToDecimal(TangraConfig.Settings.Photometry.DefaultSignalAperture);
                    }
                    else
                    {
                        appVal = Convert.ToDecimal(m_Aperture.Value);
                        if (nudAperture1.Maximum < appVal) nudAperture1.Maximum = appVal + 1;
                    }
                }
                else
                    appVal = (decimal)preselectedAperture;

                if ((float)appVal > m_Aperture) m_Aperture = (float)appVal;

                nudAperture1.SetNUDValue(Math.Round(appVal, 2));

                PlotSingleTargetPixels();

                PlotGaussian();

                return true;
            }
            finally
            {
                nudFitMatrixSize.ValueChanged += nudFitMatrixSize_ValueChanged;
            }
        }
 private bool GetFitInMatrix(ITrackedObjectPsfFit gaussian, ref int matirxSize)
 {
     return GetFitInMatrix(gaussian, ref matirxSize, float.NaN);
 }
        public static void DrawGraph(Graphics g, Rectangle rect, int bpp, ITrackedObjectPsfFit trackedPsf, uint saturation)
        {
            float margin = 6.0f;
            double maxZ = 256;

            if (bpp == 14) maxZ = 16384;
            else if (bpp == 12) maxZ = 4096;

            int totalSteps = 100;

            float halfWidth = (float)trackedPsf.MatrixSize / 2.0f;
            float step = (float)(trackedPsf.MatrixSize * 1.0 / totalSteps);

            float yScale = (float)((rect.Height * 1.0 - 2 * margin) / (maxZ - trackedPsf.I0));
            float xScale = (float)(rect.Width * 1.0 - 2 * margin) / (halfWidth * 2);

            float xPrev = float.NaN;
            float yPrev = float.NaN;

            Brush bgBrush = SystemBrushes.ControlDarkDark;
            Brush includedPointsBrush = Brushes.Yellow;
            Brush excludedPointBrush = Brushes.Gray;

            g.FillRectangle(bgBrush, rect);

            if (!trackedPsf.IsSolved)
                return;

            for (int x = 0; x < trackedPsf.MatrixSize; x++)
                for (int y = 0; y < trackedPsf.MatrixSize; y++)
                {
                    double z0 = trackedPsf.GetPSFValueAt(x, y);
                    double z = z0 + trackedPsf.GetResidualAt(x, y);
                    double d = Math.Sqrt((x - trackedPsf.X0) * (x - trackedPsf.X0) + (y - trackedPsf.Y0) * (y - trackedPsf.Y0));

                    int sign = Math.Sign(x - trackedPsf.X0); if (sign == 0) sign = 1;

                    float xVal = (float)(margin + (halfWidth + sign * d) * xScale);
                    float yVal = rect.Height - margin - (float)(z - trackedPsf.I0) * yScale;

                    if (xVal >= 0 && xVal <= rect.Width && yVal >= 0 && yVal <= rect.Height)
                    {
                        Brush pointBrush = z >= saturation ? excludedPointBrush : includedPointsBrush;
                        g.FillRectangle(pointBrush, rect.Left + xVal - 1, rect.Top + yVal - 1, 3, 3);
                    }
                }

            for (float width = -halfWidth; width < halfWidth; width += step)
            {
                float z = (float)trackedPsf.GetPSFValueAt(width + trackedPsf.X0, trackedPsf.Y0);
                float x = margin + (width + halfWidth) * xScale;
                float y = rect.Height - margin - (z - (float)trackedPsf.I0) * yScale;

                if (!float.IsNaN(xPrev) &&
                    y > margin && yPrev > margin &&
                    y < rect.Height && yPrev < rect.Height)
                {
                    g.DrawLine(Pens.LimeGreen, rect.Left + xPrev, rect.Top + yPrev, rect.Left + x, rect.Top + y);
                }

                xPrev = x;
                yPrev = y;
            }
        }
        public static void DrawDataPixels(Graphics g, Rectangle rect, float aperture, Pen aperturePen, int bpp, ITrackedObjectPsfFit trackedPsf)
        {
            try
            {
                if (rect.Width != rect.Height) return;

                int coeff = rect.Width / trackedPsf.MatrixSize;
                if (coeff == 0) return;

                int size = trackedPsf.MatrixSize;

                for (int x = 0; x < size; x++)
                {
                    for (int y = 0; y < size; y++)
                    {
                        double z = Math.Round(trackedPsf.GetPSFValueAt(x, y) + trackedPsf.GetResidualAt(x, y));

                        if (bpp == 14)
                            z = z * 255.0f / 16383;
                        else if (bpp == 12)
                            z = z * 255.0f / 4095;

                        byte val = (byte)(Math.Max(0, Math.Min(255, z)));
                        g.FillRectangle(AllGrayBrushes.GrayBrush(val), rect.Left + x * coeff, rect.Top + y * coeff, coeff, coeff);
                    }
                }

                if (aperture > 0)
                    g.DrawEllipse(
                        aperturePen,
                        rect.Left + ((float)trackedPsf.X0 - aperture + 0.5f) * coeff,
                        rect.Top + ((float)trackedPsf.Y0 - aperture + 0.5f) * coeff,
                        aperture * 2 * coeff, aperture * 2 * coeff);
                else if (aperture < 0)
                {
                    g.DrawLine(aperturePen, rect.Left + ((float)trackedPsf.X0 - 3 * aperture + 0.5f) * coeff, rect.Top + (float)(trackedPsf.Y0 + 0.5f) * coeff, rect.Left + ((float)trackedPsf.X0 - aperture + 0.5f) * coeff, rect.Top + (float)(trackedPsf.Y0 + 0.5f) * coeff);
                    g.DrawLine(aperturePen, rect.Left + ((float)trackedPsf.X0 + 3 * aperture + 0.5f) * coeff, rect.Top + (float)(trackedPsf.Y0 + 0.5f) * coeff, rect.Left + ((float)trackedPsf.X0 + aperture + 0.5f) * coeff, rect.Top + (float)(trackedPsf.Y0 + 0.5f) * coeff);
                    g.DrawLine(aperturePen, rect.Left + (float)(trackedPsf.X0 + 0.5f) * coeff, rect.Top + ((float)trackedPsf.Y0 - 3 * aperture + 0.5f) * coeff, rect.Left + (float)(trackedPsf.X0 + 0.5f) * coeff, rect.Top + ((float)trackedPsf.Y0 - aperture + 0.5f) * coeff);
                    g.DrawLine(aperturePen, rect.Left + (float)(trackedPsf.X0 + 0.5f) * coeff, rect.Top + ((float)trackedPsf.Y0 + 3 * aperture + 0.5f) * coeff, rect.Left + (float)(trackedPsf.X0 + 0.5f) * coeff, rect.Top + ((float)trackedPsf.Y0 + aperture + 0.5f) * coeff);
                }
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.ToString());
            }
        }