c语言代码Ostu大津法
时间: 2023-10-23 18:10:41 浏览: 98
好的,以下是 C 语言代码 Ostu 大津法:
```c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main()
{
int hist[256] = {0};
int i, j, k;
int threshold = 0;
int sum = 0;
int sumB = 0;
int wB = 0;
int wF = 0;
double mB, mF;
double max = 0.0;
double between = 0.0;
// 读取图像并计算直方图
// ...
// 计算总像素数
for (i = 0; i < 256; i++) {
sum += hist[i];
}
// 计算最佳阈值
for (i = 0; i < 256; i++) {
wB += hist[i]; if (wB == 0) {
continue;
}
wF = sum - wB;
if (wF == 0) {
break;
}
sumB += i * hist[i];
mB = (double)sumB / wB;
mF = (double)(sum - sumB) / wF;
between = (double)wB * (double)wF * pow(mB - mF, 2);
if (between >= max) {
threshold = i;
max = between;
}
}
printf("Threshold: %d\n", threshold);
return 0;
}
```
阅读全文