一、基础功能:
(1)在命令行窗口输入‘doc 函数’,如图: (2)弹出页面即为函数的功能,如图: (3)当遇到如图的代码里的函数参数不知道什么意思的时候, 可以在“帮助”页面里按Ctrl+F,然后搜索相应的参数,如图。
在命令行窗口输入代码时,若不需要输出结果,则在代码句末尾加‘;’,再回车;若需要输出结果,则直接回车即可。如图:
在命令行窗口输入‘clc’,会清空命令行窗口。如图:
matlab中第一个值从1开始计数。
二、项目一:
img_R = imread
('rabbit.jpg');
%读取图像
img_R_g = rgb2gray
(img_R
);
%函数rgb2gray:将图像转换为灰度图像
img_r_g = double
(img_R_g
);
%将图像转换为double型进行运算
img_r_g
(7
, 9
)
%索引特定像素的灰度值
figure
;imshow
(img_R
)
title
('RGB');
figure
;imshow
(img_R_g
)
title
('Gray');
figure
;imshow
(img_R_g
,[40 170
])
%显示图像img_R_g
[40 170
]的像素点
title
('Gray Specific range')
运行程序,输出如图:
三、项目二:
img_M = imread
('E:\Matlab\mouse.jpg');
%imread:读取图像
figure
;imshow
(img_M
)
%imshow:显示图像
[X Y] = ginput
(4
);
%ginput
(n
):能够从当前轴标识n个点
,并在x和y列矢量中返回这些点的x和y坐标
X2 =
[X(1:2); X(1:2)];
Y2 =
[Y(1); Y(1); Y(3) + 50; Y(3) + 50];
%取出X中第1、2、1、2的值赋给X2,取出Y中第1、1、3、3的值进行相应
%matlab中第一个值从1开始计数
tform = maketform
('projective', [X Y], [X2 Y2]);
%函数maketform
(a
,b
):创建TFORM结构体。
%其中,a:希望执行变换的类型,b:变换矩阵。
%’projective’:投影变换。
img_M_out = imtransform
(img_M
, tform
, 'bicubic');
%函数imtransform
(a,tform
,b
):图像的空间变换,返回变换后的图像。a:输入变换的图像,b:TFORM结构体
%'bicubic'为三次插值
figure
;imshow
(img_M_out
)
%显示图像
运行程序,输出如图:
四、项目三:
width = 19
;
sigma = 3
;
gaus = fspecial
('gaussian', width
, sigma
);
%函数fspecial
(type,para
):建立预定义的二维滤波模板。
%其中,
type:算子的类型,para:相应的参数
%gaussian为高斯低通滤波器
g = imfilter
(gaus
, gaus
, 'symmetric', 'same');
%对任意类型数组或多维图像进行滤波
%I = imfilter
(f
, w
, filtering_mode
, boundary_options
, size_optinos
);
%I为滤波结果
,f是输入图像,w为滤波模板
%filtering_mode为滤波模式,boundary_options为边界选项,size_optinos为大小选项
figure
;imshow
(g
, []);
%imshow
(I
,[low high])显示灰度图像I,以二元素向量
[low high] 形式指定显示范围。
sigma = 16
;
noise = randn
(size
(g
))*sigma
;
%randn(n)返回一个n
*n的随机项的矩阵。如果n不是个数量,将返回错误信息。
g_n = gaus
+ noise
;
figure
;imshow
(g_n
, []);
%自动调整数据的范围以便于显示。
运行程序,输出如图:
五、项目四:
width = 19
;
sigma = 3
;
gaus = fspecial
('gaussian', width
, sigma
);
%h = fspecial
(type,para
)
%type指定算子的类型,para指定相应的参数
%gaussian为高斯低通滤波器
surf
(gaus
)
%surf命令绘制着色的三维曲面
axis off
%关闭所有的坐标轴标签、刻度、背景
运行程序,输出如图:
六、项目五:
width = 19
;
sigma = 3
;
gaus = fspecial
('gaussian', width
, sigma
);
%函数fspecial
(type,para
):建立预定义的二维滤波模板。
%其中,
type:算子的类型,para:相应的参数
%gaussian为高斯低通滤波器
imagesc
(gaus
)
%采用线性映射会对数据进行缩放后,显示图像
colormap
(hot
)
%函数绘制表面图时,colormap
(即色图
)决定图片的颜色。
运行程序,输出如图:
七、项目六:
img = imread
('Hollywood.png');
figure
;imshow
(img
)
title
('img')
h = fspecial
('sobel');
%fspecial
():建立预定义的滤波算子。
%sobel:用于边缘提取,无参数。
im_dy = imfilter
(double
(im
), h
, 'conv');
im_dx = imfilter
(double
(im
), h
', 'conv
');
%imfilter(f,w,filtering_mode):对任意类型数组或多维图像进行滤波。
%f:输入图像,w:滤波模板,filtering_mode:conv指滤波通过卷积来完成。
%h':矩阵h的转置
figure
;imshow
(sqrt
(im_dx
.^2
+ im_dy
.^2
), []);
%sqrt
(x
):返回数组x的每个元素的平方根。
%若x为公式,则需将每一个变量末尾加“
.”。
title
('img 边缘提取')
colormap jet
%将色度图的一种模式jet应用到图像中。
运行程序,输出如图:
八、项目七:
img = imread
('love.jpg');
img_G = rgb2gray
(img
);
img_G_edge = edge
(img_G
, 'canny');
%edge
(I
,'sobel'):自动选择阈值用Sobel算子对图像I进行边缘检测。
figure
;imshow
(img
);
title
('img');
figure
;imshow
(img_G
);
title
('img Gray')
figure
;imshow
(img_G_edge
);
title
('img Gray 边缘检测')
运行程序,输出如图:
九、项目八:
img = imread
('jida.jpg');
figure
;imshow
(img
);
title
('img');
img_G = double
(rgb2gray
(img
))/255
;
figure
;imshow
(img_G
);
title
('img Gray');
sigma = 5
;
gaus = fspecial
('gaussian', 2
*sigma
*3
+1
, sigma
);
%3
-σ法则: 2
*sigma
*3
+1
%fspecial
(type,parameters
,sigma
):建立预定义的滤波算子。
%type:算子类型;hsize:模板尺寸;sigma:滤波器的标准值。
%gaussian:高斯低通滤波。
dx =
[-1 0 1
;-1 0 1
; -1 0 1
];
%dx:微分滤波器
Ix = imfilter
(img_G
, dx
, 'symmetric', 'same');
Iy = imfilter
(img_G
, dx
', 'symmetric
', 'same
');
%imfilter(f,w,boundary_options,size_optinos):对任意类型数组或多维图像进行滤波。
%f:输入图像,w:滤波模板,boundary_options:边界选项,size_optinos:大小选项。
%symmetric:图像大小通过沿自身的边界进行镜像映射扩展。
%same:输入图像的大小与被扩展图像的大小相同。
figure;imshow(Ix);
title('Ix
');
figure;imshow(Iy);
title('Iy
');
figure;imshow(Ix.^2);
title('Ix
.^2
');
figure;imshow(Iy.^2);
title('Iy
.^2
');
figure;imshow(Ix.*Iy);
title('Ix
.*Iy
');
Ix2 = imfilter(Ix.^2, gaus, 'symmetric
', 'same
');
figure;imshow(Ix2);
title('Ix2
');
Iy2 = imfilter(Iy.^2, gaus, 'symmetric
', 'same
');
figure;imshow(Iy2);
title('Iy2
');
Ixy = imfilter(Ix.*Iy, gaus, 'symmetric
', 'same
');
figure;imshow(Ixy);
title('Ixy
');
k = 0.04;
r = (Ix2.*Iy2 - Ixy.^2) - k*(Ix2 + Iy2).^2;
figure;imshow(r);
title('r'
);
运行程序,输出如图:
十、项目九:
img = imread
('snow.jpg')
img_G_double = double
(rgb2gray
(img
));
figure
;imshow
(img
);
title
('img');
figure
;imshow
(img_G_double
);
title
('img Gray double');
img_fft = fft
(img_G_double
);
%fft
():对信号做频谱分析,即傅里叶变换。
img_fft2 = fft2
(img_G_double
);
%fft2
():二维快速傅里叶变换
img_fft2
(1
,1
) = 0
;
%移除直流分量,提高可视化效果。
img_fft2 = log
(1
+ abs
(img_fft2
));
figure
;imshow
(img_fft2
);
title
('img_fft2');
%abs(img_fft2):幅度谱
%img_fft2 = log
(1
+ abs
(img_fft2
)):提高可视化效果。
imshow
(fftshift
(img_fft2
), []);
%fftshift
():将零频分量移动到数组中心,重新排列傅里叶变换。
title
('fftshift');
运行程序,输出如图:
十一、项目十:
I1 = imread
('krystal.jpg');
I2 = imread
('iu.jpg');
I1_fft = fft2
(I1
);
I2_fft = fft2
(I2
);
abs1_phase2 = abs
(I1_fft
).*exp
(1i
*angle
(I2_fft
));
%将一幅图像的幅度和另一幅图像的相位相结合
%abs
(I1_fft
):计算第一幅图像的幅度分量
%exp
(1i
*angle
(I2_fft
):计算第二幅图像的相位分量
%li:虚数单位
abs2_phase1 = abs
(I2_fft
).*exp
(1i
*angle
(I1_fft
));
I_abs1_phase2 = real
(ifft2
(abs1_phase2
));
%ifft2
():二维傅里叶逆变换
%real
(x
):返回复数数组x的实部
I_abs2_phase1 = real
(ifft2
(abs2_phase1
));