Rate This Document
Findability
Accuracy
Completeness
Readability

GrayErodeBorder

Performs a grayscale erosion on an image by using a mask. A grayscale erosion is to obtain the minimum value of the sum of the local source image pixels and mask values. It is represented by and defined as follows:

The formula indicates that mask B is used to perform grayscale erosion on image A. This function convolves image A with mask B. It scans all pixels in the image, calculates the minimum value of the sum of B-covered image pixels and mask B values, and replaces the pixel value specified by the anchor point with this minimum value.

The following figure shows the interface input and output.

This function supports only the HMPP_BORDER_REPL border type. For details, see the description about HmppiBorderType in Enumeration Types.

The function is called as follows:

  1. Call the Init function to initialize the HmppiMorphGrayPolicy structure. Otherwise, the main function cannot be called successfully.
  2. Call the main function.
  3. Call Release to release the memory contained in the HmppiMorphGrayPolicy function. Otherwise, memory leak occurs.

The function interface declaration is as follows:

  • Initialization:

    HmppResult HMPPI_MorphGrayInit_8u_C1R(HmppiMorphGrayPolicy_8u **policy, HmppiSize roiSize, const int32_t *mask, HmppiSize maskSize, HmppiPoint anchor);

    HmppResult HMPPI_MorphGrayInit_32f_C1R(HmppiMorphGrayPolicy_32f **policy, HmppiSize roiSize, const float *mask, HmppiSize maskSize, HmppiPoint anchor);

  • Main functions:

    HmppResult HMPPI_GrayErodeBorder_8u_C1R(const uint8_t *src, int32_t srcStep, uint8_t *dst, int32_t dstStep, HmppiSize roiSize, HmppiBorderType borderType, HmppiMorphGrayPolicy_8u *policy);

    HmppResult HMPPI_GrayErodeBorder_32f_C1R(const float *src, int32_t srcStep, float *dst, int32_t dstStep, HmppiSize roiSize, HmppiBorderType borderType, HmppiMorphGrayPolicy_32f *policy);

  • Memory release:

    HmppResult HMPPI_MorphGrayRelease_8u(HmppiMorphGrayPolicy_8u *policy);

    HmppResult HMPPI_MorphGrayRelease_32f(HmppiMorphGrayPolicy_32f *policy);

Parameters

Parameter

Description

Value Range

Input/Output

mask

Pointer to the mask

The value cannot be NULL.

Input

maskSize

Size of the mask image, in pixels

maskSize.width ∈ (0, INT_MAX], maskSize.height ∈ (0, INT_MAX]

Input

anchor

Coordinates of the anchor point

anchor.x ∈ (0, maskSize.width-1], anchor.y ∈ (0, maskSize.height-1]

Input

src

Pointer to the source image

The value cannot be NULL.

Input

srcStep

Distance between starts of consecutive lines in the source image, in bytes

The value must be a non-negative integer multiple of the bytes of src's data type.

Input

dst

Pointer to the destination vector

The value cannot be NULL.

Output

dstStep

Distance between starts of consecutive lines in the destination image, in bytes

The value must be a non-negative integer multiple of the bytes of dst's data type.

Input

roiSize

Size of the ROI of the source and destination images, in pixels

roiSize.width ∈ (0, INT_MAX], roiSize.height ∈ (0, INT_MAX]

Input

borderType

Border type for border making.

Enumeration.

HMPP_BORDER_REPL: The values for border pixels are copied from the boundary pixels of the source image.

Input

policy

(in the Init function)

Pointer to the memory that stores the pointer to HmppiMorphGrayPolicy

The value cannot be NULL.

Output

policy (in the main and release functions)

Pointer to the HmppiMorphGrayPolicy structure

The value cannot be NULL.

Input

Return Value

  • Success: HMPP_STS_NO_ERR
  • Failure: An error code is returned.

Error Codes

Error Code

Description

HMPP_STS_NULL_PTR_ERR

Any of the specified pointers is NULL.

HMPP_STS_SIZE_ERR

The value of roiSize or maskSize is 0 or negative.

HMPP_STS_ANCHOR_ERR

The value of anchor is negative or greater than or equal to the value of maskSize.

HMPP_STS_STEP_ERR

The value of srcStep or dstStep is 0 or negative.

HMPP_STS_MALLOC_FAILED

The Init function failed to allocate the memory required by the algorithm model.

HMPP_STS_NOT_EVEN_STEP_ERR

The value of srcStep or dstStep is not an integer multiple of the bytes of the involved image's data type.

HMPP_STS_ROI_ERR

The width and height of the source or destination image are smaller than those specified by roiSize.

HMPP_STS_BORDER_ERR

The border type is not supported.

HMPP_STS_NOT_SUPPORTED_INPLACE_MODE_ERR

The memory addresses of src and dst are the same.

src and dst cannot be the same array or have overlapping memories. Otherwise, the result may be incorrect.

Example

#define SRC_BUFFER_SIZE_T 96
#define DST_BUFFER_SIZE_T 64
#define MASK_BUFFER_SIZE_T 12
void GrayErodeBorderExample()
{
    HmppiSize roiSize = { 4, 4 };
    const uint8_t src[SRC_BUFFER_SIZE_T] = {
        11, 122, 13, 14, 15, 16, 17, 18, 12, 19, 13, 15,
        21, 22, 23, 24, 25, 26, 27, 28, 22, 29, 23, 25,
        31, 32, 33, 34, 35, 36, 37, 38, 32, 39, 33, 35,
        41, 42, 43, 44, 45, 46, 47, 48, 42, 49, 43, 45,
        51, 52, 53, 54, 55, 56, 57, 58, 52, 59, 53, 55,
        61, 62, 63, 64, 65, 66, 67, 68, 62, 69, 63, 65,
        71, 72, 73, 74, 75, 76, 77, 78, 72, 79, 73, 75,
        81, 82, 83, 84, 85, 86, 87, 88, 82, 89, 83, 85
    };

    uint8_t dst[DST_BUFFER_SIZE_T] = { 0 };
    HmppiSize maskSize = { 4, 3 };
    HmppiPoint anchor = { 0, 0 };
    int32_t mask[MASK_BUFFER_SIZE_T] = {
        -7, 0, -2, 3,
        5, -1, 1, 0,
        0, 2, 0, -6,
    };

    const int32_t srcWidth = 12;
    const int32_t dstWidth = 8;
    int32_t srcStep = srcWidth * sizeof(uint8_t);
    int32_t dstStep = dstWidth * sizeof(uint8_t);
    HmppiBorderType borderType = HMPPI_BORDER_REPL;
    HmppiMorphGrayPolicy_8u *policy = NULL;
    HmppResult result;

    result = HMPPI_MorphGrayInit_8u_C1R(&policy, roiSize, mask, maskSize, anchor);
    printf("HMPPI_MorphGrayInit_8u_C1R result = %d\n", result);
    if (result != HMPP_STS_NO_ERR) {
        return;
    }
    result = HMPPI_GrayErodeBorder_8u_C1R(src, srcStep, dst, dstStep, roiSize, borderType, policy);
    printf("HMPPI_GrayErodeBorder_8u_C1R result = %d\n", result);
    (void)HMPPI_MorphGrayRelease_8u(policy);
    policy = NULL;
    if (result != HMPP_STS_NO_ERR) {
        return;
    }

    printf("result = %d \ndst = ", result);
    if (result != HMPP_STS_NO_ERR) {
        printf("result error: %d\n", result);
    }

    for (int32_t i = 0; i < DST_BUFFER_SIZE_T; ++i) {
        if (i % dstWidth == 0) {
            printf("\n");
        }
        printf("%3d ", dst[i]);
    }
    printf("\n");
}

int main(void)
{
    GrayErodeBorderExample();
    return 0;
}

Output:

HMPPI_MorphGrayInit_8u_C1R result = 0
HMPPI_GrayDilateBorder_8u_C1R result = 0
result = 0
dst =
  4  12   6   7   0   0   0   0
 14  15  16  17   0   0   0   0
 24  25  26  27   0   0   0   0
 34  35  36  37   0   0   0   0
  0   0   0   0   0   0   0   0
  0   0   0   0   0   0   0   0
  0   0   0   0   0   0   0   0
  0   0   0   0   0   0   0   0