假设有一个灰度图像,其大小为64 x 64,灰度级为8,利用直方图均衡化方法进行直方图均衡化c++代码
时间: 2023-06-03 19:07:31 浏览: 147
其代码如下:
```c
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#define WIDTH 64
#define HEIGHT 64
#define DEPTH 8
void histogram_equalization(unsigned char *image, unsigned int width, unsigned int height, unsigned int depth)
{
// Calculate histogram and corresponding probabilities
int histogram[DEPTH] = { 0 };
double prob[DEPTH] = { 0.0 };
for (unsigned int i = 0; i < width * height; i++)
{
histogram[image[i]]++;
}
for (unsigned int i = 0; i < DEPTH; i++)
{
prob[i] = (double) histogram[i] / ((double) width * (double) height);
}
// Calculate cumulative distribution function
double cdf[DEPTH] = { 0.0 };
for (unsigned int i = 0; i < DEPTH; i++)
{
if (i == 0)
{
cdf[i] = prob[i];
}
else
{
cdf[i] = cdf[i - 1] + prob[i];
}
}
// Convert cdf to histeq table
unsigned char histeq_table[DEPTH] = { 0 };
for (unsigned int i = 0; i < DEPTH; i++)
{
histeq_table[i] = (unsigned char) round((DEPTH - 1) * cdf[i]);
}
// Apply histeq table to image
for (unsigned int i = 0; i < width * height; i++)
{
image[i] = histeq_table[image[i]];
}
}
int main(void)
{
// Load image
unsigned char image[WIDTH * HEIGHT] = { 63, 24, 5, 32, 62, 45, 12, 19, 30, 56, 52, 13, 4, 35, 42, 50, 38, 54, 7, 25, 1, 27, 12, 46, 32, 8, 23, 17, 3, 18, 9, 48, 55, 41, 20, 51, 2, 14, 57, 61, 31, 26, 63, 39, 53, 6, 37, 10, 33, 40, 36, 11, 28, 44, 58, 22, 47, 16, 59, 49, 15, 29, 43, 21, 60 };
// Apply histogram equalization
histogram_equalization(image, WIDTH, HEIGHT, DEPTH);
// Print result
for (unsigned int y = 0; y < HEIGHT; y++)
{
for (unsigned int x = 0; x < WIDTH; x++)
{
printf("%d ", image[y * WIDTH + x]);
}
printf("\n");
}
return 0;
}
```
请注意,这只是一个示例实现,实际上有很多种实现直方图均衡化的方法。
阅读全文
相关推荐
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)