%% 学习目标:学习常用的图像处理技巧
%% 改变图像的大小
clear all;
close all;
[X,map]=imread('trees.tif','tif'); %读取索引图像
[Y,newmap]=imresize(X,map,0.2); %改成五分之一大小
figure;
subimage(X,map);
figure;
subimage(Y,newmap);
%% 图像的剪切 获取图像的一部分 矩形切割
clear all;
close all;
I=imread('trees.tif','tif');
J=imcrop(I,[20,20,100,100]); %矩形的原点是20,20 大小是100*100
figure;
imshow(I);
figure;
imshow(J);
%% 实现图像的旋转
clear all;
close all;
A=imread('trees.tif','tif');
B=imrotate(A,-90,'nearest'); %顺时针旋转90度
figure;
subplot(121);
imshow(A);
subplot(122);
imshow(B)
set(gcf,'position',[300,300,600,400]);
%% 绘制灰度图像的直方图
clear all;
close all;
I=imread('pout.tif');
subplot(121);
imshow(I);
subplot(122);
imhist(I); %集中部分可以反映图像清晰程度
set(gcf,'position',[400,400,600,600]);
%% 图像的对比度增强
clear all;
close all;
I=imread('pout.tif');
J=imadjust(I,[0.3 0.7],[]); %对比度范围发生变化,0.3到0.7变成 0到1之间
subplot(121);
subimage(I);
subplot(122);
subimage(J);
set(gcf,'position',[400,400,600,500]);
%% 对RGB图像进行增强
clear all;
close all;
RGB1=imread('peppers.png');
RGB2=imadjust(RGB1,[0.2 .3 0; .6 .7 1],[]);
subplot(121);
subimage(RGB1);
subplot(122);
subimage(RGB2);
set(gcf,'position',[400,400,600,500]);
%% 添加噪声
clear all,close all;
I=imread('trees.tif','tif');
J1=imnoise(I,'gaussian',0.1); %高斯噪声
J2=imnoise(I,'poisson'); %泊松噪声
J3=imnoise(I,'salt & pepper',0.1); %椒盐噪声
figure;
subplot(221),imshow(I),title('原图像')
subplot(222),imshow(J1),title('Gaussian')
subplot(223),imshow(J2),title('Poisson')
subplot(224),imshow(J3),title('Salt & Pepper');
%% 图像的DCT 离散余弦变换 常用来做图像压缩
clear all;
close all;
RGB=imread('peppers.png'); %读取一个RGB的图像 注意
I=rgb2gray(RGB);
J=dct2(I); %图像的DCT 离散余弦变换
figure;
imshow(log(abs(J)),[]),colormap(jet),colorbar;
J(abs(J)<10)=0;
K=idct2(J); %反变换
figure;
imshow(K,[0 255]);
%% 图像的傅里叶变换
clear all;
close all;
I=imread('trees.tif','tif');
J=fft2(I,256,256);
K1=fftshift(J); %对傅里叶变换后的系数进行移位
K2=log(abs(K1));
figure;
subplot(121);
imshow(I);
subplot(122);
imshow(K2,[]);
set(gcf,'position',[400,400,600,500]);
%% 图像的傅里叶反变换
clear all;
close all;
I=imread('trees.tif','tif');
J=fft2(I,256,256);
K=ifft2(J,256,256);
figure;
subplot(121);
imshow(log(abs(J)),[]);
subplot(122);
imshow(K,[]);
set(gcf,'position',[300,300,400,400]);
%% 大仙QQ:1960009019
%% 在线教育微信公众号:大仙一品堂
%% 大鱼号:在线教育大仙一品堂
%% 一点资讯号:大仙一品堂
%% 2018/3/19 录制,欢迎指正