图像时域滤波(二)

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('混合增强的效果');



你可能感兴趣的:(matlab)