【Matlab】数字图像处理——02_图像的基本变换

imadjust
imcomplement
imhist
axis tight
ylim
im2gray
imnoise
medfilt2
hpfilter
dftfilt
fftshift
fft2
log

代码:

%% 
clc;
clear;
close all;

%% 图像的明暗反转
figure('name','图像的明暗反转');
f = imread('tsy.jpg');
subplot(1,2,1),imshow(f);title("原图");

g1 = imadjust(f,[0,1],[1,0]); % === imcomplement(f) 图像负片效果
subplot(1,2,2),imshow(g1);title("反转");

%% 图像的对数变换
figure('name','图像的对数变换');
f = imread('tsy.jpg');

subplot(2,2,1),imshow(f);title("原图");
subplot(2,2,2),imhist(f),axis tight;title("原图直方图");

g = im2uint8(mat2gray(log(1 + double(f)))); %对数变换
subplot(2,2,3),imshow(g);title("对数变换后图像");
subplot(2,2,4),imhist(g),axis tight;title("对数变换后图像直方图");

%% 图像对比度拉伸变换
figure('name','图像对比度拉伸变换');
f = imread('tsy.jpg');
subplot(1,2,1);imshow(f);title("原图");

g = intrans(f,'stretch',mean2(im2double(f)),0.9); % 利用intrans函数进行对比度拉伸
subplot(1,2,2);imshow(g);title("图像对比度拉伸");

%% 图像的直方图均衡
figure('name','图像的直方图均衡');
f = imread('tsy.jpg');
subplot(2,2,1),imshow(f),subplot(2,2,2),imhist(f);
ylim('auto'); % 自动设定纵坐标轴的取值范围和刻度线

g = histeq(f,256);% 直方图均衡
subplot(2,2,3),imshow(g),subplot(2,2,4),imhist(g);
ylim('auto'); % 自动设定纵坐标轴的取值范围和刻度线

%% 图像平滑
figure('name','图像平滑');
f = imread('tsy.jpg');
fg = im2gray(f);
subplot(2,2,1);imshow(fg);title("原灰度图(noise函数输入图像应为二维)");

fn = imnoise(fg, 'salt & pepper',0.2); % 加椒盐噪声
subplot(2,2,2);imshow(fn);title("椒盐噪声");

gm = medfilt2(fn);%中值滤波
subplot(2,2,3);imshow(gm);title("中值滤波");

gms = medfilt2(fn, 'symmetric');%消除边界效应中值滤波
subplot(2,2,4);imshow(gms);title("消除边界效应中值滤波");

%% 图像锐化
figure('name','图像锐化');
f = imread('tsy.jpg');
subplot(1,2,1);imshow(f);title("原图");

PQ = paddedsize(size(f));%计算扩充尺寸
D0 = 0.05*PQ(1); % 计算截止频率
H = hpfilter('gaussian',PQ(1),PQ(2),D0);
g = dftfilt(f,H);
subplot(1,2,2);imshow(g,[]);title("高斯高通滤波图像");

%% 图像傅里叶变换
figure('name','图像傅里叶变换');
f = imread('tsy.jpg');
f = im2gray(f);
subplot(3,2,1);imshow(f);title("原图");

F = fft2(f);% 傅里叶变换
S = abs(F);
subplot(3,2,2);imshow(S,[]);title("傅里叶频谱图像");

Fc = fftshift(F);
S = abs(Fc);
subplot(3,2,3);imshow(S);title("|Fc|");
subplot(3,2,4);imshow(S,[]);title("居中傅里叶频谱图像");

S = abs(F);
S2 = log(1+S);
subplot(3,2,5);imshow(S2,[]);title("对数变换视觉增强");

Fc = fftshift(F);
S = abs(Fc);
S2 = log(1+S);
subplot(3,2,6);imshow(S2,[]);title("对数变换视觉增强居中");

你可能感兴趣的:(数字图像处理,matlab,图像处理)