写一个C语言的直方图增强代码
时间: 2023-05-20 15:06:27 浏览: 85
以下是一个简单的C语言直方图增强代码示例:
```c
#include <stdio.h>
#include <stdlib.h>
#define MAX_PIXEL_VALUE 255
int main()
{
int width, height, max_val;
scanf("%*s %d %d %d", &width, &height, &max_val);
int *histogram = (int*)calloc(MAX_PIXEL_VALUE + 1, sizeof(int));
int *cumulative_histogram = (int*)calloc(MAX_PIXEL_VALUE + 1, sizeof(int));
for (int i = 0; i < width * height; i++)
{
int pixel_value;
scanf("%d", &pixel_value);
histogram[pixel_value]++;
}
cumulative_histogram[0] = histogram[0];
for (int i = 1; i <= MAX_PIXEL_VALUE; i++)
{
cumulative_histogram[i] = cumulative_histogram[i - 1] + histogram[i];
}
for (int i = 0; i < width * height; i++)
{
int pixel_value;
scanf("%d", &pixel_value);
int new_pixel_value = (int)(((float)cumulative_histogram[pixel_value] / (float)(width * height)) * MAX_PIXEL_VALUE);
printf("%d ", new_pixel_value);
}
free(histogram);
free(cumulative_histogram);
return 0;
}
```
这个代码可以读取一个PGM格式的图像文件,计算其直方图并进行直方图均衡化,输出处理后的图像。
阅读全文