Matlab DIP(瓦)ch3图像空间域滤波练习

      Matlab是一种图像视频处理实现的好工具,因为matlab是基于数组操作的,而一副图像就是一个数组。因此搞DIP或者CV的是一种必需掌握的工具。

      为了学会使用matlab在图像处理的基本应用。按照瓦萨雷斯数字图像处理Matlab版中的第三章,按照书本输了一遍。代码中用到的图片(网上可以下载)放在本目录的images中。没写什么注释,因为自己输入过程中看help命令和试验结果基本都可以弄懂,代码最后有一些小笔记。代码如下:

 

 

clc
clear

f=imread('.\images\dipum_images_ch03\Fig0303(a)(breast).tif');
subplot(2,3,1);
imshow(f)

g2=(imadjust(f,[0 1],[1 0]));
subplot(2,3,2);
imshow(g2)

g3=imcomplement(f);
subplot(2,3,3);
imshow(g3)

g4=imadjust(f,[0.5,0.75],[0 1]);
subplot(2,3,4);
imshow(g4)

g5=imadjust(f,[],[],2);
subplot(2,3,5);
imshow(g5)

g5=imadjust(f,[],[],0.8);
subplot(2,3,6);
imshow(g5)

clc
clear
f=imread('.\images\dipum_images_ch03\Fig0305(a)(spectrum).tif');
subplot(121),imshow(f),subplot(122),imhist(f),axis tight
g=im2uint8(mat2gray(log(1+double(f))));
figure,subplot(121),imshow(g),subplot(122),imhist(g),axis tight
title('ʹÓöÔÊý±ä»»¼õС¶¯Ì¬•¶Î§')

m=5;
E=10;
h=im2uint8(mat2gray(1./(1+(m./(double(f)+eps)).^E)));%ΪʲôÊÇÕâ¸ö¹«Ê½
figure,subplot(121),imshow(h),subplot(122),imhist(h),axis tight

clc
clear
f=imread('.\images\dipum_images_ch03\Fig0306(a)(bone-scan-GE).tif');
imshow(f)

g=intrans(f,'stretch',mean2(im2double(f)),0.9);
figure ,imshow(g)

clc
clear
f=[125.3 69.8 5.3 2.3];
g=uint8(f)
hf=gscale(f)
h1=gscale(g)
h2=gscale(g,'minmax',0.1,0.6)
class(h2)

clc
clear
f=imread('.\images\dipum_images_ch03\Fig0308(a)(pollen).tif');
subplot(121),imshow(f),subplot(122),imhist(f)
ylim('auto')

g1=histeq(f);
figure,subplot(121),imshow(g1),subplot(122),imhist(g1)
ylim('auto')

g2=histeq(f,8);
figure,subplot(121),imshow(g2),subplot(122),imhist(g2)
ylim('auto')

clc
clear
f = imread('.\images\dipum_images_ch03\Fig0310(a)(Moon Phobos).tif');
p = manualhist;
plot(p)
figure,subplot(121),imshow(f),subplot(122),imhist(f),ylim('auto')
g = histeq(f,p);
figure,subplot(121),imshow(g),subplot(122),imhist(g),ylim('auto')

clc
clear
f = imread('.\images\dipum_images_ch03\Fig0315(a)(original_test_pattern).tif');
f = im2double(f);
imshow(f)
w = ones(31)
gd = imfilter(f, w)
figure,imshow(gd,[])
%figure,imshow(gd)
gr = imfilter(f, w, 'replicate');
figure,imshow(gr,[])
gc = imfilter(f, w, 'circular');
f8 = im2uint8(f);
gr8 = imfilter(f8, w, 'replicate');
figure,imshow(gr8,[])


clc
clear
f = [1 2 3 4]
frb = padarray(f,[3 2],'replicate','both'); % ĬÈÏboth
g = colfilt(frb,[3 2],'sliding',@gmean);
size_g = size(g)


clc
clear
f = imread('.\images\dipum_images_ch03\Fig0316(a)(moon).tif');
imshow(f)
w = fspecial('laplacian',0);
g1 = imfilter(f,w,'replicate');
figure,imshow(g1,[])
f2 = im2double(f);
g2 = imfilter(f2,w,'replicate');
figure,imshow(g2,[])
g=f2-g2;
figure,imshow(g,[])

clc
clear
f = imread('.\images\dipum_images_ch03\Fig0316(a)(moon).tif');
imshow(f)
w4 = fspecial('laplacian',0);
w8 = [1 1 1; 1 -8 1; 1 1 1];
f = im2double(f);
g4 = f - imfilter(f,w4,'replicate');
g8 = f - imfilter(f,w8,'replicate');
figure,subplot(221)
imshow(imfilter(f,w4,'replicate'))
subplot(223)
imshow(imfilter(f,w8,'replicate'))
subplot(222)
imshow(imfilter(f,w4,'replicate'),[])
subplot(224)
imshow(imfilter(f,w8,'replicate'),[])
figure,subplot(221)
imshow(g4)
subplot(222),imshow(g4,[])
subplot(223),imshow(g4)
subplot(224),imshow(g8,[])

clc
clear
f = imread('.\images\dipum_images_ch03\Fig0318(a)(ckt-board-orig).tif');
subplot(121),imshow(f)
fn=imnoise(f,'salt & pepper',0.2);
subplot(122),imshow(fn)
gm=medfilt2(fn);
figure,subplot(121),imshow(gm)
gms=medfilt2(fn,'symmetric');
subplot(122),imshow(gms)

clc
clear
median(1:10)
median(1:11)
median(-2:-1:-10)
median(11:-1:1)
median(-20:-10)

 

 附小笔记:

  0. matlab版本2009a,matlab7.8的。f = imread('1.tif');imshow(f);imfinfo(f);这样用imfinfo报错,imfinfo参数直接写全名即f = imread('1.tif');imshow(f);imfinfo('1.tif');就不报错...难道有这么麻烦?要是路径名很长怎么 办。照理说应该可以用f当做参数直接用的,冈萨雷斯的matlab代码就是这样干的。

  1. 如果1.tif本来就是tif格式的,在windows下改成1.jpg可以显示。但是在matlab中f = imread('1.jpg');不成立。因为虽然后缀名是.jpg,但是其本质还是.tif.也就是说只能用f=imread(‘1.tif’);当然图片本身的名字也要求改为.tif.
  2. 如今换了版本R2011b的了
  3. 注意在matlab中是用’’单引号表示路径的,用\表示目录层次。
  4. imadjust(I,[LOW_IN; HIGH_IN],[LOW_OUT; HIGH_OUT]);这个函数是将LOW_IN映射到LOW_OUT,将HIGH_IN映射到HIGH_OUT,这些参数都是强度值,所以其大小在0到1之间。其它的截断。如果imadjust(f,[0,1],[1,0])则相当于imcomplement(f),相当于灰度图像反转。如果第4个参数gammar存在的话,则可以控制器映射曲线是凹凸型的,具体的见help命令。
  5. 如果想要将几幅图片显示在一起,可以用subplo(x,y,z)函数分屏,其中参数也可以不用逗号分开,显示。如果想用不同的坐标同时显示几幅图片,则可以用figure,加一个逗号即可。
  6. Imhist(f)是显示图像f的直方图。可用axis来设定坐标轴的范围。
  7. mat2gray是讲一个矩阵转换成一个强度图(但是什么叫强度图呢?)0对应的是黑色,1对应的是白色。Im2uint8是将一幅图转换成8为数据的整数图。可以将二进制图,强度图,RGB图等转换过去。可以用title对图像加标题。
  8. 所谓的模板,也指滤波器,核,掩膜或窗口
  9. 扩展工具箱中的intrans函数的功能是计算灰度或者强度图像的转换。
  10. class函数返回矩阵的类型名称,也就是说数据类型。
  11. gscale函数功能是将强度图像等映射到别的值范围。
  12. histeqs函数完成直方图均衡功能,其第二个参数表示均衡是bin的个数。可以使用函数xlim和ylim函数自动或者手动设置坐标轴的取值范围和刻度线。
  13. 为什么连续函数的cdf的pdf为均匀分布?可以通过简单的复合函数来证明。图像直方图均衡化的步骤一般是首先将图像直方图的归一化的累计直方图求出来,然后对应的乘以灰度级,而相应灰度级的值则由原图像相应灰度对应值整合而成。
  14. Im2double函数可以将图像转换成double类型。
  15. imfilter(f, w)是线性空间滤波函数,其中w为滤波模板。
  16. 可以用padarray函数来对图像进行扩展,第二个参数值扩展的行和列,即在原基础上增加的行和列,如果其方向是双向的话,则相当于增加了2倍。
  17. 可以用colfilt函数实现非线性滤波。
  18. 可以用fspecial函数指定滤波器核的类型和形状,比如Log和laplacian等。
  19. 中值滤波可以使用函数medfilt2,其默认大小为3*3.
  20. 可以用imnoise给图像加入噪声。
  21. 数字图像处理冈萨雷斯教材中所讲的亮度图像就是灰度图像。
  22. Matlab中所有的数值计算都可用double类来进行,所以它也是图像处理应用中最常见使用的数据类。
  23. 可以用whos f来查看图片的信息。
  24. Im2bw为将一图像转换成二值图像,可以自己设置阀值。
  25. linspace(a,b,n)将产生一个从a到b共n个元素,线性等分的。

 

 

 

 

 

原文地址:https://www.cnblogs.com/tornadomeet/p/2378134.html