ComputeThreshold_Otsu
Computes the value of the Otsu threshold. The calculation formula is as follows:


w0: ratio between the foreground pixels and the pixels of the entire image
wl: ratio between the background pixels and the pixels of the entire image, which is (1 – w0)
u0: grayscale value of the foreground pixels
u1: grayscale value of the background pixels
u: average value of the entire image
The function interface declaration is as follows:
Thresholding for single-channel data:
HmppResult HMPPI_ComputeThreshold_Otsu_8u_C1R(const uint8_t* src, int32_t srcStep, HmppiSize roiSize, uint8_t* threshold);
Parameters
Parameter |
Description |
Value Range |
Input/Output |
|---|---|---|---|
src |
Pointer to the source image region of interest (ROI) |
The value cannot be NULL. |
Input |
srcStep |
Distance between starts of consecutive lines in the source image, in bytes |
The value cannot be NULL. |
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 |
threshold |
Pointer to the Otsu threshold value |
Range of the input data type |
Output |
Return Value
- Success: HMPP_STS_NO_ERR
- Failure: An error code is returned.
Error Codes
Error Code |
Description |
|---|---|
HMPP_STS_NULL_PTR_ERR |
The value of src, dst, or srcDst is NULL. |
HMPP_STS_SIZE_ERR |
The width or height of roiSize is zero or negative. |
HMPP_STS_STEP_ERR |
The value of srcStep, dstStep, or srcDstStep is zero or negative. |
HMPP_STS_ROI_ERR |
roiSize.width > Step |
Example
#define BUFFER_SIZE 36
int ComputeThreshold_Ostu()
{
HmppiSize roi = { 9, 4 };
const uint8_t src[BUFFER_SIZE] = { 1, 2, 4, 8, 16, 8, 4, 2, 1,
1, 2, 4, 8, 16, 8, 4, 2, 1,
1, 2, 4, 8, 16, 8, 4, 2, 1,
1, 2, 4, 8, 16, 8, 4, 2, 1};
uint8_t threshold;
int32_t srcStep = 9 * sizeof(uint8_t);
HmppResult result = HMPPI_ComputeThreshold_Otsu_8u_C1R(src, srcStep, roi, &threshold);
printf("result = %d \n dst = ", result);
if (result != HMPP_STS_NO_ERR) {
printf("result error: %d\n", result);
}
printf("%d\n", threshold);
return 0;
}
Output:
result = 0 dst = 4