Read the Header of bitmap

23 vues (au cours des 30 derniers jours)
Tina
Tina le 11 Nov 2021
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 commentaires
Walter Roberson
Walter Roberson le 11 Nov 2021
Modifié(e) : Walter Roberson le 11 Nov 2021
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)
Tina
Tina le 11 Nov 2021
I wasn't trying to read the bmp file rather I was trying to open the header of the bmp file . I used unit8 which you mentioned earlier but that returns the image data . But do you know how we can load coloured/RGB image by using fread I tried by using Fread(file name ,[520 520],unit8 ,'b') RGB image size is 520*520*3 The 3 saves the bytes of colour of the pixel .since it is not used in size it gives grey image Any idea how to solve this proble

Connectez-vous pour commenter.

Réponse acceptée

yanqi liu
yanqi liu le 11 Nov 2021
clc; clear all; close all;
bmp = fopen('common_demos.bmp','rb');
type = fread(bmp,2,'char')
type = 2×1
66 77
bmpsize = fread(bmp,1,'long')
bmpsize = 12656
bfReserved1and2 = fread(bmp,1,'long')
bfReserved1and2 = 0
bfOffBits = fread(bmp,1,'long')
bfOffBits = 54
biSize = fread(bmp,1,'long')
biSize = 40
biWidth = fread(bmp,1,'long')
biWidth = 66
biHeight = fread(bmp,1,'long')
biHeight = 63
biPlanes = fread(bmp,1,'short')
biPlanes = 1
biBitCount = fread(bmp,1,'short')
biBitCount = 24
biCompression = fread(bmp,1,'long')
biCompression = 0
biSizeImage = fread(bmp,1,'long')
biSizeImage = 12602
biXPelsPerMeter = fread(bmp,1,'long')
biXPelsPerMeter = 2834
biYPelsPerMeter = fread(bmp,1,'long')
biYPelsPerMeter = 2834
biClrUsed = fread(bmp,1,'long')
biClrUsed = 0
biClrImportant = fread(bmp,1,'long')
biClrImportant = 0
fclose(bmp);
  3 commentaires
Walter Roberson
Walter Roberson le 11 Nov 2021
'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.
Tina
Tina le 11 Nov 2021
Oh okay . Thank you

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Get Started with MATLAB dans Help Center et File Exchange

Produits


Version

R2019a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by