Read the Header of bitmap
23 次查看(过去 30 天)
显示 更早的评论
Hi can anyone guide me how to open a bitmap or bmp file header ? I have opened in hex editor but I need to open it by using matlab ?please guide
11 个评论
Walter Roberson
2021-11-11
编辑:Walter Roberson
2021-11-11
I am saying that if you were reading from a real bmp file then using fread with '*uint8' would give you back numeric values. You could display them in decimal if you want.
But the snapshot you posted is not a real bmp file: it is part of a hexadecimal dump of a bmp file. You need to decide which kind of file you need to process.
Consider the numeric value denoted by the word sixty-six. That is an abstract value that exists by itself independent of representation. It can be represented in multiple ways. One of the ways is 66, which is the character 6 followed by the character 6. Another way is 01000010 which is character 0 followed by character 1 then several character 0 and so on. Another way is 42 which is the character 4 followed by the character 2.
But in computer languages the character 0 is not the same as the numeric value zero. The character 0 is represented by the numeric value forty-eight, the character 1 is represented by the numeric value forty-nine and so on.
A real BMP file starts with a byte with numeric value sixty-six. Which happens to be also be the numeric value used to encode the character B (this was deliberate.)
But the file you posted a snapshot of... it starts with numeric value thirty-nine. Which is the same as the numeric value used to encode apostrophe. And then your file has numeric value forty-eight. Which is the encoding of the character 0, not the numeric value zero.
Someone has taken a binary file and converted it to characters that represent the hexadecimal form.
Consider that the numeric value one hundred and twenty three can be stored in a single byte: you could store it in a file that is a single byte. Or you could store it in a file as the sequence of characters 1 2 3 (which would be numeric values forty-nine fifty fifty-one inside the file) which would be a file of length three bytes. Or you could store it as the sequence of characters 7 B which would be numeric values fifty-five sixty-six in the file, a file of length two bytes. And the file you showed us would not use the single numeric value one hundred and twenty three: it would use the characters 7B (well, 7b really but I do not have the character code for b memorized. Ninety-eight I think it would be)
采纳的回答
yanqi liu
2021-11-11
clc; clear all; close all;
bmp = fopen('common_demos.bmp','rb');
type = fread(bmp,2,'char')
bmpsize = fread(bmp,1,'long')
bfReserved1and2 = fread(bmp,1,'long')
bfOffBits = fread(bmp,1,'long')
biSize = fread(bmp,1,'long')
biWidth = fread(bmp,1,'long')
biHeight = fread(bmp,1,'long')
biPlanes = fread(bmp,1,'short')
biBitCount = fread(bmp,1,'short')
biCompression = fread(bmp,1,'long')
biSizeImage = fread(bmp,1,'long')
biXPelsPerMeter = fread(bmp,1,'long')
biYPelsPerMeter = fread(bmp,1,'long')
biClrUsed = fread(bmp,1,'long')
biClrImportant = fread(bmp,1,'long')
fclose(bmp);
3 个评论
Walter Roberson
2021-11-11
'rb' is not documented. The 'b' will be ignored. Using 'r' is equivalent.
People mistakenly think they need to use 'rb' because in C you would use 'rb' for binary and 'r' for text files. In MATLAB it is 'r' for binary, and 'rt' for text files.
更多回答(0 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!