实现灰度图像和彩色图像的迭代阈值分割算法c语言代码实现
时间: 2023-11-15 07:06:36 浏览: 77
division.rar_matlab 灰度图像 分割_图像阈值分割
5星 · 资源好评率100%
以下是灰度图像和彩色图像的迭代阈值分割算法的C语言代码实现:
## 灰度图像迭代阈值分割算法
```c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define WIDTH 512
#define HEIGHT 512
#define MAX_ITER 100
void threshold(unsigned char* img, int width, int height, int* t) {
int iter = 0;
double t1 = 0.0, t2 = 255.0;
double mean1 = 0.0, mean2 = 0.0;
int count1 = 0, count2 = 0;
double diff = 0.0, max_diff = 0.0;
while (iter < MAX_ITER && fabs(t1 - t2) > 0.1) {
iter++;
mean1 = 0.0;
mean2 = 0.0;
count1 = 0;
count2 = 0;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
if (img[i * width + j] <= *t) {
mean1 += img[i * width + j];
count1++;
} else {
mean2 += img[i * width + j];
count2++;
}
}
}
if (count1 > 0 && count2 > 0) {
mean1 /= count1;
mean2 /= count2;
t1 = *t;
*t = (int) ((mean1 + mean2) / 2.0);
t2 = *t;
} else {
break;
}
}
}
int main() {
unsigned char* img = (unsigned char*) malloc(WIDTH * HEIGHT * sizeof(unsigned char));
int t = 128;
// 读取图像
FILE* fp = fopen("lena_gray.raw", "rb");
fread(img, sizeof(unsigned char), WIDTH * HEIGHT, fp);
fclose(fp);
// 进行阈值分割
threshold(img, WIDTH, HEIGHT, &t);
// 将阈值以下的像素设为0,以上的像素设为255
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
if (img[i * WIDTH + j] <= t) {
img[i * WIDTH + j] = 0;
} else {
img[i * WIDTH + j] = 255;
}
}
}
// 保存图像
fp = fopen("lena_gray_threshold.raw", "wb");
fwrite(img, sizeof(unsigned char), WIDTH * HEIGHT, fp);
fclose(fp);
free(img);
return 0;
}
```
## 彩色图像迭代阈值分割算法
```c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define WIDTH 512
#define HEIGHT 512
#define MAX_ITER 100
void threshold(unsigned char* img, int width, int height, int* t) {
int iter = 0;
double t1 = 0.0, t2 = 255.0;
double mean1_r = 0.0, mean1_g = 0.0, mean1_b = 0.0;
double mean2_r = 0.0, mean2_g = 0.0, mean2_b = 0.0;
int count1 = 0, count2 = 0;
double diff = 0.0, max_diff = 0.0;
while (iter < MAX_ITER && fabs(t1 - t2) > 0.1) {
iter++;
mean1_r = 0.0;
mean1_g = 0.0;
mean1_b = 0.0;
mean2_r = 0.0;
mean2_g = 0.0;
mean2_b = 0.0;
count1 = 0;
count2 = 0;
for (int i = 0; i < height; i++) {
for (int j = 0; j < width; j++) {
int index = (i * width + j) * 3;
if (img[index] <= *t && img[index + 1] <= *t && img[index + 2] <= *t) {
mean1_r += img[index];
mean1_g += img[index + 1];
mean1_b += img[index + 2];
count1++;
} else {
mean2_r += img[index];
mean2_g += img[index + 1];
mean2_b += img[index + 2];
count2++;
}
}
}
if (count1 > 0 && count2 > 0) {
mean1_r /= count1;
mean1_g /= count1;
mean1_b /= count1;
mean2_r /= count2;
mean2_g /= count2;
mean2_b /= count2;
t1 = *t;
*t = (int) ((mean1_r + mean1_g + mean1_b + mean2_r + mean2_g + mean2_b) / 6.0);
t2 = *t;
} else {
break;
}
}
}
int main() {
unsigned char* img = (unsigned char*) malloc(WIDTH * HEIGHT * 3 * sizeof(unsigned char));
int t = 128;
// 读取图像
FILE* fp = fopen("lena_color.raw", "rb");
fread(img, sizeof(unsigned char), WIDTH * HEIGHT * 3, fp);
fclose(fp);
// 进行阈值分割
threshold(img, WIDTH, HEIGHT, &t);
// 将阈值以下的像素设为0,以上的像素设为255
for (int i = 0; i < HEIGHT; i++) {
for (int j = 0; j < WIDTH; j++) {
int index = (i * WIDTH + j) * 3;
if (img[index] <= t && img[index + 1] <= t && img[index + 2] <= t) {
img[index] = img[index + 1] = img[index + 2] = 0;
} else {
img[index] = img[index + 1] = img[index + 2] = 255;
}
}
}
// 保存图像
fp = fopen("lena_color_threshold.raw", "wb");
fwrite(img, sizeof(unsigned char), WIDTH * HEIGHT * 3, fp);
fclose(fp);
free(img);
return 0;
}
```
阅读全文