示例#1
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();
    }