5.1.4   HGetImage

HGetImage (see figure 5.1) reads the image data (type: Himage, see section “Pixel Data (Himage)”) of a specific gray value component (i.e., channel) of an image object referenced by its database key (type Hkey) in the HALCON database.

Note that HGetImage automatically checks the result state of the underlying procedure with the macro HCkP (see section “HCkP”). Thus, you must not use HCkP on HGetImage yourself.

    Hkey       obj_key;
    Hkey       image_key;
    Hkey       region_key;
    Himage     image;
    Hrlregion  *region;
    ...
    HGetComp(proc_handle,obj_key,IMAGE_INDEX,&image_key);
    HGetComp(proc_handle,obj_key,REGION,&region_key);
    HGetImage(proc_handle,image_key,&image);
    HGetRL(proc_handle,region_key,region);
    switch (image.kind) {
      case BYTE_IMAGE:
        /* processing (in general this should   */
        /* be done calling an action procedure) */
        area = sum = 0;
        for (c=0; c<region->num; c++) {
          for (i=CB(region->rl,c,image.width);
               i<=CE(region->rl,c,image.width); i++, area++)
            sum += image.pixel.b[i];
        }
        average = sum / area;
        break;
      default: return(H_ERR_WIT);   /* wrong image type  */
    }

Figure 5.7: Example for HGetImage: Average gray value of first channel.

The data structure Himage contains the gray values, the gray value type, and the size of the image matrix. Instead of copying the image matrix, HGetImage only returns a pointer to the raw data in image.pixel. This is much more efficient, but means that only read access to the image matrix is recommended (otherwise you will encounter unpredictable side effects). Figure 5.7 shows an application of HGetImage.