private void GrabLoop(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            while (m_grabImages)
            {
                try
                {
                    m_camera.RetrieveBuffer(m_rawImage);
                }
                catch (FC2Exception ex)
                {
                    Debug.WriteLine("Error: " + ex.Message);
                    continue;
                }

                lock (this)
                {
                    m_rawImage.Convert(PixelFormat.PixelFormatBgr, m_processedImage);
                }

                worker.ReportProgress(0);
            }

            m_grabThreadExited.Set();
        }
Beispiel #2
0
        /// <summary>
        /// 抓取相机的图像,输出所有的格式
        /// </summary>
        public override ImageAll GrabImageAll()
        {
            g_MtGrabImage.WaitOne();
            ImageAll imageAll = new ImageAll();

            try
            {
                if (!BlOpen)//如果相机打开失败
                {
                    return(null);
                }
#if (SDK)
                try
                {
                    SetTrriggerMode();//设置软触发
                    g_ManagedCameraBase.RetrieveBuffer(g_RawImage);
                }
                catch (Exception ex)
                {
                    Log.L_I.WriteError(NameClass, ex);
                    try
                    {
                        if (g_BaseParCamera.BlUsingTrigger)                       //使用软触发
                        {
                            g_ManagedCameraBase.WriteRegister(0x62C, 0x80000000); //发送软触发指令
                        }
                        g_ManagedCameraBase.RetrieveBuffer(g_RawImage);
                    }
                    catch
                    {
                        if (g_BaseParCamera.BlUsingTrigger)                       //使用软触发
                        {
                            g_ManagedCameraBase.WriteRegister(0x62C, 0x80000000); //发送软触发指令
                        }
                        g_ManagedCameraBase.RetrieveBuffer(g_RawImage);
                    }
                }

                //将图像转换为Halcon格式
                imageAll.Ho_Image     = ConvertHobject();
                imageAll.BitmapSource = ConvertImageBitmap();
                PathDirectory p = new PathDirectory();
                imageAll.Time = p.GetShortTimeName();
#endif
                return(imageAll);
            }
            catch (Exception ex)
            {
                Log.L_I.WriteError(NameClass, ex);
                return(null);
            }
            finally
            {
                g_MtGrabImage.ReleaseMutex();
            }
        }
Beispiel #3
0
        public Bitmap GrabImage()
        {
            try
            {
                if (m_camera.IsConnected())
                {
                    m_camera.StartCapture();
                    m_camera.RetrieveBuffer(m_rawImage);
                    m_rawImage.Convert(PixelFormat.PixelFormatBgr, m_processedImage);
                    m_camera.StopCapture();
                    imageSize.Width  = (int)m_rawImage.cols;
                    imageSize.Height = (int)m_rawImage.rows;
                    timeStamp        = m_rawImage.timeStamp;
                }
            }
            catch (FC2Exception ex)
            {
                Debug.WriteLine("Error: " + ex.Message);
            }

            return(m_processedImage.bitmap);
        }
Beispiel #4
0
        private void GrabLoop(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;

            while (m_grabImages)
            {
                try
                {
                    m_camera.RetrieveBuffer(m_rawImage);
                }
                catch (FC2Exception ex)
                {
                    Debug.WriteLine("Error: " + ex.Message);
                    continue;
                }

                lock (this)
                {
                    m_rawImage.Convert(PixelFormat.PixelFormatBgr, m_processedImage);
                    //sum = sum + 1;
                }

                System.Drawing.Imaging.PixelFormat format =
                    m_processedImage.bitmap.PixelFormat;
                if (checkBoxTestMode.Checked && checkBoxShow.Checked == false)
                {
                    label6.Text = (testCount.ToString("000"));
                    try {
                        newimageTEST = new Bitmap(savepath + "TestImages\\trimmed" + testCount.ToString("000") + ".bmp");
                        newimage     = newimageTEST.Clone(cloneRect, format);
                    }
                    catch { stopCaptureLoop(); MessageBox.Show("Images not found"); }
                    testCount++;
                    if (testCount > 435)
                    {
                        testCount = 0;
                        CheckBoxRecordValues.Checked = false;
                    }
                }

                if (checkBoxShow.Checked)
                {
                    numSaved = Directory.GetFiles(savepath + "\\SavedMaxima", "*", SearchOption.TopDirectoryOnly).Length;
                    if (showCount < numSaved)
                    {
                        try
                        {
                            newimageTEST = new Bitmap(savepath + "\\SavedMaxima\\" + showCount.ToString("000") + ".bmp");
                            newimage     = newimageTEST.Clone(cloneRect, format);
                        }
                        catch { stopCaptureLoop(); MessageBox.Show("Images not found: " + savepath + "\\SavedMaxima\\" + showCount.ToString("000") + ".bmp"); }
                        showCount++;
                    }
                    else
                    {
                        showCount = 0;
                    }
                }
                if (checkBoxShow.Checked == false && checkBoxTestMode.Checked == false)
                {
                    newimage = m_processedImage.bitmap.Clone(cloneRect, format);
                }

                lockBitmap = new LockBitmap(newimage);
                lockBitmap.LockBits();

                sum            = 0;
                sumW           = 0;
                aveSumPrevious = aveSum;
                aveSum         = 0;
                if (CheckBoxAlgorithm2.Checked)
                {
                    /* //this was the previous alternative algorithm
                     * for (int y = ya; y < yb; y = y + 1)
                     * {
                     *  for (int x = xa; x < xb; x = x + 1)
                     *  {
                     *      sumW = sumW + (x - xa) * (lockBitmap.GetPixel(x, y).G + lockBitmap.GetPixel(x, y).R); //the .R selects the red component of the RGB color
                     *      sum = sum + (lockBitmap.GetPixel(x, y).G + lockBitmap.GetPixel(x, y).R);
                     *  }
                     *  aveSum = aveSum + Convert.ToInt32(1000*sumW / sum);
                     * }
                     * aveSum = aveSum / (yb - ya);
                     * lockBitmap.UnlockBits();*/
                }
                else
                {
                    for (int y = ya; y < yb; y = y + 1)
                    {
                        for (int x = xa; x < xb; x = x + 1)
                        {
                            sum = sum + (lockBitmap.GetPixel(x, y).G + lockBitmap.GetPixel(x, y).R); //the .R selects the red component of the RGB color
                        }
                    }
                    sumBackground = 0;
                    if (checkBoxBackgroundCorrection.Checked)
                    {
                        for (int y = 160; y < 320; y = y + 3)
                        {
                            //TESTint = 0;
                            for (int x = 214; x < 426; x = x + 3)
                            {
                                // TESTint = TESTint + 1;
                                sumBackground = sumBackground + (lockBitmap.GetPixel(x, y).G + lockBitmap.GetPixel(x, y).R); //the .R selects the red component of the RGB color
                            }
                        }
                    }

                    lockBitmap.UnlockBits();
                    if (((yb - ya) * (xb - xa)) != 0)
                    {
                        ave = Convert.ToDouble(sum) / ((yb - ya) * (xb - xa));
                    }
                    if (checkBoxBackgroundCorrection.Checked)
                    {
                        aveBackground = Convert.ToDouble(sumBackground) / (54 * 71);
                    }
                    else
                    {
                        aveBackground = 1;
                    }

                    aveSum = Convert.ToInt32(((10000 * ave / aveBackground)));
                }



                if (checkBoxMax.Checked)
                {
                    if (UseSensitiveAlgorithmCheckbox.Checked)
                    {
                        for (int i = 1; i < longarray.Length; i++)
                        {
                            longarray[i - 1] = longarray[i];
                        }
                        longarray[longarray.Length - 1] = aveSum;
                        pastAverage = Convert.ToInt32(longarray.Average());

                        if (radioButtonUp.Checked)
                        {
                            if ((aveSum > pastAverage) & (aveSumPrevious < pastAverage))
                            {
                                ismax = true;
                                if (checkBoxSaveMax.Checked)
                                {
                                    newimage.Save(savepath + "\\SavedMaxima\\" + saveCount.ToString("000") + ".bmp");
                                    saveCount++;
                                }
                            }
                            else
                            {
                                ismax = false;
                            }
                        }
                        else if (radioButtonDown.Checked)
                        {
                            if ((aveSum < pastAverage) & (aveSumPrevious > pastAverage))
                            {
                                ismax = true;
                                if (checkBoxSaveMax.Checked)
                                {
                                    newimage.Save(savepath + "\\SavedMaxima\\" + saveCount.ToString("000") + ".bmp");
                                    saveCount++;
                                }
                            }
                            else
                            {
                                ismax = false;
                            }
                        }
                    }
                    else
                    {
                        array[0] = array[1];
                        array[1] = array[2];
                        array[2] = array[3];
                        array[3] = array[4];
                        array[4] = aveSum;
                        if (checkBoxTriggerOnMinimum.Checked)
                        {
                            if ((array[1] < array[0]) && (array[2] < array[1]) && (array[2] < array[3]) && (array[3] < array[4]))
                            {
                                ismax = true;
                                if (checkBoxSaveMax.Checked)
                                {
                                    newimage.Save(savepath + "\\SavedMaxima\\" + saveCount.ToString("000") + ".bmp");
                                    saveCount++;
                                }
                            }
                            else
                            {
                                ismax = false;
                            }
                        }
                        else
                        {
                            if ((array[1] > array[0]) && (array[2] > array[1]) && (array[2] > array[3]) && (array[3] > array[4]))
                            {
                                ismax = true;
                                if (checkBoxSaveMax.Checked)
                                {
                                    newimage.Save(savepath + "\\SavedMaxima\\" + saveCount.ToString("000") + ".bmp");
                                    saveCount++;
                                }
                            }
                            else
                            {
                                ismax = false;
                            }
                        }
                    }
                }
                else
                {
                    ismax = false;
                }

                if (readyToRecordValues)
                {
                    if (ismax)
                    {
                        if (CheckBoxAlgorithm2.Checked)
                        {
                            aveSumCopy = aveSum + 0;                                //+1500; to highlight the max value in a graph.
                        }
                        else
                        {
                            aveSumCopy = aveSum + 0;  // 700;
                        }
                    }
                    else
                    {
                        aveSumCopy = aveSum;
                    }
                    using (System.IO.StreamWriter file = new System.IO.StreamWriter(@savepath + "values.txt", true))
                    {
                        file.Write(aveSumCopy.ToString());
                        if (ismax)
                        {
                            file.Write("\t1");
                        }
                        else
                        {
                            file.Write("\t0");
                        }
                        if (FakePulse)
                        {
                            file.WriteLine("\t1");
                            FakePulse = false;
                        }
                        else
                        {
                            file.WriteLine("\t0");
                        }
                    }

                    /*using (System.IO.StreamWriter file = new System.IO.StreamWriter(@"C:\tmp\values2.txt", true))
                     * {
                     *  file.WriteLine(array[0]+" " +array[1]+" " +array[2]+" " +array[3]+" " +array[4]);
                     * }*/
                }



                if (My_count == 0)
                {
                    stopWatch.Start();
                }

                My_count = My_count + 1;
                if (My_count > 10)
                {
                    My_count = 0;
                    stopWatch.Stop();
                    ts          = stopWatch.Elapsed;
                    elapsedTime = String.Format("{0:0.0}", (10 / ts.TotalSeconds));  //hertz (10 images over their duration)
                    //elapsedTime = String.Format("{0:00}.{1:00}",ts.Seconds,ts.Milliseconds / 10);
                    stopWatch.Reset();
                }
                Thread.Sleep(10);  //this sleep is neccessary, otherwise it gets close to 60fps and UpdateUI can't go that fast.
                worker.ReportProgress(0);
            }
            m_grabThreadExited.Set();
        }