---
title: ComputeThreshold_Otsu
description: "计算Otsu阈值的值。计算公式为："
url: https://www.hikunpeng.com/document/detail/zh/kunpengboostkithistory/240RC1/accel/kunpengaccel_hmpp_06_0182.html
sourcePath: /source/zh/kunpengboostkithistory/240RC1/accel/kunpengaccel_hmpp_06_0182.html
indexId: 01ab1f4ec0935db69d115f68eb77ace32285591db48a41dacc3bba6bcad317c277
---
# ComputeThreshold_Otsu

计算Otsu阈值的值。计算公式为：


w0：前景点所占的比例。

wl：背景点所占比例(1 - w0)。

u0：前景点灰度值。

u1：背景点的灰度值。

u：图像整体的均值。

函数接口声明如下：

单通道数据的阈值操作：

HmppResult HMPPI_ComputeThreshold_Otsu_8u_C1R(const uint8_t* src, int32_t srcStep, HmppiSize roiSize, uint8_t* threshold);

#### 参数

| 参数名 | 描述 | 取值范围 | 输入/输出 |
| --- | --- | --- | --- |
| src | 指向源图像感兴趣区域的指针。 | 非空 | 输入 |
| srcStep | 源图像中连续行起点之间的距离（以字节为单位）。 | 非空 | 输入 |
| roiSize | 源和目标图像感兴趣区域的大小（以像素为单位）。 | roiSize.width∈(0, INT\_MAX]，roiSize.height∈(0, INT\_MAX] | 输入 |
| threshold | 指向Otsu阈值的指针。 | 输入数据类型的范围 | 输出 |


#### 返回值

- 成功：返回HMPP_STS_NO_ERR。
- 失败：返回错误码。


#### 错误码

| 错误码 | 描述 |
| --- | --- |
| HMPP\_STS\_NULL\_PTR\_ERR | src、dst、srcDst中存在空指针。 |
| HMPP\_STS\_SIZE\_ERR | roiSize的width、height存在零或负值。 |
| HMPP\_STS\_STEP\_ERR | srcStep、dstStep、srcDstStep中存在零或负值。 |
| HMPP\_STS\_ROI\_ERR | roiSize.width > 步长。 |


#### 示例

```
#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;
}
```

运行结果：

```
result = 0
dst = 4
```
