当前位置:文档之家› 计算机图像处理实验

计算机图像处理实验

位图文件信息的提取和二值化处理实验步骤: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 signature[2];int size;short reserved[2];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 info[1024];};struct bmphdr *get_header(char filename[]);#endifhdr.c文件内容:#include <stdio.h>#include <memory.h>#include "hdr.h"struct 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->signature[0] != 'B' || hdr->signature[1] != '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 <stdio.h>struct bmphdr {char signature[2];int size;short reserved[2];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 <filename>\n", argv[0]);exit(1);}fp = fopen(argv[1], "r");if (!fp) {printf("File open error or such file does not exist!\n");exit(1);}fread(header.signature, 2, 1, fp);if (header.signature[0] != 'B' || header.signature[1] != '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(&press_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%c\n", header.signature[0], header.signature[1]);printf("size %d\n", header.size);printf("offset %d\n", header.offset);printf("hdr_size %d\n", header.hdr_size);printf("width %d\n", header.width);printf("height %d\n", header.height);printf("nr_planes %d\n", header.nr_planes);printf("bits_per_pixel %d\n", header.bits_per_pixel);printf("compress_type %d\n", press_type);printf("data_size %d\n", header.data_size);printf("resol_hori %d\n", header.resol_hori);printf("resol_vert %d\n", header.resol_vert);printf("nr_colors %d\n", header.nr_colors);printf("important_color %d\n", header.important_color);printf("\n");return 0;}二值化程序ez.c 文件内容:#include <stdio.h>#include <stdlib.h>#include <memory.h>#include "hdr.h"struct bmphdr *hdr;unsigned char *bitmap, *to;char buf[2048];int main(int argc, char *argv[]){int i, j, k, nr_pixels;FILE *fp, *fpnew;unsigned g;if (argc != 4) {printf("Usage: %s <file_from> <file_to> <threshold>\n", argv[0]);exit(1);}hdr = get_header(argv[1]);if (!hdr) exit(1);fp = fopen(argv[1], "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(argv[3]);to = malloc(nr_pixels);memset(to, 0, nr_pixels);for (i = 0; i < nr_pixels; i++)to[i] = bitmap[i] > (unsigned char)k ? 255 : 0;fpnew = fopen(argv[2], "wb+");if (!fpnew) {printf("File create error!\n");exit(1);}fwrite(hdr->signature, 2, 1, fpnew);fwrite(&hdr->size, 4, 1, fpnew);fwrite(hdr->reserved, 4, 1, fpnew);fwrite(&hdr->offset, 4, 1, fpnew);fwrite(&hdr->hdr_size, 4, 1, fpnew);fwrite(&hdr->width, 4, 1, fpnew);fwrite(&hdr->height, 4, 1, fpnew);fwrite(&hdr->nr_planes, 2, 1, fpnew);fwrite(&hdr->bits_per_pixel, 2, 1, fpnew);fwrite(&hdr->compress_type, 4, 1, fpnew);fwrite(&hdr->data_size, 4, 1, fpnew);fwrite(&hdr->resol_hori, 4, 1, fpnew);fwrite(&hdr->resol_vert, 4, 1, fpnew);fwrite(&hdr->nr_colors, 4, 1, fpnew);fwrite(&hdr->important_color, 4, 1, fpnew);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;}直方图均衡化直方图均衡化实质上是减少图象的灰度级以换取对比度的加大例如:假设原图的灰度分布级为126(最大为256,也就是从0到255的级上的灰度都有或多或少的出现),经过直方图均衡化后,灰度分布级别将会小于126。

相关主题