满意答案
china2109
2016.04.26
采纳率:58% 等级:7
已帮助:10209人
Matlab读取BMP文件代码,参考如下:
function BMPReader(strPathName)
fFile = fopen(strPathName, 'rb');
[data, count] = fread(fFile);
%offset
nOffset = 0;
for i = 1:4
nOffset = nOffset + bitshift(data(10+i), (i-1)*8);
end
%width
nWidth = 0;
for i = 1:4
nWidth = nWidth + bitshift(data(18+i), (i-1)*8);
end
%height
nHeight = 0;
for i = 1:4
nHeight = nHeight + bitshift(data(22+i), (i-1)*8);
end
%bit count
nBitCount = data(29) + bitshift(data(30), 8);
%bmp格式存储是从图片的下到上
img = zeros(nHeight, nWidth);
img = reshape(data(nOffset+1:end), nWidth, nHeight);
img = flipud(img'); %先转置,然后上下交换
imshow(img, [0, 255]);
%close file
fclose(fFile);
00分享举报