MATLAB

1-72

% 输入若干个数,当输入0时结束输入,求这些数的平均值和他们的和
sum = 0;
n =0;
val = input('输入数字(输入0就结束):');
while (val ~= 0)
    sum = sum + val;
    n = n+1;
    val = input('输入数字(输入0时就结束):');
end
if (n > 0)
    sum   
    mean = sum/n
end

 1-73

% break命令
a = 5;
b = 8;
for i = 1:4
    b = b+1;
    if i > 2
        break
    end
    a = a + 2
end

1-74

% continue语句
a = 5;b = 8;
for i = 1:4
    b = b +1;
    if i < 2
        continue
    end
    a = a+ 2
end

1-75

% try 语句,提高程序的容错性
try
    picture = imread('01.jpg','jpg');
    filename = '01.jpg';
catch
    picture = imread('01.bmp','bmp');
    filename = '01.bmp';
end
filename

1-76

% 先求两个矩阵的乘机,若出错,则自动转去求两矩阵的点乘
A = magic(2);
B = [7, 8; 10, 11];
try 
    C = A*B;
catch
    C = A.*B;
end
C
lasterr  %显示出错原因

2.X  图形可视化

2-1

% 准备数据
x = 0 : 0.01: 3 *pi;
y1 = cos(x);
y2 = cos(2*x);
% 设置当前绘图区
figure;
% 绘图
plot(x, y1, x, y2);
% 设置坐标轴和网格线属性
axis([0 8 -2 2])
grid on;
% 标注图形
xlabel('x');
ylabel('y');
title('绘图基本步骤');
% 显示图例
legend('cos(x)', 'cos(2*x)')

2-3

% line 函数绘制cos函数图形
x = 0 :0.15 : 1.5*pi;
y = cos(x);
line(x, y);
axis([0 7 -1.5 1.5]);
xlabel('x');
ylabel('y');

2-4

% 画同心圆
theta = linspace(0, 3*pi, 50);
r = 0.4: 0.24: 1.74;
x = 1 + cos(theta)' *r ;
y = 2 + sin(theta)' *r ;
plot(x, y, 1, 2, '+');
axis([-1 3 0 4]);
title('同心圆');
axis equal;
xlabel('x');
ylabel('y');

三维绘图

2-34

% plot3 函数绘制一条三维螺旋线
t = 0: pi/50:8 * pi;
x = sin(t);
y = cos(t);
z = t;
plot3(x, y, z, 'r');
xlabel('sin(t)');
ylabel('cos(t)');
zlabel('z');
title('绘制三维螺旋线');
legend('t');
grid on;

3.X  图像处理基础

3-1

% imfinfo查询图像文件信息
info = imfinfo('01.jpg');
info

3-2

% 图像文件读取
clc;
l1 = imread('01.jpg');   %读取一副RGB图像
%l2 = imread( '02.jpg', ' tif ');    %读取一副灰度图像
l3 = imread('F:matlab_test3.jpg');
figure;
subplot(1, 2, 1), imshow(l1);
title('显示RGB图像');
%subplot(1, 3, 2), imshow(l2);
%title('显示灰度图像');
subplot(1, 2, 2), imshow(l3);
title('显示指定路径下的图像文件');

3-3

1 % 内联函数
2 f1 = inline ('x^2 + y^2', 'x', 'y');
3 f1(2, 3)
4 
5 % 匿名函数
6 f2 = @(x, y) x^2 + y^2;
7 f2(3, 4)
 1 % 读取
 2 x = imread('02.jpg');
 3 % 显示
 4 imshow(x);
 5 imshow(x, [100 150]);
 6 % 分区
 7 subplot(2, 2, 1);
 8 imshow(x); title('图一');
 9 % 保存
10 imwrite( x, 'xieyi.jpg');
% 图像邻域操作
x = imread('03.jpg');
x = rgb2gray(x);
f = @(x) max(x(:));
y = nlfilter(x , [3 3], f); %过滤函数处理灰度图像
subplot(1, 2, 1);imshow(x);
subplot(1, 2, 2);imshow(y);
1 % 图像叠加
2 I = imread('01.jpg');
3 size(I)
4 J = imread('02.jpg');
5 size(J)
6 W = imadd(J, I, 'uint16');%两幅图相加
7 K = imadd(I, 100);%增加亮度
8 imshow(K);
%快速邻域图像块操作
x = imread('04.jpg');
x = rgb2gray(x);
myf = @(x) max(x);
y = uint8 (colfilt(x, [3 3], 'sliding', myf));
subplot(1, 2, 1);imshow(x);title('原图');
subplot(1, 2, 2);imshow(y);title('处理后的图');
% 绝对差操作图像
x = imread('04.jpg');
x = rgb2gray(x);
myf = @(x) mean(x);
y = uint8(colfilt(x, [4 4], 'sliding', myf));
z = imabsdiff(x, y);
subplot(3, 1, 1);imshow(x);
subplot(3, 1, 2);imshow(y);
subplot(3, 1, 3);imshow(z);
1 % 绝对值差函数
2 Z = imabsdiff( A, B);
3 imshow(Z)
1 % 图像的减法
2 x = imread('03.jpg');
3 x = imadd(x, 100);
4 x = rgb2gray(x);
5 y = imread('0301.jpg');
6 imshow(y)
7 z = imsubtract(x, y);%减法函数
8 imshow(z);
 1 % 图像的乘法
 2 x = imread('03.jpg');
 3 x = imadd(x, 100);
 4 x = rgb2gray(x);
 5 y = imread('0301.jpg');
 6 figure;
 7 imshow(y);
 8 z = immultiply(x, y);%乘法函数
 9 figure;
10 imshow(z);
11 w = immultiply(z, 0.5);
12 figure;
13 imshow(w);
% 求补运算
i = imread('04.jpg');
i = rgb2gray(i);
i1 = imcomplement(i)
figure;imshow(i);
figure;imshow(i1);
1 % 改变图像大小
2 I = imread('01.jpg');
3 O = imresize(I, 0.5, 'bicubic');
4 figure;
5 imshow(I);
6 figure;
7 imshow(O);
1 % 图像剪切
2 I = imread('01.jpg');
3 I2 = imcrop(I , [10 10 100 100] );
4 imshow(I);
5 figure;
6 imshow(I2);
 1 % 图像旋转
 2 I = imread('02.jpg');
 3 thetal =  30;
 4 J = imrotate(I, thetal);
 5 thetal2 = -30;
 6 K = imrotate( I, thetal2, 'crop');
 7 figure;
 8 imshow(I);
 9 figure, imshow(J);
10 figure, imshow(K);
1 % 图像的几何变换
2 I = imread('03.jpg');
3 imshow(I);
4 I = rgb2gray(I);
5 tform = affine2d ([cosd(30) sind(30) 0; -sind(30) cosd(30) 0; 0 0 1]); %二维仿射函数
6 J = imwarp(I , tform); %图像几何变换
7 figure;
8 imshow(J)
 1 % 一维离散傅里叶变换
 2 Fs = 1000;
 3 T = 1/Fs;
 4 L = 1000;
 5 t = (0 :L-1) * T;
 6 S = 0.7 * sin( 2 * pi * 50 * t) + sin( 2 * pi * 120 * t);
 7 X = S + 2 * randn( size(t));
 8 Y = fft(X);
 9 P2 = abs(Y);
10 plot(P2(1 : end/2));
1 %快速傅里叶变换
2 f = zeros(256,256);
3 f (30: 144 , 78:102) = 1;
4 imshow(f);
5 F = fft2(f);
6 F2 = log(abs(F));
7 figure, imshow(F2, [-1, 5]);
 1 % 计算连个矩阵的卷积
 2 A = [ 1 2 3; 4 5 6; 7 8 9];
 3 B = ones(3);
 4 % 扩展矩阵维数
 5 A(8, 8) = 0;
 6 B(8, 8) = 0;
 7 %对A,B进行傅里叶变换并乘积后进行逆变换
 8 C = ifft ( fft2(A) .* fft2(B));
 9 % 取有效数据
10 C = C(1:5, 1:5);
11 C = real(C);
1 % 离散余弦变换,常用于图像压缩
2 I = imread('03.jpg');
3 I = rgb2gray(I);
4 imshow(I);
5 % 离散余弦变换
6 J = dct2(I);
7 figure;imshow(log(abs(J)), []);
% 图像重构
RGB = imread('04.jpg');
I = rgb2gray(RGB);
imshow(I);
J = dct2(I);
J(abs(J) < 10) = 0;
K = idct2(J);
figure;
imshow(K, [0, 255]);
原文地址:https://www.cnblogs.com/xieyi-newlife/p/9108145.html