写一个局部直方图均衡化的C语言代码
时间: 2023-05-22 10:04:45 浏览: 77
以下是一个简单的局部直方图均衡化的C语言代码,供您参考:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define WIDTH 512
#define HEIGHT 512
#define BLOCK_WIDTH 32
#define BLOCK_HEIGHT 32
#define BLOCK_SIZE (BLOCK_WIDTH * BLOCK_HEIGHT)
void histogram_equalization(unsigned char *input, unsigned char *output, int width, int height, int block_width, int block_height);
int main() {
unsigned char *input_image = (unsigned char *)malloc(WIDTH * HEIGHT);
unsigned char *output_image = (unsigned char *)malloc(WIDTH * HEIGHT);
// 读入输入图像数据
FILE *fp = fopen("lena.raw", "rb");
fread(input_image, sizeof(unsigned char), WIDTH * HEIGHT, fp);
fclose(fp);
// 执行局部直方图均衡化
histogram_equalization(input_image, output_image, WIDTH, HEIGHT, BLOCK_WIDTH, BLOCK_HEIGHT);
// 将输出数据写入文件
fp = fopen("output.raw", "wb");
fwrite(output_image, sizeof(unsigned char), WIDTH * HEIGHT, fp);
fclose(fp);
free(input_image);
free(output_image);
return 0;
}
void histogram_equalization(unsigned char *input, unsigned char *output, int width, int height, int block_width, int block_height) {
int blocks_per_row = ceil((double)width / block_width);
int blocks_per_column = ceil((double)height / block_height);
int block_histogram[BLOCK_SIZE][256];
// 计算每个块的直方图
for (int i = 0; i < blocks_per_column; i++) {
for (int j = 0; j < blocks_per_row; j++) {
memset(block_histogram, 0, BLOCK_SIZE * 256 * sizeof(int));
int start_x = j * block_width;
int start_y = i * block_height;
int end_x = (j + 1) * block_width;
int end_y = (i + 1) * block_height;
if (end_x > width) end_x = width;
if (end_y > height) end_y = height;
int block_size = (end_x - start_x) * (end_y - start_y);
for (int x = start_x; x < end_x; x++) {
for (int y = start_y; y < end_y; y++) {
block_histogram[(x - start_x) * block_height + (y - start_y)][input[x + y * width]]++;
}
}
// 均衡化直方图
for (int k = 0; k < BLOCK_SIZE; k++) {
int sum = 0;
int histogram[256] = {0};
for (int l = 0; l < 256; l++) {
sum += block_histogram[k][l];
histogram[l] = sum * 255 / block_size;
}
// 将处理后的块数据写入输出图像
int x = start_x + (k % block_width);
int y = start_y + (k / block_width);
output[x + y * width] = histogram[input[x + y * width]];
}
}
}
}
相关推荐
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)