图像时域滤波(二)
2017-09-27 09:30
197 查看
1.灰度变换
% 载入图像
fileurl = fullfile(pwd, 'images', 'IM1.dcm');
X = dicomread(fileurl);
X = im2uint8(mat2gray(X));
% 拷贝副本
Y1 = X;
Y1(X < 0.2*255) = 0.2*255;
Y1(X > 0.8*255) = 0.8*255;
% 二值化
Y2 = im2bw(X);
% log处理
Y3 = log(double(X));
% 直方图均衡化
Y4 = histeq(X);
% 显示
figure; imshow(X, []); title('原图');
figure; imshow(Y1, []); title('灰度压缩');
figure; imshow(Y2); title('二值化');
figure; imshow(Y3, []); title('非线性log');
figure; imshow(Y4, []); title('直方图均衡化');
2.直方图统计
I = imread('rice.png');
% 矩形框
rect = [10 10 20 20];
% 图像裁剪
J = imcrop(I, rect);
% 默认的直方图统计
[counts, x] = imhist(J);
figure;
subplot(2, 2, 1); imshow(I, []); title('原图像');
rectangle('Position', rect, 'EdgeColor', 'r');
subplot(2, 2, 2); imshow(J, []); title('裁剪图像');
subplot(2, 2, 3); imhist(I); axis auto; title('原图像直方图')
subplot(2, 2, 4); imhist(J); axis auto; title('裁剪图像直方图');
figure;
subplot(2, 2, 1); imshow(I, []); title('原图像');
rectangle('Position', rect, 'EdgeColor', 'r');
subplot(2, 2, 2); imshow(J, []); title('裁剪图像');
subplot(2, 2, 3); imhist(J); axis auto; title('默认的裁剪直方图');
subplot(2, 2, 4); plot(x, counts); axis auto;
set(gca, 'YLim', [0 30])
title('频数统计的直方图');
3.平滑滤波
clc; clear all; close all;
I = imread('rice.png');
figure; imshow(I); title('原图像')
I = double(I);
h = [1 1 1
1 1 1
1 1 1]/9;
J = imfilter(I, h, 'replicate');
figure; imshow(mat2gray(J)); title('均值滤波');
S = medfilt2(I);
figure; imshow(mat2gray(S)); title('中值滤波');
4.锐化滤波
clc; clear all; close all;
I = imread('rice.png');
figure; imshow(I); title('原图像');
I = double(I);
h = [1 2 1
0 0 0
-1 -2 -1];
J = imfilter(I, h, 'replicate');
figure; imshow(mat2gray(J)); title('sobel滤波器');
h2 = [0.1667 0.6667 0.1667
0.6667 -3.3333 0.6667
0.1667 0.6667 0.1667];
S = imfilter(I, h2, 'replicate');
figure; imshow(mat2gray(S)); title('拉普拉斯滤波器');
5.混合图像增强
clc; clear all; close all;
% 加载图像
J = imread(fullfile(pwd, 'images', 'ct.tif'));
if ndims(J) == 3
J = rgb2gra增强y(J);
end
% double变换
I = im2double(J);
% 拉普拉斯变换得到M1
h1 = fspecial('laplacian');
M1 = imfilter(I,h1);
% 与原图相加得到M2,锐化原图
M2 = M1+I;
% sobel
h2 = fspecial('sobel')';
h3 = fspecial('sobel');
Sx = imfilter(I, h2);
Sy = imfilter(I, h3);
% 梯度
M3 = sqrt(Sx.^2 + Sy.^2);
% 均值
h4 = fspecial('average');
M4 = imfilter(I,h4);
M5 = M2.*M4;
M6 = I+M5;
% 灰度拉伸
M7 = 2*M6.^1.15;
figure;
subplot(241);imshow(I);title('原图像');
subplot(242);imshow(M1);title('拉普拉斯变换');
subplot(243);imshow(M2);title('锐化结果1');
subplot(244);imshow(M3);title('梯度');
subplot(245);imshow(M4);title('均值');
subplot(246);imshow(M5);title('锐化结果2');
subplot(247);imshow(M6);title('锐化增强');
subplot(248);imshow(M7);title('灰度拉伸');
res = M7;
figure; imshow(I); title('原图像');
figure; imshow(res); title('混合增强的效果');
% 载入图像
fileurl = fullfile(pwd, 'images', 'IM1.dcm');
X = dicomread(fileurl);
X = im2uint8(mat2gray(X));
% 拷贝副本
Y1 = X;
Y1(X < 0.2*255) = 0.2*255;
Y1(X > 0.8*255) = 0.8*255;
% 二值化
Y2 = im2bw(X);
% log处理
Y3 = log(double(X));
% 直方图均衡化
Y4 = histeq(X);
% 显示
figure; imshow(X, []); title('原图');
figure; imshow(Y1, []); title('灰度压缩');
figure; imshow(Y2); title('二值化');
figure; imshow(Y3, []); title('非线性log');
figure; imshow(Y4, []); title('直方图均衡化');
2.直方图统计
I = imread('rice.png');
% 矩形框
rect = [10 10 20 20];
% 图像裁剪
J = imcrop(I, rect);
% 默认的直方图统计
[counts, x] = imhist(J);
figure;
subplot(2, 2, 1); imshow(I, []); title('原图像');
rectangle('Position', rect, 'EdgeColor', 'r');
subplot(2, 2, 2); imshow(J, []); title('裁剪图像');
subplot(2, 2, 3); imhist(I); axis auto; title('原图像直方图')
subplot(2, 2, 4); imhist(J); axis auto; title('裁剪图像直方图');
figure;
subplot(2, 2, 1); imshow(I, []); title('原图像');
rectangle('Position', rect, 'EdgeColor', 'r');
subplot(2, 2, 2); imshow(J, []); title('裁剪图像');
subplot(2, 2, 3); imhist(J); axis auto; title('默认的裁剪直方图');
subplot(2, 2, 4); plot(x, counts); axis auto;
set(gca, 'YLim', [0 30])
title('频数统计的直方图');
3.平滑滤波
clc; clear all; close all;
I = imread('rice.png');
figure; imshow(I); title('原图像')
I = double(I);
h = [1 1 1
1 1 1
1 1 1]/9;
J = imfilter(I, h, 'replicate');
figure; imshow(mat2gray(J)); title('均值滤波');
S = medfilt2(I);
figure; imshow(mat2gray(S)); title('中值滤波');
4.锐化滤波
clc; clear all; close all;
I = imread('rice.png');
figure; imshow(I); title('原图像');
I = double(I);
h = [1 2 1
0 0 0
-1 -2 -1];
J = imfilter(I, h, 'replicate');
figure; imshow(mat2gray(J)); title('sobel滤波器');
h2 = [0.1667 0.6667 0.1667
0.6667 -3.3333 0.6667
0.1667 0.6667 0.1667];
S = imfilter(I, h2, 'replicate');
figure; imshow(mat2gray(S)); title('拉普拉斯滤波器');
5.混合图像增强
clc; clear all; close all;
% 加载图像
J = imread(fullfile(pwd, 'images', 'ct.tif'));
if ndims(J) == 3
J = rgb2gra增强y(J);
end
% double变换
I = im2double(J);
% 拉普拉斯变换得到M1
h1 = fspecial('laplacian');
M1 = imfilter(I,h1);
% 与原图相加得到M2,锐化原图
M2 = M1+I;
% sobel
h2 = fspecial('sobel')';
h3 = fspecial('sobel');
Sx = imfilter(I, h2);
Sy = imfilter(I, h3);
% 梯度
M3 = sqrt(Sx.^2 + Sy.^2);
% 均值
h4 = fspecial('average');
M4 = imfilter(I,h4);
M5 = M2.*M4;
M6 = I+M5;
% 灰度拉伸
M7 = 2*M6.^1.15;
figure;
subplot(241);imshow(I);title('原图像');
subplot(242);imshow(M1);title('拉普拉斯变换');
subplot(243);imshow(M2);title('锐化结果1');
subplot(244);imshow(M3);title('梯度');
subplot(245);imshow(M4);title('均值');
subplot(246);imshow(M5);title('锐化结果2');
subplot(247);imshow(M6);title('锐化增强');
subplot(248);imshow(M7);title('灰度拉伸');
res = M7;
figure; imshow(I); title('原图像');
figure; imshow(res); title('混合增强的效果');
相关文章推荐
- 【图像处理】时域最小二乘逆滤波的最优化快速解法
- 【图像处理】时域最小二乘逆滤波
- 对图像进行各种滤波的编程思路
- 图像预处理——滤波
- 图像卷积与滤波的一些知识点
- 图像处理之双边滤波介绍与源码实现
- 数字图像处理 平滑滤波
- 第6章 图像处理_6.2非线性滤波:中值滤波、双边滤波
- 图像处理-线性滤波-2 图像微分(1、2阶导数和拉普拉斯算子)
- 一个视频让你理解什么是时域,傅立叶级数,滤波等概念
- 图像增强之空间滤波
- HSI彩色图像空间滤波
- 高斯图像滤波原理及其编程离散化实…
- OpenCV-跟我学一起学数字图像处理之中值滤波
- OpenCV成长之路(7):图像滤波
- filter2D 图像滤波
- 灰度图像--频域滤波 傅里叶变换之离散时间傅里叶变换(DTFT)
- 灰度图像--空域滤波 基础:卷积和相关
- 数字图像处理—频域增强(低通滤波)(高通滤波)(带通和带阻滤波)(同态滤波)
- 灰度图像--图像增强 平滑之均值滤波、高斯滤波