作者relaxpl (购购购!)
看板C_and_CPP
标题[问题] 读8bit 512*512bmp图档
时间Mon Oct 5 19:43:22 2009
小弟想把lena的图片做connect component
写到後来发现图片怎样都会有斜斜奇怪的切线
後来写一段把图片分成五层试试看
发现竟然还是有那样奇怪的切线
这是我读写的部份和测试的部份
FILE *fp;
FILE *ans;
unsigned char image[512][512];
unsigned char cc[512][512];
unsigned char head[172];
unsigned char palette[1024];
unsigned char raw_image[512*512];
int color[256]; //纪录各灰阶有多少pixel
int i,j,k;
fp = fopen("Lena.bmp", "rb");
// 读 bmp 灰阶标头档
fread(head, 172, 1, fp);
fread(palette, 1024, 1, fp);
// 读 bmp 影像资料
fread(raw_image, 512*512, 1, fp);
k=0;
for (i=0; i< 512; i++) {
for (j=0; j< 512; j++) {
image[i][j] = raw_image[k];
k++;
}} //读到二维阵列
for(i=0;i<512;i++){
for (j=0; j< 512; j++) {
if(i<100) image[i][j]=50;
else if(100<i&&i<200) image[i][j]=100;
else if(200<i&&i<300) image[i][j]=150;
else if(300<i&&i<400) image[i][j]=200;
else image[i][j]=250;
}
} //test 把图片分成五层
k=0;
for (i=0; i< 512; i++) {
for (j=0; j< 512; j++) {
raw_image[k]=image[i][j];
k++;
}
} //写回raw
ans = fopen("Lena_after.bmp","wb");
fwrite(head, 1, 172, ans);
fwrite(palette,1, 1024, ans);
fwrite(raw_image,1,512*512,ans);
他的左下角有一小块平行的线好像没读到一样有怪怪的颜色
还有中间左边有一条垂直的奇怪切线
请问是我读档byte算错或是怎样的问题吗:(
希望有高手能解答
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 140.112.251.43
1F:推 VictorTom:个人推测, 你是不是忘了处理8bpp的调色盘了?? 8bpp的BMP 10/05 20:12
2F:→ VictorTom:在raw data前有一段是color table, raw data里记录的则 10/05 20:13
3F:→ VictorTom:是color index, 也就是说8bpp的BMP里的raw data不是灰阶 10/05 20:13
4F:→ VictorTom:值, 而是索引值; 你的灰阶lena可能只是刚好index对应成 10/05 20:14
5F:→ VictorTom:灰阶值而已:) 10/05 20:14
6F:→ VictorTom:也因为最前面少处理掉一个固定大小的调色盘, 所以後面全 10/05 20:15
7F:→ VictorTom:部scanline都shift了同样的大小; 至於调色盘的位置与它 10/05 20:15
8F:→ VictorTom:的layout, 请自己Wiki/google一下吧XD 10/05 20:15
9F:推 VictorTom:糟糕, 我看到code有处理pallete了, 看来是猜错了(逃Orz) 10/05 20:18
10F:→ VictorTom:BMP的档头有172 bytes这麽多吗?_? 10/05 20:22
11F:→ relaxpl:嗯嗯我也是在想是不是调色盘或是head算错... 10/05 20:22
12F:→ relaxpl:对耶bmp54而已... 172是老师给的另种格式...感谢感谢ˊˋ 10/05 20:24
13F:推 VictorTom:我刚查Wiki, BMP两部份档头加起来只有54 bytes啊?_? 10/05 20:24
14F:推 wa120:这...connected component不是长这样吧= = 10/05 20:35
15F:→ VictorTom:他还只有读档吧, 档头算错了所以光读档写出结果就不对; 10/05 21:30
16F:→ VictorTom:connected component应该是接着才要尝试去做吧@_@" 10/05 21:30
17F:推 joefaq:每个row都要是4的倍数 不够请补齐 10/05 22:08
※ 编辑: relaxpl 来自: 140.112.251.43 (10/05 22:15)
18F:推 VictorTom:可是照说Width是512, 一pixel 1 byte, 应该是不用 10/05 22:21
19F:→ VictorTom:padding的啊?_? 10/05 22:21