Halcon custom function packaging method (the most detailed in the entire network)


Some netizens said that they are not very clear about the encapsulation method of this halcon function. Write a tutorial post today, let everyone progress and share.

1. Noun explanation

Operator:
Refers to the most basic and lowest-level function in Halcon (that is, you can't see its code implementation). An operator has only one sentence, such as the threshold operator.

Function: A function
composed of multiple operators. Its characteristic is that it can be opened by right-clicking the "Display Function". As shown below:

Insert picture description here
Insert picture description here
Generally, in the Halcon code program window, the code colors of operators and small functions are different. As shown below:
Insert picture description here

2. Introduction

1. Process the original image and tasks:

The radius of the outer circle of the bright area is required.
Insert picture description here

2. Code and analysis:

* 读入图片
read_image (Image, 'C:/Users/Administrator/Desktop/1.jpg')
* 根据图片打开窗口
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
* 显示图片,用于预览
dev_display (Image)

* 二值化
binary_threshold (Image, Region, 'max_separability', 'light', UsedThreshold)
* 分割连通域
connection (Region, ConnectedRegions)
* 使用特征筛选去掉小面积区域的干扰
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 5000, 1000000000)
* 合并区域,为下一步求取最小外接矩形做准备
union1 (SelectedRegions, RegionUnion)
* 求取最小外接矩形
smallest_circle (RegionUnion, Row, Column, Radius)

* 生成一个圆形,用于显示
gen_circle (Circle, Row, Column, Radius)

*显示结果前的准备
dev_clear_window ()
dev_set_draw ('margin')
dev_set_line_width (3)
dev_set_color ('red')

* 显示相关结果
dev_display (Image)
dev_display (Circle)
disp_message (WindowHandle, '圆环半径: '+Radius, 'window', 50, 50, 'black', 'true')

3. Halcon function packaging method

Halcon's function creation is divided into the following steps:
1. Identify the requirements and what functions to be encapsulated.
2. Select the function part to create the function and change the function interface, that is, change the input and output variables.
3. Run verification and function change operations

①Clarify needs

Let’s take the above program as an example. The main task that our program accomplishes is to find the radius of the circumcircle and display it

We look at the code is divided into three parts:
1. Preparation stage: read in the picture, open the window, preview and other tasks.
2. Processing stage: image processing, obtain the result
3. Display stage: display the result
as shown in the figure below:
Insert picture description here
Clear 1: We need to encapsulate the second part of the "processing stage operator into a function"

At this point, we look at the code in the processing stage. From the analysis, we can see:
1. This code requires a picture for image processing
2. The result of the circumcircle of the ring (the position and radius of the circumcircle) is
determined by this :
Input an image variable, and output the coordinates (X, Y) of the circumcircle and the radius of the circumcircle
. So far, the requirements have been clarified.

②Select the function part to create the function and change the function interface

Select the processing stage function, right-click, and click "Create New Function". As shown in the figure below: The
Insert picture description here
following page is displayed as follows:
Insert picture description here
first click on the general setting bar, edit the function name (you can name this function whatever you want), and then click "parameters".
After that, the displayed page is as follows: The
Insert picture description here
meaning of icon parameters: that is, graphic variables such as pictures, areas, and Xld outlines.
Variable parameter meaning: refers to the integer, floating-point, string, array and other variables that we use in the program

We can click the Add Variable button to add input and output variables.
If we want to delete a variable, the operation method is as follows:
first select the variable and click remove. After
Insert picture description here
clicking remove:
Insert picture description here
you can also select the function and click "up", "Move down" changes the order of variables.

We know from the requirements analysis step that we need to:
pass in an image variable, and send out the coordinates (X, Y) of the circumscribed circle and the radius of the circumscribed circle.
Modify the parameters of the page variables as follows, and click the general document to switch the column:
Insert picture description here
general document column: ( Descriptive documents can be written for functions) including function descriptions, example programs, parameter descriptions, and advanced precautions. We generally do not fill in.
Insert picture description here
Click on the parameter document in the figure above: the
parameter document contains the detailed parameter meaning of this function: as shown in the figure below:
Insert picture description here
if we fill in the general document and the parameter document, what will happen?
Your function has a description document like the halcon operator:
that is, select an operator, click F1 to appear the operator help document!
Insert picture description here
Insert picture description here
Complete all the above steps and click the OK button to generate the function
Insert picture description here
Insert picture description here

③Run verification and function change operation

Click F5 to run the program or click F6 to run the program in a single step to verify the accuracy of the program.
When F6 is single-stepped to the function, you can click F7 to run to the internal execution of the function. Click F8 to exit the function execution

If you want to view the function code, select the function, click the right mouse button, and select the display function to view the code:
Insert picture description here
you can click the back button in the red box to exit the current function display.
As shown in the figure below:
click the button in the blue box to open the function editing window (That is, the window where we modify the function name and interface parameter changes above)
Insert picture description here

  • About the blogger:
  • Industrial automation upper computer software engineer, machine vision algorithm engineer, motion control algorithm engineer. Currently working in the intelligent manufacturing automation industry. Blogger's mailbox: [email protected]
  • Help me like it. Haha.

Guess you like

Origin blog.csdn.net/cashmood/article/details/105195235