参考网址

(110条消息) matlab 数据导入_哆啦A梦的博客-CSDN博客_matlab导入数据

(110条消息) MATLAB-读取pgm图像_FrankDura的博客-CSDN博客_matlab读取pgm

(110条消息) matlab读写pgm文件_freshair9的专栏-CSDN博客_matlab打开pgm文件

load

1
2
% Load the file to the matrix, M :
M = load('sample_file.txt')

matlab读取pgm文件

1
2
3
4
clc,clear,close all;
im1 = imread(‘1.pgm’);
imshow(im1)
% 可知im1时一个二维数组,254表白色,0表黑色,205表未知

另一种方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
% 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);