关于PGM格式图像读写
来源:互联网 发布:知乎回答问题在哪里 编辑:程序博客网 时间:2024/05/22 20:07
一、matlab读写pgm文件
from:http://www.ilovematlab.cn/thread-4960-1-1.html
读文件
%function disp_pgm(pgm_image_name)
%不支持文件中有注释
pgm_image_name='tmp.pgm';
f = fopen(pgm_image_name,'r');
if f == -1
error(['Could not open file ',pgm_image_name]);
end
[imgsize, num]=fscanf(f, 'P5\n%d\n%d\n255\n');
if num~=2,error('error num');end
image=[];
for h=1:imgsize(2)
image=[image fread(f,imgsize(1),'uint8')];
end
image=image.';
fclose(f);
imshow(image);
写文件
% Load image
% image = imread(imageFile);
% If you have the Image Processing Toolbox, you can uncomment the following
% lines to allow input of color images, which will be converted to grayscale.
if isrgb(image)
image = rgb2gray(image);
end
[rows, cols] = size(image);
% Convert into PGM imagefile, readable by "keypoints" executable
f = fopen('tmp.pgm', 'w');
if f == -1
error('Could not create file tmp.pgm.');
end
fprintf(f, 'P5\n%d\n%d\n255\n', cols, rows);
fwrite(f, image', 'uint8');
fclose(f);
二、IplImage图像转换为p2或p5格式的pgm图像
from:http://blog.163.com/czy_sysu/blog/static/130695599201010583910102/
int i,j; unsigned char temp=0;
from:http://www.ilovematlab.cn/thread-4960-1-1.html
读文件
%function disp_pgm(pgm_image_name)
%不支持文件中有注释
pgm_image_name='tmp.pgm';
f = fopen(pgm_image_name,'r');
if f == -1
end
[imgsize, num]=fscanf(f, 'P5\n%d\n%d\n255\n');
if num~=2,error('error num');end
image=[];
for h=1:imgsize(2)
end
image=image.';
fclose(f);
imshow(image);
写文件
% Load image
% image = imread(imageFile);
% If you have the Image Processing Toolbox, you can uncomment the following
%
if isrgb(image)
end
[rows, cols] = size(image);
% Convert into PGM imagefile, readable by "keypoints" executable
f = fopen('tmp.pgm', 'w');
if f == -1
end
fprintf(f, 'P5\n%d\n%d\n255\n', cols, rows);
fwrite(f, image', 'uint8');
fclose(f);
二、IplImage图像转换为p2或p5格式的pgm图像
from:http://blog.163.com/czy_sysu/blog/static/130695599201010583910102
void cvConvertImage2pgm(char* filename,IplImage* srcImage,int type)
{
int width=srcImage->width;
int height=srcImage->height;
FILE *pgmPict;
int rSize=width*height;
pgmPict=fopen(filename,"w");
if(type==2)
{
fprintf(pgmPict,"P2\n");
}else if(type==5)
{
fprintf(pgmPict,"P5\n");
}
fprintf(pgmPict,"%d %d \n%d\n",width,height,255);
if(type==5)
{
for( i=0;i<srcImage->height;i++)
{
for( j=0;j<srcImage->width;j++)
{
temp=srcImage->imageData[i*srcImage->widthStep+j*3];
fwrite((void*)&temp,sizeof(unsigned char),1,pgmPict);
}
}
}
else if(type==2)
{
for( i=0;i<srcImage->height;i++)
{
for( j=0;j<srcImage->width;j++)
{
int temp=(int)srcImage->imageData[i*srcImage->widthStep+j*3];
if(temp<0)
temp+=256;
fprintf(pgmPict,"%d ",temp);
}
}
}
fclose(pgmPict);
}
0 0
- 关于PGM格式图像读写
- PGM格式图像文件读写
- PGM格式图像详解
- PGM格式图像详解
- PGM格式图像详解
- PGM格式图像详解
- PGM格式图像详解
- P5 PGM格式图像
- PGM格式图像详解
- PGM格式图像详解及处理方法
- jpg格式的图像与pgm格式的图像互转 matlab代码
- matlab读写pgm文件
- PGM文件读写
- matlab读写pgm文件
- PictureStudio开发笔记03-PGM格式图像详解及处理方法
- PPM / PGM / PBM 图像文件格式
- PPM / PGM / PBM 图像文件格式
- PPM / PGM / PBM 图像文件格式
- 知识片段---textfield限制字符输入个数(包括汉子)
- 关于地址转换
- 解决android数据库并发访问异常
- 【Boost】boost库中thread多线程详解4——谈谈recursive_mutex
- Boost 学习之算法篇 is_partitioned
- 关于PGM格式图像读写
- Android内存优化之------static使用篇
- 【Boost】boost库中thread多线程详解5——谈谈线程中断
- s5pv210 u-boot启动过程
- Dialog的创建 16-1-第四周
- 安装cocoapods,试验成功
- 后台存储数据到数据库中文乱码
- Label的文字自适应
- 【Boost】boost库中thread多线程详解6——线程组简单例子