资源预览内容
第1页 / 共16页
第2页 / 共16页
第3页 / 共16页
第4页 / 共16页
第5页 / 共16页
第6页 / 共16页
第7页 / 共16页
第8页 / 共16页
第9页 / 共16页
第10页 / 共16页
亲,该文档总共16页,到这儿已超出免费预览范围,如果喜欢就下载吧!
资源描述
位图文件信息的提取和二值化处理实验步骤: 1.拷贝MinGW文件夹至C:(路径为C:MinGW)2.编辑setc.bat文件,然后运行此批处理以设置路径。3.编辑hdr.h 和hdr.c文件4.编辑bmphdr.c文件,然后在当前文件路径下,使用DOS命令:gcc -c hdr.c /编译,生成hdr.o目标文件gcc -c bmphdr.c /编译,生成bmphdr.o目标文件gcc -o bmphdr.exe hdr.o bmphdr.o/链接,生成bmphdr.exe执行文件使用bmphdr.exe来提取某一个bmp文件的头信息,例如: bmphdr test.bmp5.编辑 ez.c文件(用于图像的二值化处理),然后gcc -c ez.c /编译,生成ez.o目标文件gcc -o ez.exe hdr.o ez.o /链接,生成ez.exe执行文件使用 ez.exe文件来对某一个bmp文件进行二值化处理。例如:ez test.bmp result.bmp 108 (对test.bmp文件 进行二值化处理,阈值为108,处理的结果为result.bmp文件)hdr.h文件内容:#ifndef _HDR_H_#define _HDR_H_struct bmphdr char signature2;int size;short reserved2;int offset;int hdr_size;int width;int height;short nr_planes;short bits_per_pixel;int compress_type;int data_size;int resol_hori;int resol_vert;int nr_colors;int important_color;char info1024;struct bmphdr *get_header(char filename);#endifhdr.c文件内容:#include #include #include hdr.hstruct bmphdr *get_header(char filename)FILE *fp;struct bmphdr *hdr; fp = fopen(filename, rb);if (!fp) printf(File open error or such file does not exist!n);return NULL;hdr = (struct bmphdr *)malloc(sizeof(struct bmphdr);fread(hdr-signature, 2, 1, fp);if (hdr-signature0 != B | hdr-signature1 != M) printf(Not a bmp file!n);return NULL;fread(&hdr-size, 4, 1, fp);fread(hdr-reserved, 4, 1, fp);fread(&hdr-offset, 4, 1, fp);fread(&hdr-hdr_size, 4, 1, fp);fread(&hdr-width, 4, 1, fp);fread(&hdr-height, 4, 1, fp);fread(&hdr-nr_planes, 2, 1, fp);fread(&hdr-bits_per_pixel, 2, 1, fp);fread(&hdr-compress_type, 4, 1, fp);fread(&hdr-data_size, 4, 1, fp);fread(&hdr-resol_hori, 4, 1, fp);fread(&hdr-resol_vert, 4, 1, fp);fread(&hdr-nr_colors, 4, 1, fp);fread(&hdr-important_color, 4, 1, fp); if (hdr-offset 54) fread(&hdr-info, 1024, 1, fp); fclose(fp);return hdr;bmphdr.c文件内容:#include struct bmphdr char signature2;int size;short reserved2;int offset;int hdr_size;int width;int height;short nr_planes;short bits_per_pixel;int compress_type;int data_size;int resol_hori;int resol_vert;int nr_colors;int important_color; header;int main(int argc, char *argv)FILE *fp;if (argc != 2) printf(Usage: %s n, argv0);exit(1);fp = fopen(argv1, r);if (!fp) printf(File open error or such file does not exist!n);exit(1);fread(header.signature, 2, 1, fp);if (header.signature0 != B | header.signature1 != M) printf(Not a bmp file!n);exit(1);fread(&header.size, 4, 1, fp);fread(header.reserved, 4, 1, fp);fread(&header.offset, 4, 1, fp);fread(&header.hdr_size, 4, 1, fp);fread(&header.width, 4, 1, fp);fread(&header.height, 4, 1, fp);fread(&header.nr_planes, 2, 1, fp);fread(&header.bits_per_pixel, 2, 1, fp);fread(&header.compress_type, 4, 1, fp);fread(&header.data_size, 4, 1, fp);fread(&header.resol_hori, 4, 1, fp);fread(&header.resol_vert, 4, 1, fp);fread(&header.nr_colors, 4, 1, fp);fread(&header.important_color, 4, 1, fp);fclose(fp);printf(signature %c%cn, header.signature0, header.signature1);printf(size %dn, header.size);printf(offset %dn, header.offset);printf(hdr_size %dn, header.hdr_size);printf(width %dn, header.width);printf(height %dn, header.height);printf(nr_planes %dn, header.nr_planes);printf(bits_per_pixel %dn, header.bits_per_pixel);printf(compress_type %dn, header.compress_type);printf(data_size %dn, header.data_size);printf(resol_hori %dn, header.resol_hori);printf(resol_vert %dn, header.resol_vert);printf(nr_colors %dn, header.nr_colors);printf(important_color %dn, header.important_color);printf(n);return 0;二值化程序 ez.c 文件内容:#include #include #include #include hdr.hstruct bmphdr *hdr;unsigned char *bitmap, *to;char buf2048;int main(int argc, char *argv)int i, j, k, nr_pixels; FILE *fp, *fpnew;unsigned g;if (argc != 4) printf(Usage: %s n, argv0);exit(1);hdr = get_header(argv1);if (!hdr) exit(1);fp = fopen(argv1, rb);if (!fp) printf(File open error!n);exit(1);fseek(fp, hdr-offset, SEEK_SET);nr_pixels = hdr-width * hdr-height;bitmap = malloc(nr_pixels);fread(bitmap, nr_pixels, 1, fp);fclose(fp);k = atoi(argv3);to = malloc(nr_pixels);memset(to, 0, nr_pixels);for (i = 0; i nr_pixels; i+) toi = bitm
收藏 下载该资源
网站客服QQ:2055934822
金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号