FilterMedian
Computes the median value of elements in a source vector.
A median filter is a nonlinear sorting filter that uses a mask. It replaces elements in a source vector with median values in adjacent areas. Median filters are often used in image and signal processing to filter out impulse noise. Generally, the mask length is set to an odd number, which simplifies calculation and reduces the offset of the output signal. The HMPP also enables delay lines. During calculation, the delay line array dlySrc is added to the left of the source vector. If dlySrc is null, (maskSize-1) numbers of src[0] is added.
The function interface declaration is as follows:
- Initialization:
HmppResult HMPPS_FilterMedianInit(int32_t maskSize, HmppDataType dataType, uint8_t **buffer);
- Main functions:
HmppResult HMPPS_FilterMedian_8u(const uint8_t *src, uint8_t *dst, int32_t len, int32_t maskSize, const uint8_t *dlySrc, uint8_t *dlyDst, uint8_t *buffer);
HmppResult HMPPS_FilterMedian_16s(const int16_t *src, int16_t *dst, int32_t len, int32_t maskSize, const int16_t *dlySrc, int16_t *dlyDst, uint8_t *buffer);
HmppResult HMPPS_FilterMedian_32s(const int32_t *src, int32_t *dst, int32_t len, int32_t maskSize, const int32_t *dlySrc, int32_t *dlyDst, uint8_t *buffer);
HmppResult HMPPS_FilterMedian_32f(const float *src, float *dst, int32_t len, int32_t maskSize, const float *dlySrc, float *dlyDst, uint8_t *buffer);
HmppResult HMPPS_FilterMedian_64f(const double *src, double *dst, int32_t len, int32_t maskSize, const double *dlySrc, double *dlyDst, uint8_t *buffer);
HmppResult HMPPS_FilterMedian_8u_I(uint8_t *srcDst, int32_t len, int32_t maskSize, const uint8_t *dlySrc, uint8_t *dlyDst, uint8_t *buffer);
HmppResult HMPPS_FilterMedian_16s_I(int16_t *srcDst, int32_t len, int32_t maskSize, const int16_t *dlySrc, int16_t *dlyDst, uint8_t *buffer);
HmppResult HMPPS_FilterMedian_32s_I(int32_t *srcDst, int32_t len, int32_t maskSize, const int32_t *dlySrc, int32_t *dlyDst, uint8_t *buffer);
HmppResult HMPPS_FilterMedian_32f_I(float *srcDst, int32_t len, int32_t maskSize, const float *dlySrc, float *dlyDst, uint8_t *buffer);
HmppResult HMPPS_FilterMedian_64f_I(double *srcDst, int32_t len, int32_t maskSize, const double *dlySrc, double *dlyDst, uint8_t *buffer);
- Memory release:
HmppResult HMPPS_FilterMedianRelease(uint8_t *buffer);
Parameters
Parameter |
Description |
Value Range |
Input/Output |
|---|---|---|---|
srcDst |
Pointer to the source vector for in-place operations |
The value cannot be NULL. |
Input/Output |
src |
Pointer to the source vector |
The value cannot be NULL. |
Input |
dst |
Pointer to the destination vector |
The value cannot be NULL. |
Output |
len |
Vector length |
(0, INT_MAX] |
Input |
maskSize |
Median mask size. If the value is an even number, the function subtracts 1 and uses the odd value for median filtering. |
(0, len] |
Input |
dlySrc |
Pointer to the array containing values of the source delay lines |
None |
Input |
dlyDst |
Pointer to the array containing values of the destination delay lines |
None |
Output |
buffer |
Pointer to the work buffer |
The value cannot be NULL. |
Input |
bufferSize |
Size of the work buffer |
The value cannot be NULL. |
Output |
dataType |
Data type supported by median filtering. Possible values are 8u, 16s, 32s, 32f, and 64f. |
The value cannot be NULL. |
Input |
Return Value
- Success: HMPP_STS_NO_ERR
- Alarm: HMPP_STS_EVEN_MEDIAN_MASK_SIZE
- Failure: An error code is returned.
Error Codes
Error Code |
Description |
|---|---|
HMPP_STS_NULL_PTR_ERR |
The value of srcDst, src, dst, or buffer is NULL. |
HMPP_STS_SIZE_ERR |
The value of len is less than or equal to 0. |
HMPP_STS_MASK_SIZE_ERR |
The median mask is less than or equal to 0 or greater than the value of len. |
HMPP_STS_EVEN_MEDIAN_MASK_SIZE |
An alarm indicating that the median mask is an even number |
Note
The mask length is generally an odd number. You can set the mask length to an even number, but the mask length will be converted to an odd number through subtraction.
Example
#define DATA_SIZE 10
#define MASK_SIZE 3
void FilterMedianExample(void)
{
int16_t src[DATA_SIZE] = { 1, 3, 3, 1, 5, 6, 3, 8, 19, 10 };
int16_t dst[DATA_SIZE] = { 0 };
uint8_t *buffer = NULL;
int16_t dlySrc[MASK_SIZE-1] = { 2, 4 };
int16_t dlyDst[MASK_SIZE-1] = { 0 };
HmppResult retVal;
retVal = HMPPS_FilterMedianInit(MASK_SIZE, HMPP16S, &buffer);
if (retVal != HMPP_STS_EVEN_MEDIAN_MASK_SIZE && retVal != HMPP_STS_NO_ERR) {
return;
}
retVal = HMPPS_FilterMedian_16s(src, dst, DATA_SIZE, MASK_SIZE, dlySrc, dlyDst, buffer);
printf("result = %d\n", retVal);
if (retVal != HMPP_STS_NO_ERR) {
return;
}
HMPPS_FilterMedianRelease(buffer);
int32_t i;
printf("dst =");
for (i = 0; i < DATA_SIZE; ++i) {
printf(" %d", dst[i]);
}
printf("\ndlyDst =");
for (i = 0; i < MASK_SIZE - 1; ++i) {
printf(" %d", dlyDst[i]);
}
printf("\n");
}
Output:
result = 0 dst = 2 3 3 3 3 5 5 6 8 10 dlyDst = 19 10