---
title: Threshold
description: "对图像中的像素进行阈值处理，阈值函数根据图像像素值是小于还是大于指定值来更改像素值。用于阈值像素值的比较操作的类型由HMPPCmpOp参数指定。这个操作可以是“大于”或“小于”，如果输入像素值满足比较条件，则将相应的输出像素设置为阈值，否则它不会更改或设置输出像素值，计算公式为："
url: https://www.hikunpeng.com/document/detail/zh/kunpengboostkithistory/251RC1/accel/kunpengaccel_hmpp_06_0191.html
sourcePath: /source/zh/kunpengboostkithistory/251RC1/accel/kunpengaccel_hmpp_06_0191.html
indexId: e1dd9c0df5c42219a4f6c707ae62c96d7a25c33a5443e6a57e80a57bd7e4d06277
---
# Threshold

对图像中的像素进行阈值处理，阈值函数根据图像像素值是小于还是大于指定值来更改像素值。用于阈值像素值的比较操作的类型由HMPPCmpOp参数指定。这个操作可以是“大于”或“小于”，如果输入像素值满足比较条件，则将相应的输出像素设置为阈值，否则它不会更改或设置输出像素值，计算公式为：

- 当cmpOp=HMPP_CMPLESS时，如果src中的像素值小于threshold时，将threshold值赋值给dst，否则将src中的像素值赋值给dst。
- 当cmpOp=HMPP_CMPGREATER时，如果src中的像素值大于threshold时，将threshold值赋值给dst，否则将src中的像素值赋值给dst。

函数接口声明如下：

- 单通道数据的阈值操作：
  HmppResult HMPPI_Threshold_8u_C1R(const uint8_t *src, int32_t srcStep, uint8_t *dst, int32_t dstStep, HmppiSize roiSize, uint8_t threshold, HmppCmpOp cmpOp);

  HmppResult HMPPI_Threshold_16u_C1R(const uint16_t *src, int32_t srcStep, uint16_t *dst, int32_t dstStep, HmppiSize roiSize, uint16_t threshold, HmppCmpOp cmpOp);

  HmppResult HMPPI_Threshold_16s_C1R(const int16_t *src, int32_t srcStep, int16_t *dst, int32_t dstStep, HmppiSize roiSize, int16_t threshold, HmppCmpOp cmpOp);

  HmppResult HMPPI_Threshold_32f_C1R(const float *src, int32_t srcStep, float *dst, int32_t dstStep, HmppiSize roiSize, float threshold, HmppCmpOp cmpOp);

- 多通道数据的阈值操作：
  HmppResult HMPPI_Threshold_8u_C3R(const uint8_t *src, int32_t srcStep, uint8_t*dst, int32_t dstStep, HmppiSize roiSize, const uint8_t threshold[3], HmppCmpOp cmpOp);

  HmppResult HMPPI_Threshold_16u_C3R(const uint16_t *src, int32_t srcStep, uint16_t*dst, int32_t dstStep, HmppiSize roiSize, const uint16_t threshold[3], HmppCmpOp cmpOp);

  HmppResult HMPPI_Threshold_16s_C3R(const int16_t *src, int32_t srcStep, int16_t*dst, int32_t dstStep, HmppiSize roiSize, const int16_t threshold[3], HmppCmpOp cmpOp);

  HmppResult HMPPI_Threshold_32f_C3R(const float *src, int32_t srcStep, float*dst, int32_t dstStep, HmppiSize roiSize, const float threshold[3], HmppCmpOp cmpOp);

  HmppResult HMPPI_Threshold_8u_AC4R(const uint8_t *src, int32_t srcStep, uint8_t*dst, int32_t dstStep, HmppiSize roiSize, const uint8_t threshold[3], HmppCmpOp cmpOp);

  HmppResult HMPPI_Threshold_16u_AC4R(const uint16_t *src, int32_t srcStep, uint16_t*dst, int32_t dstStep, HmppiSize roiSize, const uint16_t threshold[3], HmppCmpOp cmpOp);

  HmppResult HMPPI_Threshold_16s_AC4R(const int16_t *src, int32_t srcStep, int16_t*dst, int32_t dstStep, HmppiSizeroiSize, const int16_t threshold[3], HmppCmpOp cmpOp);

  HmppResult HMPPI_Threshold_32f_AC4R(const float *src, int32_t srcStep, float*dst, int32_t dstStep, HmppiSizeroiSize, const float threshold[3], HmppCmpOp cmpOp);

- 单通道数据的原址阈值操作：
  HmppResult HMPPI_Threshold_8u_C1IR(uint8_t *srcDst, int32_t srcDstStep, HmppiSize roiSize, uint8_t threshold, HmppCmpOp cmpOp);

  HmppResult HMPPI_Threshold_16u_C1IR(uint16_t *srcDst, int32_t srcDstStep, HmppiSize roiSize, uint16_t threshold, HmppCmpOp cmpOp);

  HmppResult HMPPI_Threshold_16s_C1IR(int16_t *srcDst, int32_t srcDstStep, HmppiSize roiSize, int16_t threshold, HmppCmpOp cmpOp);

  HmppResult HMPPI_Threshold_32f_C1IR(float *srcDst, int32_t srcDstStep, HmppiSize roiSize, float threshold, HmppCmpOp cmpOp);

- 多通道数据的原址阈值操作：
  HmppResult HMPPI_Threshold_8u_C3IR(uint8_t *srcDst, int32_t srcDstStep, HmppiSize roiSize, const uint8_t threshold[3], HmppCmpOp cmpOp);

  HmppResult HMPPI_Threshold_16u_C3IR(uint16_t *srcDst, int32_t srcDstStep, HmppiSize roiSize, const uint16_t threshold[3], HmppCmpOp cmpOp);

  HmppResult HMPPI_Threshold_16s_C3IR(int16_t *srcDst, int32_t srcDstStep, HmppiSize roiSize, const int16_t threshold[3], HmppCmpOp cmpOp);

  HmppResult HMPPI_Threshold_32f_C3IR(float *srcDst, int32_t srcDstStep, HmppiSize roiSize, const float threshold[3], HmppCmpOp cmpOp);

  HmppResult HMPPI_Threshold_8u_AC4IR(uint8_t *srcDst, int32_t srcDstStep, HmppiSize roiSize, const uint8_t threshold[3], HmppCmpOp cmpOp);

  HmppResult HMPPI_Threshold_16u_AC4IR(uint16_t *srcDst, int32_t srcDstStep, HmppiSize roiSize, const uint16_t threshold[3], HmppCmpOp cmpOp);

  HmppResult HMPPI_Threshold_16s_AC4IR(int16_t *srcDst, int32_t srcDstStep, HmppiSize roiSize, const int16_t threshold[3], HmppCmpOp cmpOp);

  HmppResult HMPPI_Threshold_32f_AC4IR(float *srcDst, int32_t srcDstStep, HmppiSize roiSize, const float threshold[3], HmppCmpOp cmpOp);


#### 参数

| 参数名 | 描述 | 取值范围 | 输入/输出 |
| --- | --- | --- | --- |
| src | 指向源图像感兴趣区域的指针。 | 非空 | 输入 |
| srcStep | 源图像中连续行起点之间的距离（以字节为单位）。 | 非负整数 | 输入 |
| dst | 指向目标图像感兴趣区域的指针。 | 非空 | 输出 |
| dstStep | 目标图像中连续行的起点之间的距离（以字节为单位）。 | 非负整数 | 输入 |
| srcDst | 指向源和目标图像感兴趣区域的指针（用于原址操作）。 | 非空 | 输入、输出 |
| srcDstStep | 原址操作的源图像和目标图像中连续行起点之间的距离（以字节为单位）。 | 非负整数 | 输入 |
| roiSize | 源和目标图像感兴趣区域的大小（以像素为单位）。 | roiSize.width∈(0, INT\_MAX]，roiSize.height∈(0, INT\_MAX] | 输入 |
| threshold | 每个像素要使用的阈值水平值；多通道数据下，使用每个颜色通道的阈值数组。 | 任意值 | 输入 |
| cmpOp | 指定用于比较像素值和阈值的操作。可以使用“大于”或“小于”的比较。 | HMPP\_CMP\_LT，HMPP\_CMP\_GT | 输入 |


#### 返回值

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


#### 错误码

| 错误码 | 描述 |
| --- | --- |
| 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\_NOT\_EVEN\_STEP\_ERR | srcStep、dstStep不能被src、dst所属数据类型的字节长度整除的错误条件。 |
| HMPP\_STS\_ROI\_ERR | roiSize.width > 步长。 |
| HMPP\_STS\_NOT\_SUPPORTED\_MODE\_ERR | 不支持的比较模式。 |


#### 示例

```
void ThresholdExample()
{
HmppiSize roi = {3,4};
uint8_t src[9*4] = { 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 dst[9*4] = {0};
uint8_t threshold[3] = {8,8,8};
int srcStep=9*sizeof(uint8_t);
int dstStep=9*sizeof(uint8_t);
HMPPI_Threshold_8u_C3R(src, srcStep, dst, dstStep, roi, threshold, HMPP_CMP_LT);
for (int i = 0; i < 4; i++){
for (int j = 0; j < 9; j++){
printf("%4d ",dst[i*9+j]);
}
printf("\n");
}
}
int main(void)
{
ThresholdExample();
return 0;
}
```

运行结果：

```
8    8    8    8   16    8    8    8    8
8    8    8    8   16    8    8    8    8
8    8    8    8   16    8    8    8    8
8    8    8    8   16    8    8    8    8
```
