Integrate Halcon Script: Data matrix recognition¶
The task Execute Halcon Script allows you to integrate your own Halcon scripts (Hdev scripts) into a Mikado| program sequence. The following tutorial uses an example to explain the steps required to implement your own recognition of a data matrix or a QR code using the Halcon library. The image data is read from one of the two mono cameras from the stereo image of the Ensenso camera.
In a practical application, such a function is helpful, for example, to trigger a product change via the barcode. The following barcodes were used:
Datamatrix code for the article '12345678'¶
Datamatrix code for the article '98765432'¶
QR code Mikado (content is a link to the Mikado website)¶
Preparation¶
As a product change is to take place using the barcode, you first need a suitable product list. In this example, the products start with the prefix Product_ followed by the article number, which is read in by the barcode. How to set up products is explained in the Product management chapter.
Sample product list¶
You will also need a camera configuration for your Ensenso camera that is suitable for capturing a mono image (in particular, you should deactivate the projector and adjust the exposure time for good contrast). Finally, save the configuration file in the ‘config’ directory of your robot cell, e.g. under ‘barcodeDetection.json’.
Hint
Explanations of the directories can be found under Directory structure.
Program schedule¶
In the first step, the program sequence is created as shown using the program editor.
Program editor: Program sequence for the integration of an Hdev script¶
- Startsequenz: 'Barcode Detection'Is executed once at program start and stops at the end of the sequence.
- Changes the configuration for the Ensenso camera. The configuration
barcodeDetection.jsonis optimized for 2D image acquisition. In this case, the projector is deactivated and the exposure time is adjusted for good contrasts. - Triggers the camera for a recording. In the following steps, no 3D point cloud is evaluated, but this task also triggers the recordings of the mono cameras.
- The actual barcode recognition takes place in this step. The HDev script
mikado_barcode_detection.hdevis executed and can access image data from the Ensenso. The script itself is created in the next step. - The HDev script for barcode recognition will access the Mikado variables and write recognized barcodes as StringList to the variable
dataCodeResultList. If the list remains empty, no barcode has been recognized. In this case, the process jumps to the second sequence calledDetection failedand ends here in the error state. If the list contains at least one entry, there is no sequence change and the process continues with the next task. - Copies the first entry of the
dataCodeResultListlist into thedetectedProductNrvariable. This means that only the first recognized barcode is evaluated. - Executes the product change. Note: If the product does not exist, an error is output at this point and the program execution is aborted.
- Stop program.
- Sequenz: 'Detection failed'’Is executed if the barcode recognition does not find any barcodes.
- Stops the program in the error state.
Create Hdev script for the recognition of data matrices / QR codes¶
The next step is to create the Halcon script, which is used to recognize the various barcodes.
Hint
All Mikado-specific halcon operators are explained in chapter Halcon Script Task.
To do this, open HDevelop and create a new HDev script. Save the file under the name mikado_barcode_detection.hdev in the scripts folder of the robot cell. At the beginning of the program, the rectified mono image of the Ensenso’s right camera is read in and stored in the variable Image2D (Z. 4).
1* Read right image from last capture of Ensenso camera and
2* detect data matrix codes / QR codes in image.
3
4mikado_get_2d_image(Image2D, 'Ensenso_Camera_0', 'right', 'rectified')
Then use the operator find_data_code_2d to search the image for data matrices (lines 5 - 7). The standard parameterization serves its purpose here, but can of course also be optimized through training. Information on this topic can be found in the Halcon documentation.
As the products in the example product list begin with the prefix Product_, the decoded article numbers are extended accordingly (lines 11 - 13)
6create_data_code_2d_model('Data Matrix ECC 200', [], [], DataCodeHandle)
7find_data_code_2d(Image2D, dataMatrixXLDs, DataCodeHandle, [], [], resultHandles, decodedDataStrings)
8clear_data_code_2d_model(DataCodeHandle)
9
10* Prepend 'Product_' to article number (as this is our Mikado product name)
11for Index := 0 to |decodedDataStrings|-1 by 1
12 decodedDataStrings[Index] := 'Product_' + decodedDataStrings[Index]
13endfor
Since the script should also recognize and decode QR codes, we start the search again with the corresponding parameterization (lines 15 - 17).
15create_data_code_2d_model('QR Code', [], [], DataCodeHandle)
16find_data_code_2d(Image2D, qrXLDs, DataCodeHandle, [], [], resultHandles, decodedQrStrings)
17clear_data_code_2d_model(DataCodeHandle)
After the results are assembled (line 19), the decoded information is passed to Mikado as a list of strings (line 20). When creating the Mikado program flow in the step above, the variable ‘dataCodeResultList’ was created for this purpose.
19decodedStrings := [decodedDataStrings, decodedQrStrings]
20mikado_write_variable('dataCodeResultList', 'StringList', decodedStrings, [])
As the last part of the script, you can pass an image to Mikado that is to display results or analysis information, for example. In this case, you use a series of operators to draw the recognized outlines of the barcodes in colour in the image (from line 24). Data matrices are framed in green, QR codes in orange.
23* Publish an image with results
24get_image_size(Image2D, Width, Height)
25open_window(0, 0, Width, Height, 0, 'buffer', '', WindowHandle)
26disp_image(Image2D, WindowHandle)
27set_line_width(WindowHandle, 5)
28set_color(WindowHandle, 'green')
29disp_xld(dataMatrixXLDs, WindowHandle)
30set_color(WindowHandle, 'orange')
31disp_xld(qrXLDs, WindowHandle)
32dump_window_image(OutputImage, WindowHandle)
33mikado_publish_2d_image(OutputImage)
The following screenshot shows the entire script as a screenshot from the HDevelop environment.
Hdev-Script ‘mikado_barcode_detection.hdev’¶
Hint
Some Halcon operators at the end of the script are marked with a red warning symbol. The warning relates to the execution of the script within the HDevelop environment. However, as the script is later executed in a separate Hdev engine, these warnings can be ignored.
Tip
If you run HDevelop directly on the Mikado controller, you can also test the script directly in HDevelop. You also have the full support of the development environment for the Mikado operators. Simply start Mikado in parallel to HDevelop. Both the Mikado program flow and the individual Halcon operators can now be executed step by step. This helps you to develop and test the HDev script.
Execute and test program¶
The result can be seen in the following screenshots. If the process is executed while the camera is pointed at one of the two data matrices, the active product is successfully changed to the coded article number.
If you would like to display the corresponding image with the results drawn in, switch to the ‘Camera’ tab at the bottom right before starting the program. There, select the entry ‘/mikado_hdevengine_node/halcon_picture’ in the combo box at the top under ‘Show Image:’. Now, after executing your Hdev script, the captured image is displayed and decoded barcodes are framed in color.
Status view when using the data matrix of the article '12345678'¶
Status view when using the data matrix of the article '98765432'¶
If the camera is pointed at the QR code, the decoded information does not contain an article number, but a URL to the Mikado website. As there is obviously no product with this name, the program sequence ends in an error as shown in the screenshot.
Status view when using the Mikado QR code¶