Example #1
0
        static void DestroysFeaturesAndBayer(SapFeature feature, SapBayer bayer)
        {
            if (feature != null && feature.Initialized)
            {
                feature.Destroy();
                feature.Dispose();
            }

            if (bayer != null && bayer.Initialized)
            {
                bayer.Destroy();
                bayer.Dispose();
            }
        }
Example #2
0
        static bool ComputeGain(String Name, SapAcqDevice Camera, SapFeature featureInfo, double coefGain)
        {
            int    featureExponent, gainCameraValue, gainGreenMin, gainGreenMax;
            float  powValue;
            double NewGainValuePrecision;
            int    NewGainRounded;

            if (!Camera.GetFeatureInfo(Name, featureInfo))
            {
                return(false);
            }

            // Get Gain Green minimum
            featureInfo.GetValueMin(out gainGreenMin);
            // Get Gain Green maximum
            featureInfo.GetValueMax(out gainGreenMax);
            // Get Gain Value
            Camera.GetFeatureValue(Name, out gainCameraValue);
            // Get Gain exponent
            featureExponent = featureInfo.SiToNativeExp10;
            powValue        = (float)Math.Pow(10, featureExponent);

            if (gainCameraValue == 0)
            {
                NewGainValuePrecision = (coefGain * powValue);
            }
            else
            {
                NewGainValuePrecision = (coefGain * gainCameraValue);
            }

            NewGainRounded = (int)Math.Round(NewGainValuePrecision);

            if (!ValidateWhiteBalance(NewGainRounded, gainGreenMin, gainGreenMax))
            {
                return(false);
            }

            Camera.SetFeatureValue(Name, NewGainRounded);
            return(true);
        }
Example #3
0
        private void FlatFieldDlg_Load(object sender, EventArgs e)
        {
            if (m_pBuffer == null || !m_pBuffer.Initialized)
            {
                MessageBox.Show("Invalid buffer object");
                this.Close();
                return;
            }

            if (m_pFlatField == null)
            {
                MessageBox.Show("Invalid flat-field object");
                this.Close();
                return;
            }

            String str;
            bool   isOffLine = (m_pXfer == null);

            str = String.Format("Step 1: {0} a Dark image", isOffLine ? "Load": "Acquire");
            label_darkImage1.Text = str;

            str = String.Format("{0} a Dark Image", isOffLine ? "Load": "Acquire");
            button_Acq_Dark.Text = str;

            str = String.Format("Step 2: {0} a non-saturated bright image", isOffLine ? "Load": "Acquire");
            label_brightImage1.Text = str;

            str = String.Format("{0} Bright Image", isOffLine ? "Load": "Acquire");
            button_Acq_Bright.Text = str;

            // Calculate recommended values for dark and bright images
            int maxPixelValue = (int)Math.Pow(2.0, m_pBuffer.PixelDepth);

            m_RecommendedDark   = (int)(maxPixelValue * SapDefFlatFieldAvgFactorBlack);
            m_RecommendedBright = (int)(maxPixelValue * SapDefFlatFieldAvgFactorWhite);

            // Adjust the recommended dark value according to the hardware limit for offset coefficients
            int offsetMax = m_pFlatField.OffsetMax;

            if (m_RecommendedDark > offsetMax)
            {
                m_RecommendedDark = offsetMax;
            }

            str = String.Format("(recommended average pixel\nvalue below {0})", m_RecommendedDark);
            label_darkImage2.Text = str;

            str = String.Format("(recommended average pixel\nvalue around {0})", m_RecommendedBright);
            label_brightImage2.Text = str;

            // Adjust maximum black deviation from the adjusted recommended dark value
            if (m_pFlatField.DeviationMaxBlack > m_RecommendedDark)
            {
                m_pFlatField.DeviationMaxBlack = m_RecommendedDark;
            }

            // Query correction type, video type, number of lines to average (line scan),
            // vertical offset, gain divisor, maximum deviation, and calibration index
            // for the dark image from flat field object
            m_CorrectionType         = m_pFlatField.CorrectionType;
            m_VideoType              = m_pFlatField.AcqVideoType;
            textBox_Line_Avg.Text    = m_pFlatField.NumLinesAverage.ToString();
            textBox_Vert_Offset.Text = m_pFlatField.VerticalOffset.ToString();
            textBox_Max_Dev.Text     = m_pFlatField.DeviationMaxBlack.ToString();
            //m_CalibrationIndex = m_pFlatField->GetIndex();
            textBox_Frame_Avg.Text = DefNumFramesAverage.ToString();
            ClippedCoefsDefects_checkbox.Checked = m_pFlatField.ClippedGainOffsetDefects ? true : false;

            // Set correction type
            comboBox_Correction_Type.Items.Add("Flat Field");
            comboBox_Correction_Type.Items.Add("Flat Line");
            comboBox_Correction_Type.SelectedIndex = m_CorrectionType == SapFlatField.ScanCorrectionType.Field ? 0 : 1;

            // Set video type
            comboBox_Video_Type.Items.Add("Monochrome");
            comboBox_Video_Type.Items.Add("Bayer");
            comboBox_Video_Type.SelectedIndex = m_VideoType != SapAcquisition.VideoType.Bayer ? 0: 1;


            ///////////////////////////////////////////////////
            //Multi flat-field not implemented in .NET/////////
            //////////////////////////////////////////////////
            // Set calibration index
            // String indexStr;

            //for (int index = 0; index < m_pFlatFieldGetNumFlatField(); index++)
            //{
            //   indexStr.Format("%d", index);
            //   m_CalibrationIndexCtrl.AddString( indexStr);
            //}

            comboBox_Calibration_Index.Items.Add(0);
            comboBox_Calibration_Index.SelectedIndex = 0;


            if (m_pFlatField.AcqDevice != null)
            {
                SapFeature feature = new SapFeature(m_pFlatField.AcqDevice.Location);
                feature.Create();

                Boolean status = false;
                String  deviceModel;

                //for all Genies except Genie TS, do not try to do a file access
                status = m_pFlatField.AcqDevice.IsFeatureAvailable("DeviceModelName");
                if (status)
                {
                    m_pFlatField.AcqDevice.GetFeatureValue("DeviceModelName", out deviceModel);
                    if (deviceModel.Contains("Genie") && !deviceModel.Contains("TS"))
                    {
                        //device is a Genie but not Genie TS

                        m_isGenie = true;
                        button_Save_and_Upload.Text         = "Save";
                        comboBox_FlatField_Selector.Visible = false;
                        label_flatField.Visible             = false;
                        goto nextPart;
                    }
                }

                for (int i = 0; i < m_pFlatField.AcqDevice.FileCount; i++)
                {
                    if (m_pFlatField.AcqDevice.FileNames[i].Contains("FlatField"))
                    {
                        String featureName = "##FileSelector." + m_pFlatField.AcqDevice.FileNames[i];

                        if (!m_pFlatField.AcqDevice.FileNames[i].Contains("0"))
                        {
                            if (m_pFlatField.AcqDevice.GetFeatureInfo(featureName, feature))
                            {
                                comboBox_FlatField_Selector.Items.Add(feature.DisplayName);
                            }
                            else
                            {
                                comboBox_FlatField_Selector.Items.Add(m_pFlatField.AcqDevice.FileNames[i]);
                            }
                            flatFieldIndexes[m_UserFlatFieldCount++] = i;
                            // comboBox_FlatField_Selector.items (m_UserFlatFieldCount++, i);
                        }
                    }
                }

                comboBox_FlatField_Selector.SelectedIndex = 0;
                feature.Destroy();
                saveLabel.Text = "Save Calibration offset and gain files";
            }
            else
            {
                button_Save_and_Upload.Text         = "Save";
                comboBox_FlatField_Selector.Visible = false;
                label_flatField.Visible             = false;
            }
nextPart:
            // Enable/disable controls according to current properties
            // Changing the correction type (flat-field vs flat-line) is only relevant when operating
            // offline, that is, when this information is not available from the acquisition hardware.
            bool isOnline = (m_pXfer != null && m_pXfer.Initialized);

            comboBox_Correction_Type.Enabled = !isOnline && m_VideoType != SapAcquisition.VideoType.Bayer;
            comboBox_Video_Type.Enabled      = m_pXfer == null;

            textBox_Frame_Avg.Enabled   = m_pXfer != null;
            textBox_Line_Avg.Enabled    = m_CorrectionType == SapFlatField.ScanCorrectionType.Line;
            textBox_Vert_Offset.Enabled = m_CorrectionType == SapFlatField.ScanCorrectionType.Line;
            textBox_Max_Dev.Enabled     = true;

            //Multi flat-field not implemented in .NET
            comboBox_Calibration_Index.Enabled = false; //m_pFlatField->GetNumFlatField() > 1);

            button_Acq_Dark.Enabled             = true;
            button_Acq_Bright.Enabled           = false;
            button_OK.Enabled                   = false;
            button_Save_and_Upload.Enabled      = false;
            comboBox_FlatField_Selector.Enabled = false;
        }
Example #4
0
        static bool AutoWhiteBalanceOperations(SapAcqDevice Camera, SapBuffer Buffers, SapTransfer Transfer)
        {
            Console.WriteLine("\nCalibration in progress ...........\n\n");

            double coefBlueGain         = MAX_COEF + 1;
            double coefGreenGain        = MAX_COEF + 1;
            double coefRedGain          = MAX_COEF + 1;
            int    calibrationIteration = 0;

            // Create a new Bayer object
            SapBayer   Bayer       = new SapBayer(Buffers);
            SapFeature FeatureInfo = new SapFeature(Camera.Location);

            if (!FeatureInfo.Create())
            {
                DestroysFeaturesAndBayer(FeatureInfo, Bayer);
                return(false);
            }

            // Create Bayer object
            if (!Bayer.Create())
            {
                DestroysFeaturesAndBayer(FeatureInfo, Bayer);
                return(false);
            }

            // Initialize all Gain colors to 0
            Camera.SetFeatureValue("GainBlue", 0);
            Camera.SetFeatureValue("GainGreen", 0);
            Camera.SetFeatureValue("GainRed", 0);

            // Choose alignment used
            Bayer.Align = SapBayer.AlignMode.RGGB;

            // Definition of ROI used for calibration
            int fixSelectedRoiLeft = 0;
            int fixSelectedRoiTop  = 0;
            // Half buffer width
            int fixSelectedRoiWidth = Buffers.Width / 2;
            // Half buffer height
            int fixSelectedRoiHeight = Buffers.Height / 2;

            // Start loop for calibration until each coefficient is under 1.05
            while (coefBlueGain > MAX_COEF || coefGreenGain > MAX_COEF || coefRedGain > MAX_COEF)
            {
                if (!Transfer.Snap())
                {
                    Console.WriteLine("Unable to acquire an image");
                    return(false);
                }

                Thread.Sleep(500);
                // Call WhiteBalance function
                if (!Bayer.WhiteBalance(Buffers, fixSelectedRoiLeft, fixSelectedRoiTop, fixSelectedRoiWidth, fixSelectedRoiHeight))
                {
                    break;
                }

                // New coefficients values are reused.
                coefBlueGain  = Bayer.WBGain.Blue;
                coefGreenGain = Bayer.WBGain.Green;
                coefRedGain   = Bayer.WBGain.Red;

                if (coefRedGain > MAX_COEF)
                {
                    if (!ComputeGain("GainRed", Camera, FeatureInfo, coefRedGain))
                    {
                        break;
                    }
                }
                if (coefGreenGain > MAX_COEF)
                {
                    if (!ComputeGain("GainGreen", Camera, FeatureInfo, coefGreenGain))
                    {
                        break;
                    }
                }
                if (coefBlueGain > MAX_COEF)
                {
                    if (!ComputeGain("GainBlue", Camera, FeatureInfo, coefBlueGain))
                    {
                        break;
                    }
                }

                if (calibrationIteration >= MAX_CALIBRATION_ITERATION)
                {
                    Console.WriteLine("Iterations for calibration are at the maximum.\n");
                    break;
                }
                calibrationIteration++;
            }

            // Uncomment this part if you want to get new values after calibration.

            /*
             * int gainBlue=0, gainRed=0, gainGreen=0;
             * Camera.GetFeatureValue("GainBlue", out gainBlue);
             * Camera.GetFeatureValue("GainRed", out gainRed);
             * Camera.GetFeatureValue("GainGreen", out gainGreen);
             */

            DestroysFeaturesAndBayer(FeatureInfo, Bayer);
            Console.WriteLine("\nCalibration finished ...........\n\n");
            return(true);
        }