I got wrong result when I read some bitmap file!!!
2 次查看(过去 30 天)
显示 更早的评论
I want to read bitmap file by fread in Matlab. For some bmp file, the result is correct, but for others, the result is wrong.
Attached are the code, bitmap files and result.doc. For Desert.bmp, the code can get correct result.
The code is following:
A= fopen('wrong.bmp');
type = fread(A,2,'char=>char')'; % BM
size = fread(A,1,'int32'); %The size of the BMP file in bytes
reserved = fread(A,1,'int32');%Reserved
offset= fread(A,1,'int32');%offset
bisize = fread(A,1,'int32'); %the size of this header
width= fread(A,1,'long');%width
height= fread(A,1,'long'); %height
planes = fread(A,1,'int16'); %planes
bitcount = fread(A,1,'int16'); %BitCount
compression= fread(A,1,'int32');%Compression
sizelmage= fread(A,1,'int32'); %The size of bitmap data
xpelspermeter = fread(A,1,'long'); %the horizontal resolution of the image
ypelspermeter= fread(A,1,'long'); %the vertical resolution of the image
clrused= fread(A,1,'int32'); %the number of colors in the color palette
clrlmportant=fread(A,1,'int32');%the number of important colors used
b=zeros(height,width,'uint8');
g=zeros(height,width,'uint8');
r=zeros(height,width,'uint8');
for i=1:height
for j=1:width
b(i,j)=fread(A,1,'uint8');
g(i,j)=fread(A,1,'uint8');
r(i,j)=fread(A,1,'uint8');
end
end
%extra pixels for 'test.bmp'
%----------------------------
% t=zeros(1,10);
% for k=1:10
% t(k)=fread(A,1,'uint8');
% end
%----------------------------
rgb=zeros(height,width,3,'uint8');
rgb(:,:,1)=flipud(r);
rgb(:,:,2)=flipud(g);
rgb(:,:,3)=flipud(b);
imshow(rgb);
///////////////////////////////////////////////////////////////////////////////////////////
However, for test.bmp, a 5*6 image, the result is wrong. The code get r, g, b matrix as follows.
r =
172 131 226 227 224 221
72 0 163 172 177 176
0 194 192 191 155 102
228 178 98 47 34 51
193 142 62 22 19 34
g =
199 160 0 163 168 171
83 0 197 203 206 203
27 215 209 204 167 112
0 182 139 64 20 8
219 164 82 40 38 50
b =
216 178 0 198 202 203
78 45 222 224 224 220
48 0 163 165 165 131
0 209 165 88 40 29
236 177 89 44 36 47
The extra pixels beside r, g, b matrix.
201 170 218 197 166 226 201 169 0 0
The correct r, g, b matrix as follows.
r
163 168 171 170 166 169
163 172 177 176 172 131
163 165 165 131 78 45
182 139 64 20 8 27
193 142 62 22 19 34
g
198 202 203 201 197 201
197 203 206 203 199 160
194 192 191 155 102 72
209 165 88 40 29 48
219 164 82 40 38 50
b
226 227 224 221 218 226
222 224 224 220 216 178
215 209 204 167 112 83
228 178 98 47 34 51
236 177 89 44 36 47
///////////////////////////////////////////////////////////////////////////////////////////
when read the following bmp file, I will get a wrong result.
the wrong result is folowing
0 个评论
回答(1 个)
另请参阅
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!