コード例 #1
0
        private void button_AutoWhite_Click(object sender, EventArgs e)
        {
            if (m_pImageWnd == null)
            {
                return;
            }

            if (m_pImageWnd.IsTrackerEmpty)
            {
                MessageBox.Show("You must select a ROI containing white pixels");
                return;
            }

            if (m_pBayer.Enabled && !m_pBayer.SoftwareConversion)
            {
                MessageBox.Show("White balance is not available when hardware Bayer conversion is enabled");
                return;
            }

            Rectangle rect = m_pImageWnd.Tracker;

            if (rect.Width > 1 && rect.Height > 1)
            {
                // Compute new white balance factors from region of interest
                if (m_pBayer.WhiteBalance(rect.Left, rect.Top, rect.Width, rect.Height))
                {
                    // Update user interface
                    UpdateInterface();

                    // Redraw the image
                    UpdateView();
                }
            }
        }
コード例 #2
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);
        }