예제 #1
0
        private void FindLine_form_Load(object sender, EventArgs e)
        {
            HOperatorSet.OpenWindow(0, 0, pictureBox1.Width, pictureBox1.Height, pictureBox1.Handle, "", "", out Hwin_tool);
            Hwin = Hwin_tool;
            HDevWindowStack.Push(Hwin);

            HOperatorSet.SetSystem("clip_region", "false");
            HOperatorSet.SetDraw(Hwin, "margin");
            HOperatorSet.SetColor(Hwin, "red");
            load_parm();
            try
            {
                HOperatorSet.GetImageSize(image_show, out Hwidth, out Hheight);
                HOperatorSet.SetPart(Hwin, 0, 0, Hheight, Hwidth);
                HOperatorSet.DispObj(image_show, Hwin);
            }
            catch
            {
            }
            try
            {
                if (Refresh_parm(FL_data))
                {
                    if (FL_data.FindLine(image_show))
                    {
                        show_pic_roi();
                        FL_data.DrawRecord(Hwin);
                    }
                }
            }
            catch
            {
            }
        }
예제 #2
0
        private void BtnOpenFile_Click(object sender, RoutedEventArgs e)
        {
            if (hv_WindowHandle != null)
            {
                HOperatorSet.CloseWindow(hv_WindowHandle);  //执行清空控件窗口
            }

            //对话框设置
            string ImagePath;

            openFileDialog.Filter           = "JPEG文件|*.jpg|BMP文件|*.bmp|png文件|*.png";
            openFileDialog.RestoreDirectory = true;

            HOperatorSet.GenEmptyObj(out ho_Image);
            HOperatorSet.GenEmptyObj(out ho_GrayImage);
            HOperatorSet.GenEmptyObj(out ho_Regions);
            ho_Image.Dispose();
            if (openFileDialog.ShowDialog() == true)
            {
                //将打开的文件名赋给ImagePath字符串变量
                ImagePath = openFileDialog.FileName;
                HOperatorSet.ReadImage(out ho_Image, ImagePath);
            }

            //打开窗口,将显示图片控件的句柄赋给窗口句柄,使窗口在控件中打开,并设置窗口宽高为显示图片控件的宽高
            HOperatorSet.OpenWindow(0, 0, HWCCameraImage.Width, HWCCameraImage.Height, HWCCameraImage.HalconWindow, "", "", out hv_WindowHandle);
            HDevWindowStack.Push(hv_WindowHandle);


            if (HDevWindowStack.IsOpen())
            {
                HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive());
            }
        }
예제 #3
0
        private void Form1_Load(object sender, EventArgs e)
        {
            //1打开窗口、赋值线程状态
            //2枚举设备
            //3.初始化设备
            //3.申请内存  获取特性 获取位数
            //4.设置触发模式
            //5.开启绑定线程
            //6开始采集
            //6开始采集(默认连续,不设置采集模式的话)
            Control.CheckForIllegalCrossThreadCalls = false;
            HOperatorSet.OpenWindow(0, 0, pictureBox1.Width, pictureBox1.Height, pictureBox1.Handle, "", "", out hv_WindowHandle);
            HDevWindowStack.Push(hv_WindowHandle);
            status = MvApi.CameraEnumerateDevice(out tCameraDevInfoList);
            //枚举设备
            if (tCameraDevInfoList != null)//此时iCameraCounts返回了实际连接的相机个数。如果大于1,则初始化第一个相机
            {
                status = MvApi.CameraInit(ref tCameraDevInfoList[0], -1, -1, ref m_hCamera);
                //初始化第一个相机

                //获得相机特性描述
                MvApi.CameraGetCapability(m_hCamera, out tCameraCapability);

                //申请内存
                m_ImageBuffer = Marshal.AllocHGlobal(tCameraCapability.sResolutionRange.iWidthMax * tCameraCapability.sResolutionRange.iHeightMax * 3 + 1024);
                //MvApi.CameraSetTriggerMode(m_hCamera, 0);
                m_bExitCaptureThread = false;
                m_tCaptureThread     = new Thread(new ThreadStart(CaptureThreadProc));
                m_tCaptureThread.Start();
                MvApi.CameraPlay(m_hCamera);
            }
        }
예제 #4
0
    // Main procedure
    private void action()
    {
        // Local iconic variables

        HObject ho_image;

        // Local control variables

        HTuple hv_Width = null, hv_Height = null, hv_WindowHandle = null;

        // Initialize local and output iconic variables
        HOperatorSet.GenEmptyObj(out ho_image);
        if (HDevWindowStack.IsOpen())
        {
            HOperatorSet.CloseWindow(HDevWindowStack.Pop());
        }
        ho_image.Dispose();
        HOperatorSet.ReadImage(out ho_image, "G:/blazarlin/calibrationdata/others/2020点胶机/旋转法/Images/mark/image mark0.bmp");
        HOperatorSet.GetImageSize(ho_image, out hv_Width, out hv_Height);
        HOperatorSet.SetWindowAttr("background_color", "black");
        HOperatorSet.OpenWindow(0, 0, hv_Width / 1.2, hv_Height / 1.22, 0, "visible", "", out hv_WindowHandle);
        HDevWindowStack.Push(hv_WindowHandle);
        if (HDevWindowStack.IsOpen())
        {
            HOperatorSet.DispObj(ho_image, HDevWindowStack.GetActive());
        }
        ho_image.Dispose();
    }
예제 #5
0
        private void btn_CameraSwitch_Click(object sender, EventArgs e)
        {
            HDevWindowStack.Push(hv_WindowHandle);
            if (m_Grabber_1 != IntPtr.Zero || m_Grabber_2 != IntPtr.Zero)
            {
                if (CameraSwitch)
                {
                    label_CameraNo.Text = "2";
                    if (m_Grabber_1 != IntPtr.Zero)
                    {
                        MvApi.CameraGrabber_StopLive(m_Grabber_1);
                    }

                    if (m_Grabber_2 != IntPtr.Zero)
                    {
                        MvApi.CameraGrabber_StartLive(m_Grabber_2);
                    }
                    CameraSwitch = false;
                }
                else
                {
                    label_CameraNo.Text = "1";
                    if (m_Grabber_2 != IntPtr.Zero)
                    {
                        MvApi.CameraGrabber_StopLive(m_Grabber_2);
                    }

                    if (m_Grabber_1 != IntPtr.Zero)
                    {
                        MvApi.CameraGrabber_StartLive(m_Grabber_1);
                    }
                    CameraSwitch = true;
                }
            }
        }
예제 #6
0
 public HDevelopExport(HWindow halconWindow)
 {
     HOperatorSet.SetSystem("width", 512);
     HOperatorSet.SetSystem("height", 512);
     this.halconWindow = halconWindow;
     //hv_WindowHandle = halconWindow;
     HDevWindowStack.Push(halconWindow);
 }
예제 #7
0
 private void GetAndSaveImageTool_Load(object sender, EventArgs e)
 {
     HOperatorSet.OpenWindow(0, 0, (pictureBoxGetAndSaveImage.Width), (pictureBoxGetAndSaveImage.Height), pictureBoxGetAndSaveImage.Handle, "visible", "", out GetAndSaveImageHWHandle);
     HOperatorSet.SetPart(GetAndSaveImageHWHandle, 0, 0, (pictureBoxGetAndSaveImage.Height), (pictureBoxGetAndSaveImage.Width));
     HDevWindowStack.Push(GetAndSaveImageHWHandle);
     oriPictureBoxShowImageWidth  = pictureBoxGetAndSaveImage.Width;
     oriPictureBoxShowImageHeight = pictureBoxGetAndSaveImage.Height;
 }
    // Main procedure
    private void action()
    {
        // Local iconic variables

        HObject ho_Image = null;


        // Local control variables

        HTuple hv_WindowHandle, hv_AcqHandle, hv_Index;
        HTuple hv_Ex = new HTuple();

        // Initialize local and output iconic variables
        HOperatorSet.GenEmptyObj(out ho_Image);

        try
        {
            //Code generated by Image Acquisition 01
            //Attention: The initialization may fail in case parameters need to
            //be set in a specific order (e.g., image resolution vs. offset).
            HOperatorSet.SetWindowAttr("background_color", "black");
            HOperatorSet.OpenWindow(0, 0, 512, 512, 0, "", "", out hv_WindowHandle);
            HDevWindowStack.Push(hv_WindowHandle);
            HOperatorSet.OpenFramegrabber("GigEVision", 0, 0, 0, 0, 0, 0, "progressive",
                                          -1, "default", -1, "false", "default", "Cam_Up", 0, -1, out hv_AcqHandle);
            HOperatorSet.SetFramegrabberParam(hv_AcqHandle, "TriggerSelector", "FrameStart");
            HOperatorSet.SetFramegrabberParam(hv_AcqHandle, "grab_timeout", 10000);
            HOperatorSet.SetFramegrabberParam(hv_AcqHandle, "TriggerMode", "On");
            HOperatorSet.SetFramegrabberParam(hv_AcqHandle, "TriggerSource", "Line1");
            HOperatorSet.GrabImageStart(hv_AcqHandle, -1);
            hv_Index = 1;
            while ((int)(1) != 0)
            {
                try
                {
                    ho_Image.Dispose();
                    HOperatorSet.GrabImageAsync(out ho_Image, hv_AcqHandle, -1);
                    hv_Index = hv_Index + 1;
                    disp_message(hv_WindowHandle, hv_Index, "window", 12, 12, "black", "true");
                }
                // catch (Ex)
                catch (HalconException HDevExpDefaultException1)
                {
                    HDevExpDefaultException1.ToHTuple(out hv_Ex);
                }

                //Do something
            }
            HOperatorSet.CloseFramegrabber(hv_AcqHandle);
        }
        catch (HalconException HDevExpDefaultException)
        {
            ho_Image.Dispose();

            throw HDevExpDefaultException;
        }
        ho_Image.Dispose();
    }
예제 #9
0
        public CalForm()
        {
            InitializeComponent();

            HOperatorSet.OpenWindow(0, 0, pictureBox_CalibrationCamera.Width, pictureBox_CalibrationCamera.Height, pictureBox_CalibrationCamera.Handle, "visible", "", out hv_WindowHandle);
            HDevWindowStack.Push(hv_WindowHandle);
            HOperatorSet.SetColor(HDevWindowStack.GetActive(), "red");
            m_FrameCallback = new pfnCameraGrabberFrameCallback(CameraGrabberFrameCallback);
        }
예제 #10
0
        private void BackgroundRemoveFBDForm_Load(object sender, EventArgs e)
        {
            HOperatorSet.OpenWindow(0, 0, (pictureBoxBackgroundRemove.Width), (pictureBoxBackgroundRemove.Height), pictureBoxBackgroundRemove.Handle, "visible", "", out BackgroundRemoveHWHandle);
            HOperatorSet.SetPart(BackgroundRemoveHWHandle, 0, 0, (pictureBoxBackgroundRemove.Height - 1), (pictureBoxBackgroundRemove.Width - 1));
            HDevWindowStack.Push(BackgroundRemoveHWHandle);


            oriPictureBoxShowImageWidth  = pictureBoxBackgroundRemove.Width;
            oriPictureBoxShowImageHeight = pictureBoxBackgroundRemove.Height;
        }
        private void MedianFilterFBDForm_Load(object sender, System.EventArgs e)
        {
            HOperatorSet.OpenWindow(0, 0, (pictureBoxMedian.Width), (pictureBoxMedian.Height), pictureBoxMedian.Handle, "visible", "", out MedianHWHandle);
            HOperatorSet.SetPart(MedianHWHandle, 0, 0, (pictureBoxMedian.Height - 1), (pictureBoxMedian.Width - 1));
            HDevWindowStack.Push(MedianHWHandle);

            groupBoxMedianFilterFBDMaskSize.Enabled = false;
            oriPictureBoxShowImageWidth             = pictureBoxMedian.Width;
            oriPictureBoxShowImageHeight            = pictureBoxMedian.Height;
        }
예제 #12
0
        private void ThresholdFBDForm_Load(object sender, EventArgs e)
        {
            HOperatorSet.OpenWindow(0, 0, (pictureBoxThreshold.Width), (pictureBoxThreshold.Height), pictureBoxThreshold.Handle, "visible", "", out ThresholdHWHandle);
            HOperatorSet.SetPart(ThresholdHWHandle, 0, 0, (pictureBoxThreshold.Height - 1), (pictureBoxThreshold.Width - 1));
            HDevWindowStack.Push(ThresholdHWHandle);

            panelenable.Enabled          = false;
            oriPictureBoxShowImageWidth  = pictureBoxThreshold.Width;
            oriPictureBoxShowImageHeight = pictureBoxThreshold.Height;
        }
예제 #13
0
        private void getRectCenter()
        {
            // Local iconic variables

            HObject ho_image, ho_Region, ho_RegionTrans;
            HObject ho_RegionBorder;

            // Local control variables

            HTuple hv_Width = null, hv_Height = null, hv_WindowHandle = hWindowControl1.HalconWindow;
            HTuple hv_Area = null, hv_Row = null, hv_Column = null;

            // Initialize local and output iconic variables
            HOperatorSet.GenEmptyObj(out ho_image);
            HOperatorSet.GenEmptyObj(out ho_Region);
            HOperatorSet.GenEmptyObj(out ho_RegionTrans);
            HOperatorSet.GenEmptyObj(out ho_RegionBorder);
            //if (HDevWindowStack.IsOpen())
            //{
            //    HOperatorSet.CloseWindow(HDevWindowStack.Pop());
            //}
            //ho_image.Dispose();
            image_path = "G:/blazarlin/my_halcon_function/rect.png";
            HOperatorSet.ReadImage(out ho_image, image_path);
            HOperatorSet.GetImageSize(ho_image, out hv_Width, out hv_Height);
            HOperatorSet.SetWindowAttr("background_color", "black");
            //HOperatorSet.OpenWindow(0, 0, hv_Width / 1.2, hv_Height / 1.22, 0, "visible", "", out hv_WindowHandle);
            HDevWindowStack.Push(hv_WindowHandle);
            //阈值
            //ho_Region.Dispose();
            HOperatorSet.Threshold(ho_image, out ho_Region, 128, 255);
            // 外接矩形
            // ho_RegionTrans.Dispose();
            HOperatorSet.ShapeTrans(ho_Region, out ho_RegionTrans, "rectangle1");
            // 获取中心
            HOperatorSet.AreaCenter(ho_RegionTrans, out hv_Area, out hv_Row, out hv_Column);
            // 外边界
            // ho_RegionBorder.Dispose();
            HOperatorSet.Boundary(ho_RegionTrans, out ho_RegionBorder, "outer");
            HOperatorSet.DispObj(ho_image, HDevWindowStack.GetActive());
            // 显示边界
            // 设置绘制器的颜色
            HOperatorSet.SetColor(HDevWindowStack.GetActive(), "red");
            HOperatorSet.DispCross(hv_WindowHandle, hv_Row, hv_Column, 6, 0);
            HOperatorSet.SetColor(HDevWindowStack.GetActive(), "green");
            HOperatorSet.DispObj(ho_RegionBorder, HDevWindowStack.GetActive());
            HOperatorSet.SetTposition(hv_WindowHandle, hv_Row, hv_Column);
            HOperatorSet.WriteString(hv_WindowHandle, (hv_Row + new HTuple(",")) + hv_Column);

            //HOperatorSet.DispObj(ho_RegionBorder, hWindowControl1.HalconWindow);
            ho_image.Dispose();
            ho_Region.Dispose();
            ho_RegionTrans.Dispose();
            ho_RegionBorder.Dispose();
        }
예제 #14
0
        public FormMain(string[] args)
        {
            InitializeComponent();

            if (args.Length > 0)
            {
                imageWindow = new FormDockImageWindow();
                HDevWindowStack.Push(imageWindow.Window.HalconWindow);
                processBar = new FormDockProcessBar(args[0]);
            }
        }
예제 #15
0
        private void btn_TwoCameras_Find_Photos_Click(object sender, EventArgs e)
        {
            if (m_Grabber_1 != IntPtr.Zero || m_Grabber_2 != IntPtr.Zero)
            {
                HDevWindowStack.Push(hv_WindowHandle_1);

                HDevWindowStack.Push(hv_WindowHandle_2);
                MvApi.CameraGrabber_StartLive(m_Grabber_2);
                MvApi.CameraGrabber_StartLive(m_Grabber_1);
            }
        }
예제 #16
0
        /// <summary>
        /// 创建DockContext
        /// </summary>
        private void CreateContents()
        {
            if (processBar == null)
            {
                imageWindow = new FormDockImageWindow();
                HDevWindowStack.Push(imageWindow.Window.HalconWindow);
                processBar = new FormDockProcessBar();
            }

            toolBox = new FormDockToolBox(this);
        }
        private void MorphologyProcessingFBDForm_Load(object sender, EventArgs e)
        {
            HOperatorSet.OpenWindow(0, 0, (pictureBoxMorphologyProcessing.Width), (pictureBoxMorphologyProcessing.Height), pictureBoxMorphologyProcessing.Handle, "visible", "", out MorphologyProcessingHWHandle);
            HOperatorSet.SetPart(MorphologyProcessingHWHandle, 0, 0, (pictureBoxMorphologyProcessing.Height - 1), (pictureBoxMorphologyProcessing.Width - 1));
            HDevWindowStack.Push(MorphologyProcessingHWHandle);

            //panelenable.Enabled = false;
            oriPictureBoxShowImageWidth  = pictureBoxMorphologyProcessing.Width;
            oriPictureBoxShowImageHeight = pictureBoxMorphologyProcessing.Height;
            MessageBox.Show("注意:该模块输入默认为二值图像,否则将会被强制转化为二值图像!");
        }
예제 #18
0
 private void btn_TwoCameras_Snap_Click(object sender, EventArgs e)
 {
     HDevWindowStack.Push(hv_WindowHandle);
     if (m_Grabber_1 != IntPtr.Zero)
     {
         MvApi.CameraGrabber_StopLive(m_Grabber_1);
     }
     if (m_Grabber_2 != IntPtr.Zero)
     {
         MvApi.CameraGrabber_StopLive(m_Grabber_2);
     }
 }
예제 #19
0
 private void TemplateMatch_Load(object sender, EventArgs e)
 {
     if (HDevWindowStack.IsOpen())
     {
         HOperatorSet.CloseWindow(HDevWindowStack.Pop());
     }
     HOperatorSet.SetWindowAttr("border_width", 0);
     HOperatorSet.SetWindowAttr("background_color", "black");
     HOperatorSet.OpenWindow(0, 0, 1024, 768, TMImage.Handle, "visible", "", out MhvWindowHandle);
     HOperatorSet.SetPart(MhvWindowHandle, 0, 0, 767, 1023);
     HDevWindowStack.Push(MhvWindowHandle);
 }
예제 #20
0
        public void ShowImage(HTuple HDWindow_, string ImageFile_)
        {
            HObject ho_Image;

            HOperatorSet.GenEmptyObj(out ho_Image);
            HDevWindowStack.Push(HDWindow_);
            ho_Image.Dispose();
            HOperatorSet.ReadImage(out ho_Image, ImageFile_);
            if (HDevWindowStack.IsOpen())
            {
                HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive());
            }
        }
예제 #21
0
 private void btn_CurrentCameraPhoto_Click(object sender, EventArgs e)
 {
     HDevWindowStack.Push(hv_WindowHandle);
     if (m_Grabber_1 != IntPtr.Zero || m_Grabber_2 != IntPtr.Zero)
     {
         if (label_CameraNo.Text == "2")
         {
             MvApi.CameraGrabber_StartLive(m_Grabber_2);
         }
         else
         {
             MvApi.CameraGrabber_StartLive(m_Grabber_1);
         }
     }
 }
예제 #22
0
 private void Form1_Load(object sender, EventArgs e)
 {
     //1设置窗口颜色为黑色
     //2打开窗口
     //3设置为活跃窗口
     //4打开相机
     //5设置采集模式为灰度图
     HOperatorSet.SetWindowAttr("background_color", "black");
     HOperatorSet.OpenWindow(0, 0, pictureBox1.Width, pictureBox1.Height, pictureBox1.Handle, "", "", out hv_WindowHandle);
     HDevWindowStack.Push(hv_WindowHandle);
     //Image Acquisition 01: Code generated by Image Acquisition 01
     HOperatorSet.OpenFramegrabber("MindVision12_X64", 1, 1, 0, 0, 0, 0, "progressive",
                                   8, "Gray", -1, "false", "auto", "camera1", 0, -1, out hv_AcqHandle);
     HOperatorSet.GrabImageStart(hv_AcqHandle, -1);
 }
예제 #23
0
        private void SurfaceBasedMatchFBDForm_Load(object sender, EventArgs e)
        {
            try
            {
                HOperatorSet.OpenWindow(0, 0, 256, 192, SurfaceMatchingPicture.Handle, "visible", "", out SBMWindowHandle);
                HDevWindowStack.Push(SBMWindowHandle);

                HOperatorSet.OpenWindow(0, 0, 375, 328, SMPartptx.Handle, "visible", "", out SBMPartWindowHandle);
                HDevWindowStack.Push(SBMPartWindowHandle);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
예제 #24
0
 private void BtnCollection_Click(object sender, RoutedEventArgs e)
 {
     try
     {
         timer.Start();
         timer.IsEnabled = true;
         HOperatorSet.SetWindowAttr("background_color", "black");
         HOperatorSet.OpenWindow(0, 0, HWCCameraImage.Width, HWCCameraImage.Height, HWCCameraImage.HalconWindow, "", "", out hv_WindowHandle);
         HDevWindowStack.Push(hv_WindowHandle);
         Console.WriteLine("开始采集");
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.ToString());
     }
 }
예제 #25
0
    // Local procedures
    // Short Description: open window and set part
    public void open_zoom_window(HTuple hv_RowPos, HTuple hv_ColPos, HTuple hv_RowROI1,
                                 HTuple hv_ColROI1, HTuple hv_RowROI2, HTuple hv_ColROI2, HTuple hv_ZoomFactor,
                                 out HTuple hv_WindowHandleZoom)
    {
        // Initialize local and output iconic variables
        HOperatorSet.SetWindowAttr("background_color", "black");
        HOperatorSet.OpenWindow(hv_RowPos, hv_ColPos, (hv_ColROI2 - hv_ColROI1) * hv_ZoomFactor, (hv_RowROI2 - hv_RowROI1) * hv_ZoomFactor, 0, "visible", "", out hv_WindowHandleZoom);
        HDevWindowStack.Push(hv_WindowHandleZoom);
        if (HDevWindowStack.IsOpen())
        {
            HOperatorSet.SetPart(HDevWindowStack.GetActive(), hv_RowROI1.TupleRound(),
                                 hv_ColROI1.TupleRound(), hv_RowROI2.TupleRound(), hv_ColROI2.TupleRound()
                                 );
        }

        return;
    }
예제 #26
0
 private void Vision_Form_Load(object sender, EventArgs e)
 {
     HOperatorSet.OpenWindow(0, 0, pictureBox1.Width, pictureBox1.Height, pictureBox1.Handle, "", "", out Hwin_tool);
     Hwin = Hwin_tool;
     HDevWindowStack.Push(Hwin);
     HOperatorSet.SetSystem("clip_region", "false");
     HOperatorSet.SetDraw(Hwin, "margin");
     HOperatorSet.SetColor(Hwin, "red");
     try
     {
         HOperatorSet.GetImageSize(image_show, out Hwidth, out Hheight);
         HOperatorSet.SetPart(Hwin, 0, 0, Hheight, Hwidth);
         HOperatorSet.DispObj(image_show, Hwin);
     }
     catch
     {
     }
 }
예제 #27
0
 private void FileInputSet_Load(object sender, EventArgs e)
 {
     HOperatorSet.OpenWindow(0, 0, (pictureBoxImage.Width), (pictureBoxImage.Height), pictureBoxImage.Handle, "visible", "", out FileInputHWHandle);
     HOperatorSet.SetPart(FileInputHWHandle, 0, 0, (pictureBoxImage.Height), (pictureBoxImage.Width));
     HDevWindowStack.Push(FileInputHWHandle);
     oriPictureBoxShowImageWidth  = pictureBoxImage.Width;
     oriPictureBoxShowImageHeight = pictureBoxImage.Height;
     numericUpDownColumn.Value    = ConfigInformation.GetInstance().tFileCfg.columns;
     numericUpDownRow.Value       = ConfigInformation.GetInstance().tFileCfg.rows;
     if (ConfigInformation.GetInstance().tFileCfg.channelNum == 1)
     {
         comboBoxChannel.SelectedIndex = 0;
     }
     else
     {
         comboBoxChannel.SelectedIndex = 1;
     }
 }
예제 #28
0
        private void Form1_Load(object sender, EventArgs e)
        {
            //1打开窗口
            //2枚举设备
            //3初始化设备
            //4.获得位数,申请内存
            //5绑定回调事件  并在回调事件中加入halcon显示代码
            //6开始采集(默认连续,不设置采集模式的话)
            HOperatorSet.OpenWindow(0, 0, pictureBox1.Width, pictureBox1.Height, pictureBox1.Handle, "", "", out hv_WindowHandle);
            HDevWindowStack.Push(hv_WindowHandle);
            status = MvApi.CameraEnumerateDevice(out tCameraDevInfoList);
            if (tCameraDevInfoList != null)//此时iCameraCounts返回了实际连接的相机个数。如果大于1,则初始化第一个相机
            {
                status = MvApi.CameraInit(ref tCameraDevInfoList[0], -1, -1, ref m_hCamera);
            }
            MvApi.CameraGetCapability(m_hCamera, out tCameraCapability);

            m_ImageBuffer = Marshal.AllocHGlobal(tCameraCapability.sResolutionRange.iWidthMax * tCameraCapability.sResolutionRange.iHeightMax * 3 + 1024);

            m_CaptureCallback = new CAMERA_SNAP_PROC(ImageCaptureCallback);
            MvApi.CameraSetCallbackFunction(m_hCamera, m_CaptureCallback, m_iCaptureCallbackCtx, ref pCaptureCallOld);
            MvApi.CameraPlay(m_hCamera);
        }
예제 #29
0
    // Procedures
    // External procedures
    // Chapter: Develop
    // Short Description: Open a new graphics window that preserves the aspect ratio of the given image.
    public void dev_open_window_fit_image(HObject ho_Image, HTuple hv_Row, HTuple hv_Column,
                                          HTuple hv_WidthLimit, HTuple hv_HeightLimit, out HTuple hv_WindowHandle)
    {
        // Local control variables

        HTuple hv_MinWidth = new HTuple(), hv_MaxWidth = new HTuple();
        HTuple hv_MinHeight = new HTuple(), hv_MaxHeight = new HTuple();
        HTuple hv_ResizeFactor, hv_ImageWidth, hv_ImageHeight;
        HTuple hv_TempWidth, hv_TempHeight, hv_WindowWidth, hv_WindowHeight;

        // Initialize local and output iconic variables

        //This procedure opens a new graphics window and adjusts the size
        //such that it fits into the limits specified by WidthLimit
        //and HeightLimit, but also maintains the correct image aspect ratio.
        //
        //If it is impossible to match the minimum and maximum extent requirements
        //at the same time (f.e. if the image is very long but narrow),
        //the maximum value gets a higher priority,
        //
        //Parse input tuple WidthLimit
        if ((int)((new HTuple((new HTuple(hv_WidthLimit.TupleLength())).TupleEqual(0))).TupleOr(
                      new HTuple(hv_WidthLimit.TupleLess(0)))) != 0)
        {
            hv_MinWidth = 500;
            hv_MaxWidth = 800;
        }
        else if ((int)(new HTuple((new HTuple(hv_WidthLimit.TupleLength())).TupleEqual(
                                      1))) != 0)
        {
            hv_MinWidth = 0;
            hv_MaxWidth = hv_WidthLimit.Clone();
        }
        else
        {
            hv_MinWidth = hv_WidthLimit[0];
            hv_MaxWidth = hv_WidthLimit[1];
        }
        //Parse input tuple HeightLimit
        if ((int)((new HTuple((new HTuple(hv_HeightLimit.TupleLength())).TupleEqual(0))).TupleOr(
                      new HTuple(hv_HeightLimit.TupleLess(0)))) != 0)
        {
            hv_MinHeight = 400;
            hv_MaxHeight = 600;
        }
        else if ((int)(new HTuple((new HTuple(hv_HeightLimit.TupleLength())).TupleEqual(
                                      1))) != 0)
        {
            hv_MinHeight = 0;
            hv_MaxHeight = hv_HeightLimit.Clone();
        }
        else
        {
            hv_MinHeight = hv_HeightLimit[0];
            hv_MaxHeight = hv_HeightLimit[1];
        }
        //
        //Test, if window size has to be changed.
        hv_ResizeFactor = 1;
        HOperatorSet.GetImageSize(ho_Image, out hv_ImageWidth, out hv_ImageHeight);
        //First, expand window to the minimum extents (if necessary).
        if ((int)((new HTuple(hv_MinWidth.TupleGreater(hv_ImageWidth))).TupleOr(new HTuple(hv_MinHeight.TupleGreater(
                                                                                               hv_ImageHeight)))) != 0)
        {
            hv_ResizeFactor = (((((hv_MinWidth.TupleReal()) / hv_ImageWidth)).TupleConcat(
                                    (hv_MinHeight.TupleReal()) / hv_ImageHeight))).TupleMax();
        }
        hv_TempWidth  = hv_ImageWidth * hv_ResizeFactor;
        hv_TempHeight = hv_ImageHeight * hv_ResizeFactor;
        //Then, shrink window to maximum extents (if necessary).
        if ((int)((new HTuple(hv_MaxWidth.TupleLess(hv_TempWidth))).TupleOr(new HTuple(hv_MaxHeight.TupleLess(
                                                                                           hv_TempHeight)))) != 0)
        {
            hv_ResizeFactor = hv_ResizeFactor * ((((((hv_MaxWidth.TupleReal()) / hv_TempWidth)).TupleConcat(
                                                       (hv_MaxHeight.TupleReal()) / hv_TempHeight))).TupleMin());
        }
        hv_WindowWidth  = hv_ImageWidth * hv_ResizeFactor;
        hv_WindowHeight = hv_ImageHeight * hv_ResizeFactor;
        //Resize window
        HOperatorSet.SetWindowAttr("background_color", "black");
        HOperatorSet.OpenWindow(hv_Row, hv_Column, hv_WindowWidth, hv_WindowHeight, 0, "", "", out hv_WindowHandle);
        HDevWindowStack.Push(hv_WindowHandle);
        if (HDevWindowStack.IsOpen())
        {
            HOperatorSet.SetPart(HDevWindowStack.GetActive(), 0, 0, hv_ImageHeight - 1, hv_ImageWidth - 1);
        }

        return;
    }
예제 #30
0
    // Main procedure
    private void action()
    {
        // Local iconic variables

        HObject ho_Image, ho_DarkRegion = null, ho_CharRegion = null;
        HObject ho_CharConnected = null, ho_CharOrig = null, ho_CharSel = null;
        HObject ho_Characters = null, ho_CharRectangle = null, ho_CharROI = null;
        HObject ho_ImageReduced = null, ho_ImageScaled = null, ho_CharConvex = null;
        HObject ho_SingleChar = null;


        // Local control variables

        HTuple hv_OCRFontName, hv_FontName, hv_FontImages;
        HTuple hv_NumImages, hv_Width, hv_Height, hv_WindowID;
        HTuple hv_OCRHandle, hv_WidthCharacter, hv_HeightCharacter;
        HTuple hv_Interpolation, hv_Features, hv_Char, hv_NumHidden;
        HTuple hv_Preprocessing, hv_NumComponents, hv_NumChar;
        HTuple hv_NumSamples, hv_i, hv_Min = new HTuple(), hv_Max = new HTuple();
        HTuple hv_Range = new HTuple(), hv_WindowChar = new HTuple();
        HTuple hv_DoLoop = new HTuple(), hv_Row = new HTuple(), hv_Column = new HTuple();
        HTuple hv_Button = new HTuple(), hv_Index = new HTuple(), hv_RecChar = new HTuple();
        HTuple hv_Confidence = new HTuple();

        // Initialize local and output iconic variables
        HOperatorSet.GenEmptyObj(out ho_Image);
        HOperatorSet.GenEmptyObj(out ho_DarkRegion);
        HOperatorSet.GenEmptyObj(out ho_CharRegion);
        HOperatorSet.GenEmptyObj(out ho_CharConnected);
        HOperatorSet.GenEmptyObj(out ho_CharOrig);
        HOperatorSet.GenEmptyObj(out ho_CharSel);
        HOperatorSet.GenEmptyObj(out ho_Characters);
        HOperatorSet.GenEmptyObj(out ho_CharRectangle);
        HOperatorSet.GenEmptyObj(out ho_CharROI);
        HOperatorSet.GenEmptyObj(out ho_ImageReduced);
        HOperatorSet.GenEmptyObj(out ho_ImageScaled);
        HOperatorSet.GenEmptyObj(out ho_CharConvex);
        HOperatorSet.GenEmptyObj(out ho_SingleChar);

        try
        {
            //
            //OCR
            //
            // dev_update_window(...); only in hdevelop
            //Specify the name of the font to use for reading the date on the bottle.
            //It is easiest to use the pre-trained font Document.  If you have run the
            //program fontt.hdev in this directory, you can activate the second line
            //to use the font trained with this program.
            hv_OCRFontName = "Document";
            //OCRFontName := 'arial'
            hv_FontName      = "arial";
            hv_FontImages    = new HTuple();
            hv_FontImages[0] = "a1";
            hv_FontImages[1] = "a2";
            hv_FontImages[2] = "a3";
            hv_FontImages[3] = "a4";
            hv_FontImages[4] = "a5";
            hv_FontImages[5] = "a6";
            hv_FontImages[6] = "a7";
            hv_FontImages[7] = "a8";
            hv_FontImages[8] = "a9";
            hv_NumImages     = new HTuple(hv_FontImages.TupleLength());
            ho_Image.Dispose();
            HOperatorSet.ReadImage(out ho_Image, (("fonts/" + hv_FontName) + "_") + (hv_FontImages.TupleSelect(
                                                                                         0)));
            if (HDevWindowStack.IsOpen())
            {
                HOperatorSet.CloseWindow(HDevWindowStack.Pop());
            }
            HOperatorSet.GetImageSize(ho_Image, out hv_Width, out hv_Height);
            HOperatorSet.SetWindowAttr("background_color", "black");
            HOperatorSet.OpenWindow(0, 0, hv_Width / 2, hv_Height / 2, 0, "", "", out hv_WindowID);
            HDevWindowStack.Push(hv_WindowID);
            set_display_font(hv_WindowID, 14, "mono", "true", "false");
            if (HDevWindowStack.IsOpen())
            {
                HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive());
            }
            disp_continue_message(hv_WindowID, "black", "true");
            //stop ()
            //---------- read OCR classifier ----------
            HOperatorSet.ReadOcrClassMlp(hv_OCRFontName, out hv_OCRHandle);
            HOperatorSet.GetParamsOcrClassMlp(hv_OCRHandle, out hv_WidthCharacter, out hv_HeightCharacter,
                                              out hv_Interpolation, out hv_Features, out hv_Char, out hv_NumHidden, out hv_Preprocessing,
                                              out hv_NumComponents);
            hv_NumChar = new HTuple(hv_Char.TupleLength());
            disp_continue_message(hv_WindowID, "black", "true");
            HDevelopStop();
            //---------- segmentation of characters ----------
            if (HDevWindowStack.IsOpen())
            {
                HOperatorSet.SetDraw(HDevWindowStack.GetActive(), "margin");
            }
            if (HDevWindowStack.IsOpen())
            {
                HOperatorSet.SetShape(HDevWindowStack.GetActive(), "rectangle1");
            }
            hv_NumSamples = 0;
            for (hv_i = 1; hv_i.Continue(hv_NumImages, 1); hv_i = hv_i.TupleAdd(1))
            {
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.SetColored(HDevWindowStack.GetActive(), 12);
                }
                ho_Image.Dispose();
                HOperatorSet.ReadImage(out ho_Image, (("fonts/" + hv_FontName) + "_") + (hv_FontImages.TupleSelect(
                                                                                             hv_i - 1)));
                ho_DarkRegion.Dispose();
                HOperatorSet.BinThreshold(ho_Image, out ho_DarkRegion);
                ho_CharRegion.Dispose();
                HOperatorSet.ClosingCircle(ho_DarkRegion, out ho_CharRegion, 8.5);
                ho_CharConnected.Dispose();
                HOperatorSet.Connection(ho_CharRegion, out ho_CharConnected);
                ho_CharOrig.Dispose();
                HOperatorSet.Intersection(ho_CharConnected, ho_DarkRegion, out ho_CharOrig
                                          );
                ho_CharSel.Dispose();
                HOperatorSet.SelectShape(ho_CharOrig, out ho_CharSel, "area", "and", 50,
                                         99999);
                ho_Characters.Dispose();
                HOperatorSet.SortRegion(ho_CharSel, out ho_Characters, "character", "true",
                                        "row");
                //adapt to different brigthness
                ho_CharRectangle.Dispose();
                HOperatorSet.ShapeTrans(ho_CharRegion, out ho_CharRectangle, "rectangle1");
                ho_CharROI.Dispose();
                HOperatorSet.DilationRectangle1(ho_CharRectangle, out ho_CharROI, 10, 10);
                ho_ImageReduced.Dispose();
                HOperatorSet.ReduceDomain(ho_Image, ho_CharROI, out ho_ImageReduced);
                HOperatorSet.MinMaxGray(ho_CharROI, ho_Image, 5, out hv_Min, out hv_Max,
                                        out hv_Range);
                ho_ImageScaled.Dispose();
                HOperatorSet.ScaleImage(ho_ImageReduced, out ho_ImageScaled, 255 / hv_Range,
                                        ((-hv_Min) * 255) / hv_Range);
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive());
                }
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.SetColor(HDevWindowStack.GetActive(), "green");
                }
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.DispObj(ho_Characters, HDevWindowStack.GetActive());
                }
                disp_continue_message(hv_WindowID, "black", "true");
                HDevelopStop();
                //interactive OCR
                HOperatorSet.SetWindowAttr("background_color", "black");
                HOperatorSet.OpenWindow(0, (hv_Width / 2) + 50, 128, 128, 0, "", "", out hv_WindowChar);
                HDevWindowStack.Push(hv_WindowChar);
                set_display_font(hv_WindowChar, 27, "mono", "true", "false");
                HDevWindowStack.SetActive(hv_WindowID);
                ho_CharConvex.Dispose();
                HOperatorSet.ShapeTrans(ho_Characters, out ho_CharConvex, "rectangle1");
                if (HDevWindowStack.IsOpen())
                {
                    HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive());
                }
                hv_DoLoop = 1;
                while ((int)(new HTuple(hv_DoLoop.TupleEqual(1))) != 0)
                {
                    if (HDevWindowStack.IsOpen())
                    {
                        HOperatorSet.SetColor(HDevWindowStack.GetActive(), "red");
                    }
                    disp_message(hv_WindowID, "Left-click on a character to read; right mouse button to proceed to next image",
                                 "image", 8, 10, "blue", "false");
                    HOperatorSet.GetMbutton(hv_WindowID, out hv_Row, out hv_Column, out hv_Button);
                    if (HDevWindowStack.IsOpen())
                    {
                        HOperatorSet.DispObj(ho_Image, HDevWindowStack.GetActive());
                    }
                    HOperatorSet.GetRegionIndex(ho_CharConvex, hv_Row, hv_Column, out hv_Index);
                    if ((int)(new HTuple(hv_Index.TupleGreater(0))) != 0)
                    {
                        ho_SingleChar.Dispose();
                        HOperatorSet.SelectObj(ho_Characters, out ho_SingleChar, hv_Index);
                        HOperatorSet.DoOcrSingleClassMlp(ho_SingleChar, ho_ImageScaled, hv_OCRHandle,
                                                         1, out hv_RecChar, out hv_Confidence);
                        HDevWindowStack.SetActive(hv_WindowChar);
                        if (HDevWindowStack.IsOpen())
                        {
                            HOperatorSet.SetPart(HDevWindowStack.GetActive(), 0, 0, 127, 127);
                        }
                        if (HDevWindowStack.IsOpen())
                        {
                            HOperatorSet.ClearWindow(HDevWindowStack.GetActive());
                        }
                        disp_message(hv_WindowChar, hv_RecChar.TupleSelect(0), "image", 40, 53,
                                     "green", "false");
                        HDevWindowStack.SetActive(hv_WindowID);
                        if (HDevWindowStack.IsOpen())
                        {
                            HOperatorSet.SetPart(HDevWindowStack.GetActive(), 0, 0, hv_Height - 1,
                                                 hv_Width - 1);
                        }
                    }
                    if ((int)(new HTuple(hv_Button.TupleEqual(4))) != 0)
                    {
                        hv_DoLoop = 0;
                        HDevWindowStack.SetActive(hv_WindowChar);
                        if (HDevWindowStack.IsOpen())
                        {
                            HOperatorSet.CloseWindow(HDevWindowStack.Pop());
                        }
                        HDevWindowStack.SetActive(hv_WindowID);
                    }
                }
            }
            HOperatorSet.ClearOcrClassMlp(hv_OCRHandle);
            if (HDevWindowStack.IsOpen())
            {
                HOperatorSet.SetShape(HDevWindowStack.GetActive(), "original");
            }
            // dev_update_window(...); only in hdevelop
        }
        catch (HalconException HDevExpDefaultException)
        {
            ho_Image.Dispose();
            ho_DarkRegion.Dispose();
            ho_CharRegion.Dispose();
            ho_CharConnected.Dispose();
            ho_CharOrig.Dispose();
            ho_CharSel.Dispose();
            ho_Characters.Dispose();
            ho_CharRectangle.Dispose();
            ho_CharROI.Dispose();
            ho_ImageReduced.Dispose();
            ho_ImageScaled.Dispose();
            ho_CharConvex.Dispose();
            ho_SingleChar.Dispose();

            throw HDevExpDefaultException;
        }
        ho_Image.Dispose();
        ho_DarkRegion.Dispose();
        ho_CharRegion.Dispose();
        ho_CharConnected.Dispose();
        ho_CharOrig.Dispose();
        ho_CharSel.Dispose();
        ho_Characters.Dispose();
        ho_CharRectangle.Dispose();
        ho_CharROI.Dispose();
        ho_ImageReduced.Dispose();
        ho_ImageScaled.Dispose();
        ho_CharConvex.Dispose();
        ho_SingleChar.Dispose();
    }