MATLAB学习笔记 学习总结归纳(第一周)

来源:互联网 发布:linux中ping命令 编辑:程序博客网 时间:2024/06/05 20:34

此学习总结归纳为自己在这一周学习知识的总结,进行归纳整理,如有错误,请批评指正

本文进度-第1章~第3章


性能测试

时间

tic toc 用于记录所包含的语句的执行时间

tic; function(); toc % 返回函数运行时间

timeit(function) 用于记录传入的函数句柄的执行时间

f = @() function(x); % 函数句柄timeit(f) % 返回函数运行时间(不是很准确,此函数中有一些检测函数运行情况的函数,占用一部分时间,不过用起来很方便)

对象操作

矩阵

eye(n, m) 产生单位矩阵(生成n*m的单位矩阵)

f = eye(2)f = eye(1, 2)

zeros(x, y, z) 生成x*y*z的三维矩阵(个数无限制)每个元素都为0

f = zeros(1, 2, 3);f = zeros(1);

ones(x, y, z) 生成x*y*z的三维矩阵(个数无限制)每个元素都为1

f = ones(1, 2, 3);

linspace(a, b, numel(T)) 线性插值(相当于创建一个行向量,其每个元素的值为a~b的线性值-斜率为1)

linspace(1, 0, 25)

meshgrid(x, y, z) 用于生成二维、三维数组(速度快,我的电脑的话,比正常的forforfor快5-10倍)

以二维为例,C返回一个二维数组,一共有numel(x)列,numel(y)行,每行数字相同,每列对应为x[id],R 则与之相反。

[C, R] = meshgrid(1:2:10, 1:3:30);

prod(A, n) 返回传入参数的乘积 (默认n为1[不写即为1])

此函数有3种常用情况
* 当传入参数为一维数组(只有一行/一列)时,传出为元素的乘积

prod([1 2 3 4 5]) % 返回 120

当传入参数为二维数组时,传出为每列的乘积

prod([1 2;3 4]) % 返回3 8

当传入参数为一个二维数组和一个数(代表维度),则会返回以这个维度为基准的每维的乘积

prod([1 2; 3 4], 2) % 返回 2 12

padarray(A, [x, y], method, direction) 填充数组,第一个参数为要填充的数组,x表示在x轴,要填充几个,y表示在y轴要填充几个,method(方法)有四种情况(书上有三种- -),direction(方向)也有三种情况

方法: P ‘replicate’ ‘symmetric’ ‘circular’
方向: ‘pre’ ‘post’ ‘both’

padarray([1 2; 3 4], [3 2], 'post')padarray([1 2; 3 4], [3 2], 'pre')padarray([1 2; 3 4], [3 2], 'both')padarray([1 2; 3 4], [3 2], 5, 'post')padarray([1 2; 3 4], [3 2], 5, 'pre')padarray([1 2; 3 4], [3 2], 5, 'both')padarray([1 2; 3 4], [3 2], 'replicate', 'post')padarray([1 2; 3 4], [3 2], 'replicate', 'pre')padarray([1 2; 3 4], [3 2], 'replicate', 'both')padarray([1 2; 3 4], [3 2], 'symmetric', 'post')padarray([1 2; 3 4], [3 2], 'symmetric', 'pre')padarray([1 2; 3 4], [3 2], 'symmetric', 'both')padarray([1 2; 3 4], [3 2], 'circular', 'post')padarray([1 2; 3 4], [3 2], 'circular', 'pre')padarray([1 2; 3 4], [3 2], 'circular', 'both')

symmetric 就是镜像复制, replicate就是直接复制边界, circular就是周期复制,post就是在每个维度后面填充, pre就是在前面, both就是post+pre。

size(A, n) 返回A矩阵的第n维有几个,默认为全部(三维则返回三个)

f = zeros(1, 2, 3);size(f)size(f, 1)% size(f, 0) % 会报错,因为维度从1开始(不像编程语言)

numel(A) 返回A矩阵的元素个数(全部元素,跨维度计数)

f = zeros(1, 2, 3);numel(f)

ndims(A) 返回矩阵是几维.

A = ones(1, 2, 3);ndims(A) % 返回3

图像

imread(fileName) 读取名为fileName的图片,范围类型根据图片不同而不同

f = imread('coins.png');

imwrite(image, fileName) 将图片image, 储存为fileName

fileName可以包含路径(相对、绝对都可以),格式根据给定格式,函数会自动转换

f = imread('coins.png');imwrite(f, '1.jpg');imwrite(f, 'c:\1.png');

imfinfo(fileName) 将fileName的图片信息读出来

f = imfinfo('conins.png')

显示图片 imshow(image) 将图片显示(在上一个窗口显示,如果没有则新建一个)

% 普通图f = imread('onion.png');imshow(f);% 索引图[X, map] = rgb2ind(f, 256);imshow(X, map);
  • 输入:
    这里写图片描述
  • 输出:
    这里写图片描述

图像转换

gray2ind(iamge, n) 将传入的灰度图转化索引图, n代表要分为几个索引颜色
f = imread('coins.png');[X, map] = gray2ind(f, 5); % X为索引坐标 map为颜色索引表imshow(X, map);
  • 输入:
    这里写图片描述
  • 输出:
    这里写图片描述
rgb2gray(image) 将rgb转为gray(灰度图)
f = imread('onion.png');g = rgb2gray(f);imshow(g)
  • 输入:
    这里写图片描述
  • 输出:
    这里写图片描述
im2bw(image, level) 将图转换为二值图,level为阈值(区间为[0, 1])

低于level 都为黑色(0), 否则为白色(1)

f = imread('coins.png');g = im2bw(f, 80/ 256);imshow(g);
  • 输入:
    这里写图片描述
  • 输出:
    这里写图片描述
ind2gray(X, map) 从索引图到灰度图
ind2rgb(X, map) 从索引图到RGB图
**mat2gray() 将矩阵转化为灰度图
f = ones(100, 100); % 创建一个100*100的矩阵for i=1:100    for j=1:100        f(i, j) = i;    endendsubplot(1, 2, 1), imshow(f), title('直接显示');g = mat2gray(f);subplot(1, 2, 2), imshow(g), title('转换后显示');
  • 输入:
    这里写图片描述
  • 输出:
    这里写图片描述

图像处理

直方图

imhist(image, num) 归一化直方图

image 表示要处理的图
num表示将灰度级分为平等的几份
返回的则是灰度范围在每份灰度级中的像素个数

f = imread('onion.png');g = rgb2gray(f);h = imhist(g, 25);subplot(1, 2, 1), imshow(g);subplot(1, 2, 2), bar(h);
  • 输入:
    这里写图片描述
  • 输出:
    这里写图片描述

bar(horz, z, width) 绘制条形图

horz 表示 水平增量,需要与z的行数相同。
z 表示要显示的条形图的数据
width 表示条形图的宽度

f = imread('onion.png');g = rgb2gray(f);h = imhist(g, 25) / numel(g);subplot(2, 1, 1), imshow(g);subplot(2, 1, 2), bar(linspace(0, 255, 25), h, 1);
  • 输入:
    这里写图片描述
  • 输出:
    这里写图片描述

stem(z) 生成一个杆状图(也可以省略h)

f = imread('onion.png');g = rgb2gray(f);h = imhist(g, 25) / numel(g);subplot(1, 2, 1), stem(h);subplot(1, 2, 2), stem(linspace(0, 1, 25), h);
  • 输入:
    这里写图片描述
  • 输出:
    这里写图片描述

stem 还有一个参数,表示杆状图以什么形式显示

stem(h, 'k:p')

具体如下
‘颜色说明符+线型说明符+标记点说明符’
图片

plot 生成一个折线图,用法跟stem一样

f = imread('onion.png');g = rgb2gray(f);h = imhist(g, 25) / numel(g);subplot(1, 2, 1), plot (h);subplot(1, 2, 2), plot (linspace(0, 1, 25), h);

axis([xMin xMax yMin yMax]) 设置上面最近显示的图表的x轴和y轴的范围

f = imread('onion.png');g = rgb2gray(f);h = imhist(g, 25);z = linspace(0, 255, 25);bar(z, h)axis([0 255 0 10000])
  • 输入:
    这里写图片描述
  • 输出
    这里写图片描述

接着下面可以跟着二行
set(gca, ‘xtick’, 0:50:255) % 表示将x轴的以传入的数组表示
set(gca, ‘ytick’, 0:2000:10000) % % 表示将y轴的以传入的数组表示
* 输入:
这里写图片描述
* 输出
这里写图片描述

axis tight 自适应图标

f = imread('onion.png');g = rgb2gray(f);h = imhist(g, 25);z = linspace(0, 255, 25);bar(z, h)axis tight
  • 输入:
    这里写图片描述
  • 输出:
    这里写图片描述

xlabel ylabel 给x和y轴添加标记

xlabel('灰度级')ylabel('次数')

text(x, y, text) 在x轴的值为x,y轴的值为y处添加text文件

text(50, 2000, '50,2000')

title(title) 给图片添加标题

title('我是可爱的标题')
  • 输入:
    这里写图片描述
  • 输出:
    这里写图片描述

hold on, hold off 开启、关闭保持当前图标状态

bar(z, h)hold onstem(z, h)hold offbar(z, h)

ylim xlim 设置y轴x轴的范围(默认就是auto状态)

ylim([0 60000])xlim([0 255])

灰度级

imadjust(image, [low_in high_in], [low_out high_out], gamma) 用于将图像进行灰度级转换

f = imread('onion.png');g = imadjust(f, [], [1, 0]);imshow(g)

imcomplement(image) 获得图片的负片, 可以实现CMY模型与RGB互换

rgb = imcomplement(cmy);cmy = imcomplement(rgb);

上面二个结果一样
* 输入:
这里写图片描述
* 输出:
这里写图片描述

stretchlim(image, tol) 用于实现对比度拉伸

f = imread('onion.png');f = rgb2gray(f);Low_High = stretchlim(f, [0.15 0.99]);g= imadjust(f, Low_High, []);subplot(2,2,1), imshow(f), title('原图');subplot(2,2,3), bar(imhist(f) / numel(f)), axis tight,title('原图 灰度图');subplot(2,2,2), imshow(g), title('拉伸后');subplot(2,2,4), bar(imhist(g) / numel(g)), axis tight, title('拉伸后 灰度图');
  • 输入:
    这里写图片描述
  • 输出:
  • 这里写图片描述

graythresh(image) 获得图片最优的阈值。

f = imread('rice.png');g = im2bw(f, graythresh(f));subplot(1, 2, 1), imshow(f), title('原图');subplot(1, 2, 2), imshow(g), title('二值化(最优阈值)');
  • 输入:
    这里写图片描述
  • 输出:
    这里写图片描述

histeq(image, num) 该函数可以将输入的图片按灰度级分为num份,使得每份所占的比例近似相等。

f = imread('rice.png');g = histeq(f, 5);t = imhist(g, 5);subplot(1, 2, 1), bar(t); subplot(1, 2, 2), imshow(g);
  • 输入:
    这里写图片描述
  • 输出:
    这里写图片描述

adapthisteq(image) 将图像对比度增强

f = imread('onion.png');f = rgb2gray(f);subplot(1, 2, 1), imshow(f), title('原图');g = adapthisteq(f);subplot(1, 2, 2), imshow(g), title('增强后');
  • 输入:
    这里写图片描述
  • 输出:
    这里写图片描述

imfilter(image, mod) 线性滤波

可选参数为三个,分别是滤波模式、边界选项、大小选项
滤波模式: ‘corr’ ‘conv’
边界选项: P ‘replicate’ ‘symmetric’ ‘circular’
大小选项: ‘full’ ‘same’

% 创建测试图像f = zeros(500);f(250:500, 1:250)=1;f(1:250, 250:500)=1;imwrite(f, 'temp.bmp'); % 保存测试图片g = imfilter(f / 50 / 50, ones(50)); subplot(2, 2, 1), imshow(g);title('none');g = imfilter(f / 50 / 50, ones(50), 'replicate'); subplot(2, 2, 2), imshow(g);title('replicate');g = imfilter(f / 50 / 50, ones(50), 'circular'); subplot(2, 2, 3), imshow(g);title('circular');g = imfilter(f / 50 / 50, ones(50), 'symmetric'); subplot(2, 2, 4), imshow(g);title('symmetric');
  • 输入:
    这里写图片描述
  • 输出:
    这里写图片描述

colfilt(image, [m n], ‘sliding’, fun) 非线性空间滤波, 传入图片,滤波区域,和函数句柄

t = colfilt(g, [5 5], 'sliding', fun);t = gscale(t, 'full16'); % 该函数看附录

fspecial(type, paramters) 线性空间滤波器,type值有9种情况,对应paramters也不同

type : ‘average’ ‘disk’ ‘gaussian’ ‘laplacian’ ‘log’ ‘motion’ ‘prewitt’ ‘sobel’ ‘unsharp’
paramters: [r c] r [r c], sig alpha [r c], sig len, theta null null alpha (与上面的一一对应,null为没有)

f = imread('moon.tif');figure, imshow(f);title('none');g = imfilter(tofloat(f), fspecial('average')); subplot(3, 3, 1), imshow(g); title('arverage 3*3');g = imfilter(tofloat(f), fspecial('disk')); subplot(3, 3, 2), imshow(g); title('disk 5');g = imfilter(tofloat(f), fspecial('gaussian')); subplot(3, 3, 3), imshow(g); title('gaussian 3*3 0.5');g = imfilter(tofloat(f), fspecial('laplacian')); subplot(3, 3, 4), imshow(tofloat(f) - g); title('laplacian 0.2');g = imfilter(tofloat(f), fspecial('log')); subplot(3, 3, 4), imshow(tofloat(f) - g); title('log 5*5 0.5');g = imfilter(tofloat(f), fspecial('motion')); subplot(3, 3, 5), imshow(g); title('motion 9 0');g = imfilter(tofloat(f), fspecial('prewitt')); subplot(3, 3, 6), imshow(tofloat(f) - g); title('prewitt');g = imfilter(tofloat(f), fspecial('sobel')); subplot(3, 3, 7), imshow(tofloat(f) - g); title('sobel');g = imfilter(tofloat(f), fspecial('unsharp')); subplot(3, 3, 8), imshow(g); title('unsharp 0.2');

参数的具体定义直接输入 doc fspecial 即可
* 输入:
这里写图片描述
* 输出:
这里写图片描述

medfilt2(image, [x y], padpot) 非线性滤波器 [x y]默认为3*3表示滤波区域, padpot表示滤波方式,分为三种

padpot: ‘zero’ ‘symmetric’ ‘indexed’

f = imread('onion.png');f = rgb2gray(f);g = imnoise(f, 'salt & pepper', 0.2);t = medfilt2(g); subplot(3, 3, 1), imshow(t);title('none 3*3');t = medfilt2(g, 'zero'); subplot(3, 3, 2), imshow(t);title('zero 3*3');t = medfilt2(g, 'symmetric'); subplot(3, 3, 3), imshow(t);title('symmetric 3*3');t = medfilt2(g, 'indexed'); subplot(3, 3, 4), imshow(t);title('indexed 3*3');t = medfilt2(g, [4 4]); subplot(3, 3, 5), imshow(t);title('none 4*4');t = medfilt2(g, [4 4], 'zero'); subplot(3, 3, 6), imshow(t);title('zero 4*4');t = medfilt2(g, [4 4], 'symmetric'); subplot(3, 3, 7), imshow(t);title('symmetric 4*4');t = medfilt2(g, [4 4], 'indexed'); subplot(3, 3, 8), imshow(t);title('indexed 4*4');subplot(3, 3, 9),imshow(g);title('原图');

去燥效果明显,但是具体参数的话,还是看出来太大的差别(滤波区域的设定还是很重要地)。
* 输入:
这里写图片描述
* 输出:
这里写图片描述

输入输出

窗口

figure(h) 打开一个新窗口(将h窗口置顶,h省略时则新建一个窗口)

figure(1);figure(2);figure(1);imshow(imread('coins.png'));figure(2);imshow(imread('onion.png'));
  • 输入:
    这里写图片描述
  • 输出:
    这里写图片描述

subplot(h, w, id) 将最近显示的的窗口(之前没有显示的话,会新建一个窗口),分为高位h, 宽为w个单位,并指定下一个要显示的图像在这个窗口的第id个单位格;

f = imread('coins.png');subplot(1, 3, 1), imshow(f), title('原图');subplot(1, 3, 2), imshow(imcomplement(f)), title('负片');subplot(1, 3, 3), imshow(im2bw(f, graythresh(f))), title('二值图');
  • 输入:
    这里写图片描述
  • 输出:
    这里写图片描述

控制台

disp 显示信息

disp 'Asdasd' % 输出Asdasd

input 从键盘中输入

t = input('asd', 's'); % 参数s表示将结果以字符串的形式储存

error 显示警告

error '这是错误的!' % 这里是红色警告字

附录

预设图片

预设图片

函数

gscale.m

function g = gscale(f, varargin)if isempty(varargin)    method = 'full8';else    method = varargin{1};endif strcmp(class(f), 'double') && (max(f(:)) > 1 || min(f(:)) < 0)    f = mat2gray(f);endswitch method    case 'full8'        g = im2uint8(mat2gray(double(f)));    case 'full16'        g = im2uint16(mat2gray(double(f)));    case 'minmax'        low = varargin{2};        high = varargin{3};        if low > 1 || low < 0 || high > 1 || high < 0            error('low | high must be [0, 1]');        end        if strcmp(class(f), 'double')            low_in = max(f(:));            high_in = max(f(:));        elseif strcmp(class(f), 'uint8')            low_in = double(min(f(:))) ./ 255;            high_in = double(max(f(:))) ./ 255;        elseif strcmp(class(f), 'uint16')            low_in = double(min(f(:))) ./ 65535;            high_in = double(max(f(:))) ./ 65535;        end        g = imadjust(f, [low_in high_in], [low high]);    otherwise        error('Unknow method');end

tofloat.m

function [out, revertclass] = tofloat(in)identity = @(x) x;tosingle = @im2single;table = {'uint8', tosingle, @im2uint8    'uint16', tosingle, @im2uint16    'uint32', tosingle, @im2int32    'logical', tosingle, @logical    'double', identity, identity    'single', identity, identity};classIndex = find(strcmp(class(in), table(:, 1)));if isempty(classIndex)    error('Unsupported input image class');endout = table{classIndex, 2}(in);revertclass = table{classIndex, 3};
原创粉丝点击