学习了数字图像处理后,完成了本次实验
目录
实验内容:
- 1.利用 matlab 自带的工具函数实现下列任务:
(1)计算图像的直方图,并以图形化方式绘制直方图。
(2)实现基于伽马变换的图像灰度变换。
(3)实现图像均衡化处理。
- 2.独立编程实现图像中值滤波算法(不考虑边界),并与 matlab 自带的中值滤波函数进
- 行对比(滤波模板大小自定,可为 3×3,5×5,7×7 等)。
实验1:直方图统计
原图:
matlab代码如下:
close all;clear all;clc;
f = imread('1.tif');
figure(1)
imhist(f),title('原图直方图')
得到图像1的直方图:
用不同方式表示直方图:
figure(2)
h1 = f(1:10:256);
horz = 1:10:256;
subplot(2,2,1)
imhist(h1)
subplot(2,2,2)
bar(horz,h1,0.5)
subplot(2,2,3)
stem(horz,h1,'fill')
subplot(2,2,4)
plot(imhist(h1))
四个图进行对比:
f1 = imread('1.tif');
f2 = imread('2.tif');
f3 = imread('3.tif');
f4 = imread('4.tif');
subplot(221)
imhist(f1),title('原图1直方图')
subplot(222)
imhist(f2),title('原图2直方图')
subplot(223)
imhist(f3),title('原图3直方图')
subplot(224)
imhist(f4),title('原图4直方图')
实验1完整代码:
close all;clear all;clc;
f = imread('1.tif');
figure(1)
imhist(f),title('原图直方图')
[counts, locations] = imhist(f);
figure(2)
h1 = f(1:10:256);
horz = 1:10:256;
subplot(2,2,1)
imhist(h1)
subplot(2,2,2)
bar(horz,h1,0.5)
subplot(2,2,3)
stem(horz,h1,'fill')
subplot(2,2,4)
plot(imhist(h1))
f1 = imread('1.tif');
f2 = imread('2.tif');
f3 = imread('3.tif');
f4 = imread('4.tif');
subplot(221)
imhist(f1),title('原图1直方图')
subplot(222)
imhist(f2),title('原图2直方图')
subplot(223)
imhist(f3),title('原图3直方图')
subplot(224)
imhist(f4),title('原图4直方图')
实验2:灰度变换
close all;clear ;clc;
f = imread('1.tif');
figure(1)
imhist(f),title('原图直方图')
figure(2)
subplot(131)
imshow(f),title('原图')
a = imadjust(f,[0.2;0.5],[0;1],1); %小于0.2灰度值设置为0,大于0.5灰度值设置为1
subplot(132)
imshow(a),title('[0.2;0.5],[0;1]')
a = imadjust(f,[0.2;0.5],[1;0],1); %小于0.2灰度值设置为1,大于0.5灰度值设置为0
subplot(133)
imshow(a),title('[0.2;0.5],[1;0]')
figure(3)
subplot(131)
imshow(f),title('原图')
a = imadjust(f,[0;0.4],[0;1],1); %第二个值越小越白
subplot(132)
imshow(a),title('[0;0.4],[0;1],1')
a = imadjust(f,[0.6;0.9],[0;1],1); %第一个值越大越黑
subplot(133)
imshow(a),title('[0.6;0.9],[0;1],1')
实验3:直方图均衡化
close all;clear ;clc;
%函数histeq()进行直方图均衡化处理
I=imread('1.tif');
J=histeq(I); %直方图均衡化
figure,
subplot(121),imshow(uint8(I));
title('原图')
subplot(122),imshow(uint8(J));
title('均衡化后')
figure,
subplot(121),imhist(I,64);
title('原图像直方图');
subplot(122),imhist(J,64);
title('均衡化后的直方图');
实验4:中值滤波
t=imread('img.tif');
figure(1)
subplot(2,2,1),imshow(t),title('原图')
t1=imnoise(t,'salt & pepper',0.3);
subplot(2,2,2),imshow(t1),title('加入椒盐噪声后')
t2=medfilt2(t1,[3 3]);
subplot(2,2,3),imshow(t2),title('matlab自带函数中值滤波后')
t2=median_filter(t1,[3,3]);%调用函数
subplot(2,2,4),imshow(t2),title('自己的函数中值滤波后')
function [ img ] = median_filter( image, n )
[ height, width ] = size(image);
x1 = double(image);
x2 = x1;
for i = 1: height-n+1
for j = 1:width-n+1
mb = x1( i:(i+n-1), j:(j+n-1) );%获取图像中n*n的矩阵
mb = mb(:);%将mb变成向量化,变成一个列向量
mm = median(mb);%取中间值
x2( i+(n-1)/2, j+(n-1)/2 ) = mm;
end
end
img = uint8(x2);
end