怎样用c读取pgm格式的图像文件

1062 2008-10-26 10:28:19
有c源码的最好
现在需要读取pgm格式文件,将其转换为灰度值矩阵
如一个112×92的图像
结果应是一个112×92的矩阵,其中元素是原图像的灰度值
...全文
763 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
碎片球球 2012-06-20
  • 打赏
  • 举报
回复
http://blog.pfan.cn/miaowei/50921.html

http://download.csdn.net/download/zt0621/2366090

或者public void LoadPgmFile(string path)
...{
fileStream = new FileStream(path, FileMode.Open);
stringReader = new StreamReader(fileStream);

string typeHead = stringReader.ReadLine();
string sizeHead = stringReader.ReadLine();
if (typeHead[1] == '5')
...{
data.Type = PGMType.P5;
}
else
...{
if (typeHead[1] == '2')
...{
data.Type = PGMType.P2;
}
else
...{
data.Type = PGMType.UNKOWN;
throw new UnKonwPGMTypeException ();
return;
}
}
int midPosit = 0;
for (int posit = 0; posit < sizeHead.Length; posit++)
...{
if (sizeHead[posit] == ' ')
...{
midPosit = posit;
break;
}
}

if (midPosit == 0)
...{
data.Width = Convert.ToUInt32(sizeHead);
sizeHead = null;
sizeHead = stringReader.ReadLine();
data.Height = Convert.ToUInt32(sizeHead);
}
else
...{
StringBuilder sizeHead2 = new StringBuilder();
for(int posit = 0 ; posit < midPosit ; posit++)
...{
sizeHead2.Append(sizeHead[posit]);
}
data.Width = Convert.ToUInt32(sizeHead2.ToString());

sizeHead2 = new StringBuilder();
for (int posit = midPosit; posit < sizeHead.Length; posit++)
...{
sizeHead2.Append(sizeHead[posit]);
}
data.Height = Convert.ToUInt32(sizeHead2.ToString());
}

string grayMax = stringReader.ReadLine();
data.GrayMax = Convert.ToByte(grayMax);


if (data.Type == PGMType.P5)
...{
binaryReader = new BinaryReader(fileStream);

data.PgmByteData = new byte[data.PixelCount];
binaryReader.Read(data.PgmByteData, 0, data.PixelCount);

stringReader.Close();
binaryReader.Close();
}

if (data.Type == PGMType.P2)
...{

data.PgmStringData = new string[data.Height,data.Width];
data.PgmByteData = new byte[data.PixelCount];
char[] temp0, temp;
int lineLength = 0;

for (int row = 0; row < data.Height; row++)
...{
for (int col = 0; col < data.Width; col++)
...{


if (row == 3&& col == 0x12)
col = col;
temp0 = new char[4];
stringReader.Read(temp0, 0, 4);
lineLength += 4;

int posit = 0;
for (int i = 0; i < 4; i++)
...{
if (temp0[i] == ' ')
...{
posit = i;
break;
}
}

temp = new char[posit];
for (int i = 0; i < posit; i++)
...{
temp[i] = temp0[i];
}

data.PgmStringData[row, col] = new String(temp);
data.PgmByteData[row * data.Width + col] =
Convert.ToByte(data.PgmStringData[row, col]);



if (lineLength >= 70)
...{
stringReader.Read();
lineLength = 0;
if (col == (data.Width - 1))
continue;
}
if (col == (data.Width - 1))
...{
stringReader.Read();
lineLength = 0;
}
}
}
}





fileStream.Close();

}
1062 2008-10-30
  • 打赏
  • 举报
回复
哪位大虾帮帮忙啊

33,311

社区成员

发帖
与我相关
我的任务
社区描述
C/C++ 新手乐园
社区管理员
  • 新手乐园社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧