数字图像处理第七次作业
南京大学电子科学与工程学院
weibo:@Fanchy_Lee
2012-4-16
1 图像相对信息冗余的计算
图像的相对信息冗余 R
表
关于同志近三年现实表现材料材料类招标技术评分表图表与交易pdf视力表打印pdf用图表说话 pdf
示一个图像经过压缩后数据体积减少的百分比,用公式来表示即:
R = 1� 1
C
其中的 C 代表数据压缩比,显然从这个公式可以看出相对冗余信息是针对具体的压缩方法而言
的。
根据我的 PC 上自带的压缩方法,Lenna 图像的体积为 257.1 KB,最好的压缩比为 1.4379:1,
采用的是 bz2 算法。故而对这一压缩算法来说,相对信息冗余为 0.3045。
2 游程编码的实现
游程编码的原理非常简单,简单地说就是用相同的数据及其长度来表示一串数据,这种编码
适合用来去除图像的空间冗余,也就是空间中一连串的相同像素点。尤其适合处理黑白图像,
传真即使用了这种编码。
我使用 C 语言实现了一个简单的可读的针对二值图像的游程编码工具。它可以将 8 位 *.bmp
图像编码为一个文本文件,文本文件中存放为数据长度串,用换行区别不同的行。代码如下:
1 #include
2 #include
3 #include
4 #include
5
6 typedef struct bmpfile_header {
7 uint32_t filesz;
8 uint16_t creator1;
9 uint16_t creator2;
10 uint32_t bmp_offset;
11 } FILEH;
12 typedef struct {
13 uint32_t header_sz;
14 int32_t width;
15 int32_t height;
16 uint16_t nplanes;
17 uint16_t bitspp;
18 uint32_t compress_type;
19 uint32_t bmp_bytesz;
20 int32_t hres;
21 int32_t vres;
22 uint32_t ncolors;
23 uint32_t nimpcolors;
24 } INFOH;
25
26 int main(int argc , char * argv[]){
27 INFOH ih ;
28 FILEH fh ;
29 FILE* pic ;
30 FILE* output ;
31 char* o_str ;
32 int i ,j , color, counts, color_tmp;
33 pic = fopen(argv[1], "r") ;
1
34 fseek(pic, 0x2, SEEK_SET);
35 fread(&fh.filesz, 4, 1, pic);
36 fprintf(stderr,"fh.filesz:0x%X\n",fh.filesz);
37 fseek(pic, 0xa, SEEK_SET);
38 fread(&fh.bmp_offset, 4, 1, pic) ;
39 fprintf(stderr,"fh.bmp_offset:0x%X\n",fh.bmp_offset);
40 fseek(pic, 0x1c, SEEK_SET);
41 fread(&ih.bitspp, 2, 1, pic);
42 fprintf(stderr,"ih.bitspp:0x%X\n",ih.bitspp);
43 fseek(pic, 0x12, SEEK_SET);
44 fread(&ih.width, 4, 1, pic);
45 fprintf(stderr,"ih.width:%d\n",ih.width);
46 fseek(pic, 0x16, SEEK_SET);
47 fread(&ih.height, 4, 1, pic);
48 fprintf(stderr,"ih.height:%d\n",ih.height);
49 fseek(pic, 0x1e, SEEK_SET);
50 fread(&ih.compress_type, 4, 1, pic);
51 fprintf(stderr,"ih.compresstype:0x%X\n",ih.compress_type);
52 o_str = malloc(strlen(argv[1] + 5));
53 strcpy(o_str, argv[1]);
54 strcat(o_str, ".rle");
55 output = fopen(o_str, "w");
56 free(o_str);
57
58
59 fseek(pic, fh.bmp_offset ,SEEK_SET);
60 for(i = 1 ; i < ih.height + 1; i++){
61 // fprintf(output, "%d:", i);
62 counts = 0 ;
63 for(j = 1 ; j < ih.width + 1; j ++){
64 fread(&color,1,1,pic);
65 if(color == color_tmp ){
66 counts ++ ;
67 if(j == ih.width ){
68 fprintf(output, "(%d,%d)", color, counts);
69 }
70 }else{
71 if(counts){
72 fprintf(output, "(%d,%d)",color_tmp, counts);
73 }
74 counts = 1 ;
75 }
76 color_tmp = color ;
77 }
78 putc('\n', output);
79 }
80 }
代码 1. rle.c
试验图像为图 ?? 中的图像。这个图像的体积为 136918 B,经过压缩得到的编码的第 26 行
(对应图像的底部第 10 行像素点)如代码 ?? 所示,所得文件的总大小为 34633 B,压缩比为
136918 : 34633 � 3:95。
1 (0,382)(255,2)(0,95)
代码 2. 游程编码后的文件
2
图 1. 试验图像
3