[Common problems when halcon is programmed with C#]

Common problems when halcon is programmed with C#
Software environment
halcon17+VS2017
1. Configuration or reference problems
If you don’t know how to convert halcon codes to C# projects, you can first refer to halcon and C# mixed programming
1. Remember to choose to use when converting halcon codes to C# Export templates, so that there will be fewer problems when referencing C# projects.
insert image description here
2. When the C# project is configured, the "Target Framework" is changed to ".NET Framework 4". When it is set to a higher version, it is easy to report an error when compiling or running for the first time, but the error will no longer appear when the compilation is successful and then changed back to a higher version. The specific reason is not yet clear.

2. Interaction problem
1. Display of Halcon graphic elements
hWindowControl1.HalconWindow.DispObj(image);//Display image, Region,

XldhWindowControl1.HalconWindow.SetDraw("margin");//Set the filling mode

hWindowControl1.HalconWindow.SetColor("green");//Set the display color

2. System global parameter setting (not necessary, but recommended)

HOperatorSet.SetSystem("do_low_error", "false");//Less error reporting

HOperatorSet.SetSystem("clip_region", "false");//region is not cut off outside the image

HOperatorSet.SetSystem("border_shape_models", "true");// still match the shape of the edge

3. Debugging
the code of Halcon in C# is not very convenient, but it is not impossible.

① When debugging, the variable of type HTuple can directly view the value.

② Variables of Image, Region, and Xld can only be checked by "Save the file locally".

HOperatorSet.WriteImage(image, “jpg”, 0x000000, “1.jpg”);//color image

HOperatorSet.WriteImage(image, “jpg”, 0, “1.jpg”);//grayscale image

HOperatorSet.WriteRegion(ho_RegionUnion, “1.hobj”);//The suffix can also be .reg

HOperatorSet.WriteContourXldDxf(contours,“1.dxf”);

③ Install Halcon's variable checking plug-in to Visual Studio. The default path where the installation file is located is:

C:\Program Files\MVTec\HALCON-12.0\misc\HALCON_Variable_Inspect.vsix

4. Display text in hWindowControl control

The contents of the following two functions are exported by the set_display_font and disp_message operators in Halcon, which can complete the text display in the control.

set_display_font(windowHandle, hv_TextSize, “mono”, “true”, “false”);

disp_message(windowHandle, hv_Text, “window”, row, col, hv_Color,“true”);

5. Format conversion

① HTuple is compatible with int, double and other data types

② The image of HObject type is compatible with the image of HImage type

③ For the method of converting Bitmap to Himage (24-bit or 8-bit), see the end of the article:

6. Scale the image in the hWindowControl control

In hWindowControl, the mouse position can be obtained by GetMposition, and SetPart can change the part of the image displayed in the window.

HOperatorSet.GetMposition(hWHandle, out mouseY, out mouseX, out mbutton);

HOperatorSet.SetPart(hWHandle, mRow1, mCol1, mRow2, mCol2);

set_part: Used to modify the part of the image displayed in the window. (Row1, Column1) indicates the upper left corner of the image part to be displayed, and (Row2, Column2) indicates the lower right corner of the image part to be displayed. (See the help documentation of dev_set_part and set_part for details)

9. Interaction between Halcon graphic elements and other image algorithm library data

Halcon's Region and Xld are its own unique formats, which are not common to the outside world. At present, what I have researched is mainly to interact with the outside world by using coordinate points as bridges.

Region is converted to coordinate points: get_region_polygon(Region : : Tolerance : Rows, Columns)

Convert coordinate points to Region: gen_region_polygon_filled( : Region : Rows, Columns : )

Convert Xld to coordinate points: get_contour_xld(Contour : : : Row, Col)

Convert coordinate points to Xld: gen_contour_polygon_xld ( : Contour : Row, Col : )
—————————————————————————————————————————————————————————————————————————————————————————————————————————————————————— .csdn.net/the_future_way/article/details/108521374

Bitmap to Himage (24-bit) method

public static HImage Bitmap2HImage_24(Bitmap bImage)
{
    
    
    Bitmap bImage24;
    BitmapData bmData = null;
    Rectangle rect;
    IntPtr pBitmap;
    IntPtr pPixels;
    HImage hImage = new HImage();
    rect = new Rectangle(0, 0, bImage.Width, bImage.Height);
    bImage24 = new Bitmap(bImage.Width, bImage.Height, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
    System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bImage24);
    g.DrawImage(bImage, rect);
    g.Dispose();
    bmData = bImage24.LockBits(rect, ImageLockMode.ReadOnly, System.Drawing.Imaging.PixelFormat.Format24bppRgb);
    pBitmap = bmData.Scan0;
    pPixels = pBitmap;
    hImage.GenImageInterleaved(pPixels, "bgr", bImage.Width, bImage.Height, -1, "byte", 0, 0, 0, 0, -1, 0);
    bImage24.UnlockBits(bmData);

    return hImage;
}

Bitmap to Himage (8-bit) method

public static HImage Bitmap2HImage_8(Bitmap bImage)
{
    
    
    Bitmap bImage8;
    BitmapData bmData = null;
    Rectangle rect;
    IntPtr pBitmap;
    IntPtr pPixels;
    var hImage = new HImage();
    rect = new Rectangle(0, 0, bImage.Width, bImage.Height);
    bmData = bImage.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
    pBitmap = bmData.Scan0;
    pPixels = pBitmap;
    hImage.GenImage1("byte", bImage.Width, bImage.Height, pPixels);
    bImage.UnlockBits(bmData);
    //formathimage = hImage;
    return hImage;
}

Guess you like

Origin blog.csdn.net/wr9zgo48/article/details/123821008