1.bmp文件的定义

#ifndef IMAGE_H
#define IMAGE_H
void image_info(FILE* file);
void image_save(FILE *file);
void image_gray();
void image_binarization();
void image_opposite();
void image_channel(); //抽取RGB通道
void image_bright();//改变图像亮度
typedef struct BMP
{//14字节unsigned short bfType; //文件标识 2字节 必须为BMunsigned int bfSize; //文件大小 4字节unsigned short bfReserved1; //保留,每字节以"00"填写 2字节unsigned short bfReserved2; //同上 2字节unsigned int bfOffBits; //记录图像数据区的起始位置(图象数据相对于文件头字节的偏移量)。 4字节//40字节unsigned int biSize; //表示本结构的大小 4字节int biWidth; //位图的宽度 4字节int biHeight; //位图的高度 4字节unsigned short biPlanes; //永远为1 , 2字节unsigned short biBitCount; //位图的位数 分为1 4 8 16 24 32 2字节unsigned int biCompression; //压缩说明 4字节unsigned int biSizeImage; //表示位图数据区域的大小以字节为单位 4字节int biXPelsPerMeter; //用象素/米表示的水平分辨率 4字节int biYPelsPerMeter; //用象素/米表示的垂直分辨率 4字节unsigned int biClrUsed; //位图使用的颜色索引数 4字节unsigned int biClrImportant; //对图象显示有重要影响的颜色索引的数目 4字节
} BMP;
int line_byte;
unsigned char *imagedata;
extern BMP bmp;
extern int line_byte;
extern unsigned char *imagedata;
#endif

2.bmp图片的读写以及保存处理

#include<stdio.h>
#include<stdlib.h>
#include"image.h"
void image_info(FILE *file)
{int times=3; //输入文件名次数。char bmp_name[10]; //文件名printf("\nplease enter a file name for reading:");do{if (times<3){printf("\nplease enter a file name for reading again:");}fflush(stdin);gets(bmp_name);//printf("\n%s",bmp_name);file=fopen(bmp_name,"rb+"); //打开一个文件进行读写操作。--times;if (file==NULL){printf("\nerror opening %s for reading! ",bmp_name);}else{break;}}while(times!=0);if (times==0){printf("\nsorry, shutdown!");exit(1);}//读取图像信息fseek(file,0L,0); //读取图像文件类型fread(&bmp,sizeof(BMP),1,file);printf("\n bmp tpye: %u",bmp.bfType);printf("\n bmp size: %u",bmp.bfSize);printf("\n bmp reserved1: %u",bmp.bfReserved1);printf("\n bmp reserved2: %u",bmp.bfReserved2);printf("\n bmp offBits: %u",bmp.bfOffBits);printf("\n bmp bisize: %u",bmp.biSize);printf("\n bmp biWidth: %d",bmp.biWidth);printf("\n bmp biHeight: %d",bmp.biHeight);printf("\n bmp biplans: %u",bmp.biPlanes);printf("\n bmp biBitCount: %u",bmp.biBitCount);printf("\n bmp biCompression: %u",bmp.biCompression);printf("\n bmp biSizeImage: %u",bmp.biSizeImage);printf("\n bmp biXPelsPerMeter: %d",bmp.biXPelsPerMeter);printf("\n bmp biYPelsPerMeter: %d",bmp.biYPelsPerMeter);printf("\n bmp biClrUsed: %u",bmp.biClrUsed);printf("\n bmp biClrImportant: %u\n",bmp.biClrImportant);line_byte=(bmp.biWidth*bmp.biBitCount/8+3)/4*4; //获得图像数据每行的数据个数//printf("dfsa%u",bmp.line_byte);//bmp.imagedata=NULL;imagedata=(unsigned char*)malloc(bmp.biSizeImage);fseek(file,(long)bmp.bfOffBits,0);fread(imagedata,sizeof(unsigned char),bmp.biSizeImage,file);fclose(file);
}
//保存图像
void image_save(FILE *file)
{int times=3; //输入文件名次数。char bmp_name[10]; //文件名//int i; //记录数据区个数printf("\nplease enter a file name for writeing:");do{if (times<3){printf("\nplease enter a file name for writeing again:");}fflush(stdin);gets(bmp_name);printf("\n%s",bmp_name);file=fopen(bmp_name,"wb+"); //打开一个文件进行读写操作。--times;if (file==NULL){printf("\nerror opening %s for writing",bmp_name);}else{break;}}while(times!=0);if (times==0){printf("\nsorry, shutdown!");exit(1);}//写文件头printf("\n%s",bmp_name);fseek(file,0L,0); //图像文件类型fwrite(&(bmp.bfType),sizeof(short),1,file);printf("\n bmp tpye: %d",bmp.bfType);fseek(file,2L,0); //图像文件大小fwrite(&(bmp.bfSize),sizeof(int),1,file);printf("\n bmp size: %d",bmp.bfSize);fseek(file,6L,0); //图像文件保留字1fwrite(&(bmp.bfReserved1),sizeof(short),1,file);printf("\n bmp reserved1: %d",bmp.bfReserved1);fseek(file,8L,0); //图像文件保留字2fwrite(&(bmp.bfReserved2),sizeof(short),1,file);printf("\n bmp reserved2: %d",bmp.bfReserved2);fseek(file,10L,0);//数据区的偏移量fwrite(&(bmp.bfOffBits),sizeof(short),1,file);printf("\n bmp offBits: %d",bmp.bfOffBits);fseek(file,14L,0);//文件头结构大小fwrite(&(bmp.biSize),sizeof(int),1,file);printf("\n bmp bisize: %d",bmp.biSize);fseek(file,18L,0);//图像的宽度fwrite(&(bmp.biWidth),sizeof(int),1,file);printf("\n bmp biWidth: %d",bmp.biWidth);fseek(file,22L,0);//图像的高度fwrite(&(bmp.biHeight),sizeof(int),1,file);printf("\n bmp biHeight: %d",bmp.biHeight);fseek(file,24L,0);//图像的面数fwrite(&(bmp.biPlanes),sizeof(short),1,file);printf("\n bmp biplans: %d",bmp.biPlanes);fseek(file,28L,0);//图像一个像素的字节数fwrite(&(bmp.biBitCount),sizeof(short),1,file);printf("\n bmp biBitCount: %d",bmp.biBitCount);fseek(file,30L,0);//图像压缩信息fwrite(&(bmp.biCompression),sizeof(short),1,file);printf("\n bmp biCompression: %d",bmp.biCompression);fseek(file,34L,0);//图像数据区的大小fwrite(&(bmp.biSizeImage),sizeof(int),1,file);printf("\n bmp biSizeImage: %d",bmp.biSizeImage);fseek(file,38L,0);//水平分辨率fwrite(&(bmp.biXPelsPerMeter),sizeof(int),1,file);printf("\n bmp biXPelsPerMeter: %d",bmp.biXPelsPerMeter);fseek(file,42L,0);//垂直分辨率fwrite(&(bmp.biYPelsPerMeter),sizeof(int),1,file);printf("\n bmp biYPelsPerMeter: %d",bmp.biYPelsPerMeter);fseek(file,46L,0);//颜色索引数fwrite(&(bmp.biClrUsed),sizeof(int),1,file);printf("\n bmp biClrUsed: %d",bmp.biClrUsed);fseek(file,50L,0);//重要颜色索引数fwrite(&(bmp.biClrImportant),sizeof(int),1,file);printf("\n bmp biClrImportant: %d\n",bmp.biClrImportant);fseek(file,(long)(bmp.bfOffBits),0);fwrite(imagedata,sizeof(unsigned char),bmp.biSizeImage,file);fclose(file);
}

3.bmp图片像素的处理(灰度化、二值化、反相、抽取RGB通道以及改变图像亮度)

//pixProcess.c文件
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include"image.h"
//灰度化
void image_gray()
{int i,j;unsigned char tmp;for (i=0;i<bmp.biHeight;i++){for (j=0;j<line_byte/3;j++){tmp=0.11*(*(imagedata+i*line_byte+j*3+0))+0.59*(*(imagedata+i*line_byte+j*3+1))+0.3*(*(imagedata+i*line_byte+j*3+2));imagedata[i*line_byte+j*3+0]=tmp;imagedata[i*line_byte+j*3+1]=tmp;imagedata[i*line_byte+j*3+2]=tmp;//printf("\nnidsfh%d %d",i,j);}}
}
//二值化
void image_binarization()
{int i,j;for (i=0;i<bmp.biHeight;i++){for (j=0;j<line_byte;j++){if ((*(imagedata+i*line_byte+j))<128){imagedata[i*line_byte+j]=0;}else{imagedata[i*line_byte+j]=255;}}}
}
//反相
void image_opposite()
{int i,j;for (i=0;i<bmp.biHeight;i++){for (j=0;j<line_byte;j++){imagedata[i*line_byte+j]=abs(255-imagedata[i*line_byte+j]);}}
}
//抽取RGB通道
void image_channel()
{int i,j;char rgb;printf("\nplease enter a char(r/g/b): ");fflush(stdin);scanf("%c",&rgb);if (rgb=='b'){for (i=0;i<bmp.biHeight;i++){for (j=0;j<line_byte/3;j++){imagedata[i*line_byte+3*j+1]=0;imagedata[i*line_byte+3*j+2]=0;}}}else if(rgb=='g'){for (i=0;i<bmp.biHeight;i++){for (j=0;j<line_byte/3;j++){imagedata[i*line_byte+3*j]=0;imagedata[i*line_byte+3*j+2]=0;}}}else{for (i=0;i<bmp.biHeight;i++){for (j=0;j<line_byte/3;j++){imagedata[i*line_byte+3*j]=0;imagedata[i*line_byte+3*j+1]=0;}}}
}
//改变图像亮度
void image_bright()
{int level;int i,j;printf("\n please enter the level of brightness[-255 to 255] :");fflush(stdin);scanf("%d",&level);for (i=0;i<bmp.biHeight;i++){for (j=0;j<line_byte;j++){if (level>=0){if ((imagedata[i*line_byte+j]+level)>255)imagedata[i*line_byte+j]=255;elseimagedata[i*line_byte+j]+=level;}else{if ((imagedata[i*line_byte+j]-abs(level))<0)imagedata[i*line_byte+j]=0;elseimagedata[i*line_byte+j]+=level;}}}
}

4.

【图像处理基础】C语言对bmp图片进行处理相关推荐

  1. C语言实现BMP图片的放大缩小

    C语言实现BMP图片的放大缩小 BMP图片简介:BMP图片是windows操作系统中的标准图像文件格式,可以分为两类:设备相关位图(DDB)和设备无关位图(DIB),使用广泛.它采用位映射存储格式,除 ...

  2. C语言实现bmp图片裁剪

    C语言实现bmp图片裁剪 bmp图片如何储存的,在这篇文章中有很好的介绍:https://blog.csdn.net/weixin_46987028/article/details/109171867 ...

  3. 【数据压缩】C语言实现bmp图片序列生成yuv视频

    一.实验要求 1.解析BMP格式文件,获取图像信息 2.转化BMP图像为YUV格式的图像 3.多张BMP图像,转化为YUV视频 二.实验内容 1.获取图片 获取(540*720)的bmp图片若干: 2 ...

  4. c语言编程读取bmp文件数据,c语言读取BMP图片的RGB数据

    BMP图片是位图(bitmap),一般未压缩,要读取BMP文件只要知道它的文件结构就可以了,具体格式可以百度或者google,就不多说了,几个重要的点在代码里面有注释. /** c语言读取位图信息 * ...

  5. 通过c语言访问bmp图片文件修改图片信息

    在c语言中访问设定路径下的bmp图片文件,修改图片中像素RGB信息,从而达到修改图片中颜色的目的. 本程序实现的是将原图片中蜡笔小新的眼睛和嘴巴改了. #include <head.h> ...

  6. c语言给bmp图片加水印

    在 C 语言中,可以使用如下的步骤来给 BMP 图片添加水印: 打开图片文件,并读取图片文件头信息. 读取图片像素数据,并将其存储在内存中. 在内存中修改图片像素数据,实现对图片的修改. 将修改后的图 ...

  7. c语言给bmp图片加滤镜,图片编辑器PixelStyle: 图像处理,滤镜特效

    图片编辑器PixelStyle: 图像处理,滤镜特效 支持系统 OS X 10.8 价格 0 下载次数 590 官方网站 *不要错过"超级抠图",一键抠图工具,从此告别PS的蜗牛抠 ...

  8. C语言读写BMP图片(附Github下载链接和视频讲解地址)

    BMP全称(BITMAP)是微软WINDOWS系统默认使用的一种通用图片数据存储格式,特点是结构清晰,解析简单和多平台支持广泛.其文件结构由文件头结构体,文件信息头结构体,调色板(可选),以及图片数据 ...

  9. c语言给bmp图片加滤镜,关于BMP位图透明通道的详解制作教程, 教你输出透明的BMP位图...

    我是sjmhiex啊月谢谢大家的支持  百度贴吧:sjmhiex吧 QQ群:243153684 BMP支持透明比较常见的方法有两种: 一种是32位图,直接就可以是透明的,还可以是半透明效果,一般都是用 ...

最新文章

  1. php不能加载oci8,无法加载动态库'oci8.so'(PHP 7.2)
  2. 计算机二级关于数据结构的题目,计算机二级MS OFFICE 练习题(一五三)
  3. mysql 压力测试知乎_MySQL 对于千万级的大表要怎么优化? - MySQL
  4. python 形参 拷贝_Day124:python中的变量、引用、拷贝
  5. 找规律 百度之星资格赛 1001 大搬家
  6. oracle 产看执行计划_ODBA 技能SPM计划
  7. Linux故障解决(2)——使用yum安装netcat 及报错问题解决
  8. 浮点数表示(记录学习过程中遇到的问题)
  9. JAVA环境变量安装
  10. keras可视化模型训练过程
  11. 编程基本功:顾名思义是可视化编程的要求
  12. MSDEV.EXE 版本
  13. 苹果iPod设计及商业操作内幕
  14. 手机IMSI号码编码规则表
  15. 中南大学数字中南、电信校园网无法弹出验证界面解决方法
  16. 解决Tomcat运行内存不足问题
  17. python字典转字符串并输出其长度_从Python中SOX的字符串输出中获取字典中的可用数据...
  18. 编译可在Android上运行的依赖库(二):gettext库
  19. Java 版 Prim 算法求最小生成树
  20. linux操作系统下 c语言编程入门

热门文章

  1. Arduino模拟刷卡门禁
  2. 视觉SLAM中,本质矩阵、基础矩阵、单应性矩阵自由度和秩分析
  3. dna序列分类数学建模matlab,MATLAB软件课程设计---MATLAB实现DNA序列的分类识别
  4. 使用篇丨链路追踪(Tracing)很简单:链路实时分析、监控与告警
  5. win10安装ensp启动40_升级win10后 eNSP AR启动失败错误代码40的终极思路和解决办法...
  6. 基于51单片机电子秒表倒计时器proteus仿真程序原理图PCB数码管显示语言播报
  7. 【JS基础-1】JavaScript语言简单介绍(语法、变量、数组、流程控制语句、函数、对象和事件)
  8. TCP/IP编程:DNS域名解析
  9. LeetCode 292 Nim Game(Nim游戏)
  10. 变频器电路图 MD320T400G变频自绘电路图