您好,欢迎来到保捱科技网。
搜索
您的当前位置:首页matlab中读取PPM格式的图像程序

matlab中读取PPM格式的图像程序

来源:保捱科技网


function [I,w,h,level] = ppmread(path)

%*****************************************************

% Title: Ppmread

% Input Parameter: path of the ppm file

% Description: This file reads .ppm file

% open the file in read mode

f= fopen(path,'r');

A = 0 ; % Ascii flag

% ignore the comments in the file

a = fscanf(f,'%s',1);

while(a(1)=='#')

a = fscanf(f,'%s',1);

end

% check magic number

if ((strcmp(a,'P3')==0) &(strcmp(a,'P6')==0))

while(a(1)=='#')

a = fscanf(f,'%s',1);

end

display('its not ppm file');

else

display('its ppm file');

if(strcmp(a,'P3'))

A = 1;

end

a = fscanf(f,'%s',1);

while(a(1) == '#')

b = fgets(f); % throw away the comments line

a= fscanf(f,'%s',1); % read first character of next line

end

w = str2num(a); % width of image

a= fscanf(f,'%s',1);

while(a(1) == '#')

b = fgets(f);

a= fscanf(f,'%s',1);

end

h = str2num(a); % hight of image

a= fscanf(f,'%s',1);

while(a(1) == '#')

b = fgets(f); % throw away the comments line

a= fscanf(f,'%s',1);

end

level = str2num(a); % colour levels

if (A == 1)

for i = 1:h

for j = 1:w

I(i,j,1) = fscanf(f,'%i',1); % Red

I(i,j,2) = fscanf(f,'%i',1); % Green

I(i,j,3) = fscanf(f,'%i',1); % Blue

end

end

else

% Skip one more char

fread(f,1);

% Now read the matrix

Arr = uint8(fread(f));

index = 0;

for i = 1:1:h

for j = 1:w

index = index+1;

I(i,j,1) = Arr(index);

index = index+1;

I(i,j,2) = Arr(index);

index = index+1;

I(i,j,3) = Arr(index);

end

end

end

end

因篇幅问题不能全部显示,请点此查看更多更全内容

Copyright © 2019- baoaiwan.cn 版权所有 赣ICP备2024042794号-3

违法及侵权请联系:TEL:199 18 7713 E-MAIL:2724546146@qq.com

本站由北京市万商天勤律师事务所王兴未律师提供法律服务