c语言图像压缩算法实现_C语言实现BMP图像处理:直方图均衡化

计算步骤:

1)统计各灰度值的概率;

2)计算了累积概率直方图(CDF);

3)取整扩展:Tk = int[ (L-1)*Tk];

代码

#include #include #include #include  int main(int* argc, char** argv){FILE* fp = fopen("./01.bmp", "rb");if (fp == 0)return 0;BITMAPFILEHEADER fileHead;fread(&fileHead, sizeof(BITMAPFILEHEADER), 1, fp);BITMAPINFOHEADER infoHead;fread(&infoHead, sizeof(BITMAPINFOHEADER), 1, fp);int width = infoHead.biWidth;int height = infoHead.biHeight;int biCount = infoHead.biBitCount; int lineByte = (biCount*width / 8 + 3) / 4 * 4;RGBQUAD* pColorTable;pColorTable = new RGBQUAD[256];fread(pColorTable, sizeof(RGBQUAD), 256, fp); unsigned char* pBmpBuf;pBmpBuf = new unsigned char[lineByte*height];fread(pBmpBuf, lineByte*height, 1, fp);fclose(fp);// 统计概率double st[256] = { 0 };int st1[256] = { 0 };int t;for (int i = 0; i < height; ++i){for (int j = 0; j < width; ++j){t = *(pBmpBuf + i*lineByte + j);st[t]++;}}// 计算累加直方图并完成映射st[0] = st[0] / (width*height);st1[0] = round(double((256 - 1)*st[0]));for (int i = 1; i < 256; ++i){st[i] = st[i] / (width*height);st[i] = st[i] + st[i - 1];st1[i] = int(round(double((256 - 1)*st[i])));printf("st[i] = %d, st1[t] = %d", st[i], st1[i]);}// 新图像的像素填充unsigned char* pBmpBuf1;pBmpBuf1 = new unsigned char[lineByte*height];for (int i = 0; i < height; ++i){for (int j = 0; j < width; ++j){t = *(pBmpBuf + i*lineByte + j);*(pBmpBuf1 + i*lineByte + j) = st1[t];}} FILE* fop = fopen("./imhist.bmp", "wb");if (fop == 0)return 0;fwrite(&fileHead, sizeof(BITMAPFILEHEADER), 1, fop);fwrite(&infoHead, sizeof(BITMAPINFOHEADER), 1, fop);fwrite(pColorTable, sizeof(RGBQUAD), 256, fop);fwrite(pBmpBuf1, lineByte*height, 1, fop);fclose(fop); system("pause");return 0;}

效果展现

e83873312ed5a90c408f05c15be56d1c.png

原图

39849b2d7ac09344f5ebf38220f4b74d.png

直方图均衡化的结果

编译环境

983b37fc8b3bb71baeac7044741c78d8.png

VS2013

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以为您提供一些关于BMP图像直方图均衡算法的基本思路和C语言实现直方图均衡化是一种图像增强技术,它可以使图像的对比度增强,提高图像的质量。直方图均衡化的基本思路是将图像的像素值进行重新分配,使得图像的灰度级分布更加均匀。 下面是一种基于C语言实现BMP图像直方图均衡算法的伪代码: 1. 读取BMP图像的像素矩阵 2. 统计图像像素矩阵的灰度级直方图 3. 计算直方图均衡化映射表 4. 对原始像素矩阵进行直方图均衡化 5. 将均衡化后的像素矩阵写回BMP图像 下面是一份可以实现直方图均衡化C语言代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> #pragma pack(1) //按字节对齐 typedef struct BMPFileHeader { char bfType[2]; int bfSize; short bfReserved1; short bfReserved2; int bfOffBits; } BMPFileHeader; typedef struct BMPInfoHeader { int biSize; int biWidth; int biHeight; short biPlanes; short biBitCount; int biCompression; int biSizeImage; int biXPelsPerMeter; int biYPelsPerMeter; int biClrUsed; int biClrImportant; } BMPInfoHeader; typedef struct RGB { unsigned char blue; unsigned char green; unsigned char red; } RGB; int main() { FILE* fp; BMPFileHeader bmpFileHeader; BMPInfoHeader bmpInfoHeader; RGB* image; RGB* image2; unsigned char* grayImage; unsigned char* grayImage2; int* hist; int* eqHist; int* map; int width, height, size, size2, i, j, k, max; char filename[100]; // 读取图像文件 printf("Enter the filename of the BMP image: "); scanf("%s", filename); fp = fopen(filename, "rb"); if (!fp) { printf("Error: Failed to open the file.\n"); return -1; } // 读取文件头 fread(&bmpFileHeader, sizeof(BMPFileHeader), 1, fp); if (bmpFileHeader.bfType[0] != 'B' || bmpFileHeader.bfType[1] != 'M') { printf("Error: Not a BMP file.\n"); return -1; } // 读取信息头 fread(&bmpInfoHeader, sizeof(BMPInfoHeader), 1, fp); if (bmpInfoHeader.biBitCount != 24) { printf("Error: Not a 24-bit BMP file.\n"); return -1; } // 计算图像大小 width = bmpInfoHeader.biWidth; height = bmpInfoHeader.biHeight; size = width * height; size2 = sizeof(unsigned char) * size; // 读取像素数据 image = (RGB*)malloc(sizeof(RGB) * size); fread(image, sizeof(RGB), size, fp); // 将RGB图像转换为灰度图像 grayImage = (unsigned char*)malloc(size2); for (i = 0; i < size; i++) { grayImage[i] = (unsigned char)(0.299 * image[i].red + 0.587 * image[i].green + 0.114 * image[i].blue); } // 计算灰度级直方图 hist = (int*)malloc(sizeof(int) * 256); memset(hist, 0, sizeof(int) * 256); for (i = 0; i < size; i++) { hist[grayImage[i]]++; } // 计算直方图均衡化映射表 eqHist = (int*)malloc(sizeof(int) * 256); map = (int*)malloc(sizeof(int) * 256); max = size / 256; for (i = 0; i < 256; i++) { eqHist[i] = 0; for (j = 0; j <= i; j++) { eqHist[i] += hist[j]; } map[i] = (int)(eqHist[i] * 255.0 / size + 0.5); if (eqHist[i] > max) { eqHist[i] = max; } } // 对灰度图像进行直方图均衡化 grayImage2 = (unsigned char*)malloc(size2); for (i = 0; i < size; i++) { grayImage2[i] = (unsigned char)map[grayImage[i]]; } // 将灰度图像转换为RGB图像 image2 = (RGB*)malloc(sizeof(RGB) * size); for (i = 0; i < size; i++) { image2[i].red = grayImage2[i]; image2[i].green = grayImage2[i]; image2[i].blue = grayImage2[i]; } // 写回图像文件 fseek(fp, sizeof(BMPFileHeader) + sizeof(BMPInfoHeader), SEEK_SET); fwrite(image2, sizeof(RGB), size, fp); fclose(fp); free(image); free(image2); free(grayImage); free(grayImage2); free(hist); free(eqHist); free(map); printf("Done.\n"); return 0; } ``` 这份代码可以读取一个BMP图像文件,将其转换为灰度图像,进行直方图均衡化,然后将均衡化后的图像写回文件中。注意,这份代码只支持24位的BMP图像文件。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值