5.4.7   HCrImage

HCrImage (see figure 5.19) is used for creating output images especially in the context of filter16) operations. As you know, HALCON image objects can share the underlying image matrices (gray value channels). Thus, input image objects of a filter operation might only differ in their domain (i.e., in their region component), but not in their gray values. Such objects have different database keys and different regions, but contain references to the same gray value channels. Obviously, it is very desirable to propagate this relation to the resulting output image objects as well.

HCrImage allocates memory for the image matrix within the Himage structure image_out (similar to HNewImage in section “Permanent Data”). The parameters type (BYTE_IMAGE, LONG_IMAGE, FLOAT_IMAGE, etc.; see figure 4.2, width and height allow to specify the pixel type and the size of the new matrix. Note that new matrices are by default initialized with 0. However, this can be suppressed from outside or inside of the operator, see the discussion of HNewImage in section “Permanent Data” for details.

Furthermore, HCrImage creates a new database object encapsulating this image data and returns the corresponding database key in image_key_out (similar to HPutImage in section “HPutImage”).

  Hkey       obj_key,image_key_in,image_key_out,key_out;
  Hrlregion  *region;
  Himage     image_in,image_out;

  ...
  HGetSPar(proc_handle,1,LONG_PAR,&Rows,1); /* height: filter mask */
  HGetSPar(proc_handle,2,LONG_PAR,&Cols,1); /* width:  filter mask */

  HAllObj(proc_handle,1,obj_key,i) {         /* all input images   */
    HGetFDRL(proc_handle,obj_key,&region);   /* get region data    */
    HGetComp(proc_handle,obj_key,IMAGE1,&image_key_in);
    if (image_key_in == UNDEFINED)
      return(H_ERR_UNDI);                    /* 'undefined image'  */
    HGetImage(proc_handle,image_key_in,&image_in); /*get image data*/
    HCkP(HCrImage(proc_handle,image_key_in,1,image_in.kind,
                  image_in.width,image_in.height,
                  &image_key_out,&image_out));

    HCkP(IPFilterOperation(&image_in,region,Rows,Cols,&image_out));

    HCopyObj(proc_handle,obj_key,1,&key_out); /* create result obj.*/
    HDefObj(proc_handle,key_out,image_key_out,IMAGE1);
                       /* insert the result image in output object */
  }

Figure 5.23: A typical example for using HCrImage: Providing an empty image matrix for a filter operation.

Finally, HCrImage establishes a link between the original input image data (referenced by the database key image_key_in) and this new iconic object to avoid the allocation of multiple output matrices per input matrix. HCrImage checks, whether there already exists an output matrix with the specified index index (see below) for a given input image image_key_in. If there is any, only a reference to it is returned in image_out and image_key_out instead of creating a new matrix. Without this mechanism, a new matrix would be created again and again within the loop over all input iconic objects (HAllObj).

The parameter index is only of importance, if more than one output matrix per input matrix is needed: This is necessary for example, if an edge filter is performed in x- and y-direction independently. In this case, HCrImage must be called several times for every input component – one time for each output matrix to be created. Assign increasing indices (1, 2, 3, ...) to the parameter index to indicate a new output matrix. If there is only one matrix to create for the output image, set index to 1.

Figure 5.23 shows an exemplary application of HCrImage.

Note that HCrImage is no macro, i.e., it does not check the result state of the underlying procedure using HCkP (see section “HCkP”). Thus, you must call HCkP on HCrImage yourself as shown in the example.

16) This is due to the fact, that filtering only modifies the gray values but not the regions of image objects.