数字图像处理实验一(位图文件信息的提取和二值化处理)

上传人:mg****85 文档编号:34447870 上传时间:2018-02-24 格式:DOC 页数:7 大小:59.50KB
返回 下载 相关 举报
数字图像处理实验一(位图文件信息的提取和二值化处理)_第1页
第1页 / 共7页
数字图像处理实验一(位图文件信息的提取和二值化处理)_第2页
第2页 / 共7页
数字图像处理实验一(位图文件信息的提取和二值化处理)_第3页
第3页 / 共7页
数字图像处理实验一(位图文件信息的提取和二值化处理)_第4页
第4页 / 共7页
数字图像处理实验一(位图文件信息的提取和二值化处理)_第5页
第5页 / 共7页
点击查看更多>>
资源描述

《数字图像处理实验一(位图文件信息的提取和二值化处理)》由会员分享,可在线阅读,更多相关《数字图像处理实验一(位图文件信息的提取和二值化处理)(7页珍藏版)》请在金锄头文库上搜索。

1、数字图像处理实验一(位图文件信息的提取和二值化处理)实验平台:MinGW(Mini Gnu for Windows) ,里面包含 gcc for windows 的编译器,将其拷贝值某目录,然后设置环境变量(这里我设为 c:MinGW,通过一个批处理命令 setc.bat 实现)设置完成后,即可通过 gcc(g+)命令进行编译。(附:使用 VC+ 6.0 也可)相关实验文件下载地址:ftp:/218.193.154.238 /课件 /数字图像处理/实验步骤: 拷贝 MinGW 文件夹至 C: (路径为 C:MinGW) 编辑 setc.bat 文件,然后运行此批处理以设置路径。 编辑 hdr.

2、h 和 hdr.c 文件 编辑 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.bmp 编辑 ez.c 文件(用于图像的二值化处理),然后gcc -c ez.c /编译,生成 ez.o 目标文件gcc -o ez.exe hdr.o ez.o

3、/链接,生成 ez.exe 执行文件使用 ez.exe 文件来对某一个 bmp 文件进行二值化处理。例如:ez test.bmp result.bmp 108 (对 test.bmp 文件 进行二值化处理,阈值为 108,处理的结果为 result.bmp 文件)setc.bat 内容:echo offset msdir=C:MinGWset PATH=%msdir%bin;%PATH%set INCLUDE=%msdir%Include;%msdir%MFCInclude;%INCLUDE%set LIB=%msdir%Lib;%LIB%补充:如果以上批处理命令无效的话,可以:我的电脑-属性

4、-高级- 环境变量 编辑变量 path 增加 C: MinGWbin 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 importa

5、nt_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 bmphd

6、r *)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(fread(hdr-reserved, 4, 1, fp);fread(fread(fread(fread(fread(fread(fread(fread(fread(fread(fread(fread(if (hdr-offset 54)fread(fclose(fp);return hd

7、r;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

8、 *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(fread(header.reserved, 4, 1

9、, fp);fread(fread(fread(fread(fread(fread(fread(fread(fread(fread(fread(fread(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 %

10、dn, header.height);printf(nr_planes %dn, header.nr_planes);printf(bits_per_pixel %dn, header.bits_per_pixel);printf(compress_type %dn, press_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.

11、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

12、(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

13、(nr_pixels);memset(to, 0, nr_pixels);for (i = 0; i (unsigned char)k ? 255 : 0;fpnew = fopen(argv2, wb+);if (!fpnew) printf(File create error!n);exit(1);fwrite(hdr-signature, 2, 1, fpnew);fwrite(fwrite(hdr-reserved, 4, 1, fpnew);fwrite(fwrite(fwrite(fwrite(fwrite(fwrite(fwrite(fwrite(fwrite(fwrite(fwrite(fwrite(if (hdr-offset 54)fwrite(hdr-info, hdr-offset - 54, 1, fpnew);fwrite(to, nr_pixels, 1, fpnew);fclose(fpnew);free(hdr);free(bitmap);return 0;

展开阅读全文
相关资源
相关搜索

当前位置:首页 > 行业资料 > 教育/培训

电脑版 |金锄头文库版权所有
经营许可证:蜀ICP备13022795号 | 川公网安备 51140202000112号