[关闭]
@dragonfive 2016-03-10T07:44:45.000000Z 字数 18876 阅读 1237

数字图像处理课程笔记

计算机视觉


一些图像的像素范围映射变化

灰度平方运算

  1. % 下面是在做平方运算;
  2. figure;
  3. img=imread('E:\资料\onedrive\code\test\image\lenargb.jpg');
  4. img=rgb2gray(img); %matlab把真彩图转化为灰度图;
  5. img=mat2gray(img); %读入的图片刚开始是uint8的,然后转化为double类型之后才能用mat2gray转化为0-1double
  6. i=1;
  7. for c=[0.04,0.1,0.2,0.4,0.67,1,1.5,2.5,5.0,10.0,25.0]
  8. y=img.^c;
  9. subplot(3,4,i); %把多个图片放在一起的方法;
  10. imshow(y);
  11. i=i+1;
  12. %hold on
  13. end

untitled.jpg

untitled.jpg

灰度区间映射

matlab中的imadjust函数可以将灰度在一定范围内的图像到另一个区间里面

  1. g=imadjust(f,[low_in,high_in],[low_out,high_out],gamma)

Paste_Image.png
在[0-1]范围内做平方,能调整灰度范围

  1. %下面是测试matlab画曲线
  2. x=0:0.01:1;
  3. for c=[0.04,0.1,0.2,0.4,0.67,1,1.5,2.5,5.0,10.0,25.0]
  4. y=x.^c;
  5. plot(x,y);
  6. hold on;
  7. end

Paste_Image.png

  1. % 下面测试用imadjust方法做灰度调整
  2. img=imread('E:\资料\onedrive\code\test\image\result.png');
  3. class(img)
  4. figure;
  5. subplot(1,2,1);imshow(img);
  6. img=imadjust(mat2gray(img),[0,1],[0.4,0.6]); %这个函数对于输入图像不要求其类型为[0,1]的double
  7. subplot(1,2,2);imshow(img);

untitled.jpg

灰度直方图均衡化

  1. % 下面做灰度直方图均衡和分配
  2. f=imread('E:\资料\onedrive\code\test\image\Fig0206(a)(rose-original).tif');
  3. imshow(f);figure;imhist(f);
  4. %ylim('auto');
  5. g=histeq(f,256); %进行直方图均衡;
  6. figure;imshow(g);
  7. figure;imhist(g);
  8. %ylim('atuo'); %sets the axis limit mode to auto.
  9. hnorm=imhist(f)./numel(f); %归一化直方图
  10. cdf=cumsum(hnorm); %累加
  11. x=linspace(0,1,256); %在01之间产生256个等距离的点;
  12. figure;
  13. plot(x,cdf);
  14. axis([0 1 0 1]);

log变换

  1. % 下面进行log变换
  2. %Fig0206(a)(rose-original).tif
  3. %Fig0305(a)(spectrum).tif
  4. f=imread('E:\资料\onedrive\code\test\image\Fig0305(a)(spectrum).tif');
  5. subplot(2,2,1);imshow(f);
  6. g=im2uint8(mat2gray(log(1+double(f))));%进行log变换
  7. %g=mat2gray(log(1+double(f)));
  8. %h=histeq(f,256); %进行直方图均衡;
  9. %j=histeq(g,256);
  10. subplot(2,2,3);imhist(f);%画出直方图
  11. subplot(2,2,4);imhist(g);

untitled.png
untitled.png

相对拉伸变换

subplot(2,2,2);imshow(g);
Paste_Image.png

  1. % 下面进行相对伸展变换
  2. %Fig0206(a)(rose-original).tif
  3. %Fig0305(a)(spectrum).tif
  4. %Fig0308(a)(pollen).tif
  5. %下面先画出拉伸变换的图像;
  6. m=0.5;
  7. A=[0:1:10];
  8. B=[10:10:100];
  9. r=[0:0.01:1];
  10. for E=[A B]
  11. s=1./(1+(m./r).^E);
  12. subplot(2,2,1);plot(r,s);
  13. axis([0 1 0 1])
  14. set(gca,'xtick',0:0.1:1);
  15. set(gca,'ytick',0:0.1:1);
  16. xlabel('x');
  17. ylabel('y');
  18. hold on
  19. end
  20. %s=1./(1+(m./r).^E)
  21. %plot(r,s);
  22. %下面对图像进行操作
  23. f=imread('E:\资料\onedrive\code\test\image\Fig0308(a)(pollen).tif');
  24. E=2;
  25. s=1./(1+(m./r).^E);
  26. subplot(2,2,2);plot(r,s);
  27. title('函数图像') %添加图像标题
  28. text(0.5,0.5,'E=2') %将cosx这个注解加到坐标中的某个位置
  29. %gtext('s=1./(1+(m./r).^E)') % 用鼠标的光标定位,将sinx这个注解放在你鼠标点击的地方
  30. s=1./(1+(m./double(f)+eps).^E);
  31. subplot(2,2,3);imshow(f);title('原图')
  32. subplot(2,2,4);imshow(mat2gray(s));title('扩展后的图')

untitled.png

画出自适应的坐标轴

  1. % 画出自适应的坐标轴
  2. function myplot(x,y)
  3. % xyplot Plot 2D axes through the origin
  4. % Example 1:
  5. % t = linspace(0,2*pi,500);
  6. % y1 = 80*sin(t);
  7. % y2 = 100*cos(t);
  8. % xyplot(t,[y1;y2])
  9. %
  10. % Example 2:
  11. % x = -2*pi:pi/10:2*pi;
  12. % y = sin(x);
  13. % plot(x,y)
  14. % xyplot
  15. % PLOT
  16. if nargin>0
  17. if nargin == 2
  18. plot(x,y);
  19. else
  20. display(' Not 2D Data set !')
  21. end
  22. end
  23. hold on;
  24. % GET TICKS
  25. X=get(gca,'Xtick');
  26. Y=get(gca,'Ytick');
  27. % GET LABELS
  28. XL=get(gca,'XtickLabel');
  29. YL=get(gca,'YtickLabel');
  30. % GET OFFSETS
  31. Xoff=diff(get(gca,'XLim'))./40;
  32. Yoff=diff(get(gca,'YLim'))./40;
  33. % DRAW AXIS LINEs
  34. plot(get(gca,'XLim'),[0 0],'k');
  35. plot([0 0],get(gca,'YLim'),'k');
  36. % Plot new ticks
  37. for i=1:length(X)
  38. plot([X(i) X(i)],[0 Yoff],'-k');
  39. end;
  40. for i=1:length(Y)
  41. plot([Xoff, 0],[Y(i) Y(i)],'-k');
  42. end;
  43. % ADD LABELS
  44. text(X,zeros(size(X))-2.*Yoff,XL);
  45. text(zeros(size(Y))-3.*Xoff,Y,YL);
  46. box off;
  47. % axis square;
  48. axis off;
  49. set(gcf,'color','w');
  50. end

空域滤波  

基础知识

输出图像是对输入图像的像素做简单的运算得到的,邻域像素也会参与运算
可以是线性运算,也可以是非线性运算

线性滤波函数

  1. g = imfilter(f, w, ltering mode,boundary options, size options)

计算过程中使用的是double, 但是imfilter的结果要保证输出图像和输入图像的数据类型一致,这样可能会在最后转换的时候出现截断的情况,所以正确的做法应该是在调用函数之前将图像转化为double的格式。

Paste_Image.png

  1. fspecial('type', parameters)

这个函数可以产生线性滤波器,
type取值:
Paste_Image.png

拉普拉斯滤波器

Paste_Image.png

用拉普拉斯滤波器检测边缘来做图像增强,图像锐化。
Paste_Image.png

  1. %下面做拉普拉斯空域滤波
  2. f=imread('E:\资料\onedrive\code\test\image\Fig0316(a)(moon).tif');
  3. w4=fspecial('laplacian',0);
  4. w8=[1 1 1;1 -8 1;1 1 1] ;%自己生成一个矩阵做算子;
  5. g1=imfilter(f,w4,'replicate');%拉普拉斯边缘检测结果;
  6. g2=imfilter(f,w8,'replicate');
  7. douf=im2double(f);%转换为double类型
  8. g3=imfilter(douf,w4,'replicate');
  9. g4=imfilter(douf,w8,'replicate');
  10. g5=f-g1;g6=f-g2;g7=douf-g3;g8=douf-g4;
  11. subplot(2,5,1);imshow(f);
  12. subplot(2,5,2);imshow(douf);
  13. for i=3:10
  14. subplot(2,5,i),imshow(eval(['g',num2str(i-2)]));%把字符串当作变量名
  15. end

untitled.png

拉普拉斯变换算子的矩阵的所有元素的和为1,而梯度算子的所有元素的和为0

非线性空域滤波

colfilt方法比nlfilter方法更快,在使用cofit方法之前,我们需要对图像进行装填。
在图像处理中非空域里面使用最多的滤波器是统计顺序滤波器,使用最多的函数是ordfilt2

g=ordfilt2(f,order,domain)
通过使用在邻域中的从小到大排序在第order个的元素来替换原图中的每一个元素,邻域的大小由domain确定

  1. g=ordfilt2(f,median(1:m*n),ones(m,n))

这里的median函数用来产生中位数;比如:

  1. g=ordfilt2(f,1,ones(m,n)); %用邻域中最小的元素替代
  2. g=ordfilt2(f,m*n,ones(m,n)); %用邻域中最大的元素替代
  3. g=ordfilt2(f,median(1:m*n),ones(m,n)); %用邻域中中位数元素替代

在统计顺序滤波中最常使用的是中位数滤波,可以用来进行降噪处理;

  1. g=medfilt2(f,[m,n],padopt);

在m*n的邻域中寻找中位数来替代中间元素,padopt是图像边缘的填充方式;

频域滤波

DFT:discrete Fouriter Transform离散傅立叶变换
傅立叶表换具有唯一性。傅立叶变换揭示了信号的时域特性和频域特性之间的确定的内在联系。
在图像里面时域特性表现为空域特性。
Paste_Image.png

一个例子

下面是用傅立叶与逆傅立叶变换操作图像

  1. % 下面求图像的傅立叶变换频谱
  2. f=imread('E:\资料\onedrive\code\test\image\Fig0403(a)(image).tif');
  3. F=fft2(f);
  4. S=abs(F); % F的频谱
  5. w=2;h=3;i=0;
  6. i=i+1;subplot(w,h,i);imshow(f,[]);
  7. i=i+1;subplot(w,h,i);imshow(S,[]);
  8. % imshow(K)与imshow(K,[])的区别;
  9. % imshow(K)直接显示K;
  10. % imshow(K,[])显示K,并将K的最大值和最小值分别作为纯白(255)和纯黑(0),中间的K值映射为0255之间的标准灰度值。
  11. %fftshift移动零频点到频谱中间,重新排列fft,fft2fftn的输出结果。
  12. Fc = fftshift(F);
  13. i=i+1;subplot(w,h,i);imshow(abs(Fc),[]);
  14. %log变换将像素范围压缩;
  15. S2=log(1+abs(Fc));
  16. i=i+1;subplot(w,h,i);imshow(S2,[]);
  17. f=real(ifft2(F));% 逆傅立叶变换;逆变换后取得实部就好
  18. %这次变换中傅立叶变换后没有做什么操作,所以逆傅立叶变换后还是原来的结果;
  19. i=i+1;subplot(w,h,i);imshow(f,[]);

显示的优化

从上面的代码我们看到,在一个窗口输出多个图片,需要自己设置位置什么的,但是刚开始我们不知道最终要显示多少行多少列的图片,而且随着图片的增加需要经常修改显示的参数。同时如果调换显示顺序和插入显示都需要调整很多代码,基于这些不便,我自己写了个imshow函数。程序会一直保存你要输出的图像,知道输入0,把图像全部输出出来,同时能够实现自适应。

  1. function myImshow(img,argTitle)
  2. persistent myNumOfImg %记录最终需要的图片个数;
  3. persistent M %记录所有的图片
  4. persistent Title ; % 保存标题;
  5. if isempty(myNumOfImg)
  6. myNumOfImg=0;
  7. M=cell(0);
  8. Title=cell(0);
  9. end
  10. if isequal(img,0) %表示结束
  11. h=ceil(sqrt(myNumOfImg)); %宽
  12. w=ceil(myNumOfImg*1.0/h); %高
  13. for i=1:myNumOfImg
  14. subplot(w,h,i);
  15. imshow(M{i},[]);
  16. title(Title{i});
  17. end
  18. clear myNumOfImg;clear Title;clear M;
  19. else
  20. myNumOfImg=myNumOfImg+1;
  21. M{myNumOfImg}=img;
  22. if nargin==2
  23. Title{myNumOfImg}=argTitle;
  24. else
  25. Title{myNumOfImg}='';
  26. end
  27. end

运行示例

  1. clear all;
  2. close all;
  3. f=imread('E:\资料\onedrive\code\test\image\Fig0403(a)(image).tif');
  4. F=fft2(f);
  5. S=abs(F); % F的频谱
  6. myImshow(f);
  7. myImshow(S);
  8. % w=2;h=3;i=0;
  9. % i=i+1;subplot(w,h,i);imshow(f,[]);
  10. % i=i+1;subplot(w,h,i);imshow(S,[]);
  11. % imshow(K)与imshow(K,[])的区别;
  12. % imshow(K)直接显示K;
  13. % imshow(K,[])显示K,并将K的最大值和最小值分别作为纯白(255)和纯黑(0),中间的K值映射为0255之间的标准灰度值。
  14. %fftshift移动零频点到频谱中间,重新排列fft,fft2fftn的输出结果。
  15. Fc = fftshift(F);
  16. % i=i+1;subplot(w,h,i);imshow(abs(Fc),[]);
  17. myImshow(abs(Fc));
  18. %log变换将像素范围压缩;
  19. S2=log(1+abs(Fc));
  20. % i=i+1;subplot(w,h,i);imshow(S2,[]);
  21. myImshow(S2);
  22. f=real(ifft2(F));% 逆傅立叶变换;
  23. %这次变换中傅立叶变换后没有做什么操作,所以逆傅立叶变换后还是原来的结果;
  24. % i=i+1;subplot(w,h,i);imshow(f,[]);
  25. myImshow(f);
  26. myImshow(0);

运行结果

untitled.png

相关参考

http://yunniyu.blog.163.com/blog/static/22208431201261665217676
如何在matlab里面实现静态变量
matlab中cell的使用方法
这个说明可以使用cell来建立三维数组,低两维的维数可以不一样
matlab如何清空程序运行过程中产生的矩阵
因为这里我们的程序用到了静态变量,所以需要学会清空

傅立叶变换滤波

滤波步骤

  1. 获得填充大小:PQ=paddedSize(size(f))
  2. 获得填充后的傅立叶变换 F=fft2(f,PQ(1),PQ(2))
  3. 生成一个滤波器,大小为PQ(1)*PQ(2),需要使用fftshift把重心的波峰移到边角处
  4. 频域点乘 G=H.*F
  5. 获得逆傅立叶变换出的图像的实部g=real(ifft2(G))
  6. 恢复图像原始大小 g=g(1:size(f,1),1:size(f,2))

从空域滤波生成频域滤波

下面测试几个基础函数的使用效果

  1. % % % % % % % % % % % % % % % %% % % % % % % % % % % % % % % %
  2. % 测试几个频域常用的函数的用法
  3. % % % % % % % % % % % % % % % %% % % % % % % % % % % % % % % %
  4. clear all;
  5. close all;
  6. % 测试gscale 测试freqz2 测试fftshiftifftshift
  7. f=imread('E:\资料\onedrive\code\test\image\Fig0409(a)(bld).tif');
  8. F=fft2(f);
  9. S=fftshift(log(1+abs(F)));
  10. %subplot(1,3,1);imshow(S);
  11. %subplot(1,3,2);imshow(S,[]);
  12. S=gscale(S); % 测试得这个函数的作用是让频谱自适应地显示出来;
  13. %subplot(1,3,3);imshow(S);
  14. w=1;h=3;i=0;
  15. % 由空域滤波器生成频域滤波器 对比fftshiftifftshift
  16. H=fspecial('sobel'); %梯度算子
  17. H3=freqz2(H); %由空域滤波器生成频域滤波器;
  18. i=i+1;subplot(w,h,i);mesh(abs(H3));%生成函数图像?
  19. % figure;
  20. H1=ifftshift(H3);
  21. i=i+1;subplot(w,h,i);mesh(abs(H1));
  22. H2=fftshift(H3);
  23. i=i+1;subplot(w,h,i);mesh(abs(H2));
  24. %view(45,30); %设置看图像的视角;第一个是左右倾斜度,第二个是上下倾斜度
  25. %我就感觉这个ifftshiftfftshift的效果是一样的
  1. 测试后发现矩阵经过gscale后的效果与imshow(f,[])的效果一致。但是明显gscale适用范围更广,可以先把这个矩阵标准化,进行相应操作而不是简简单单的输出。
  2. freqz2:frequcy表示生成频域滤波,用空域滤波生成频域滤波
  3. fftshift:表示把位于中间的波峰移动边上,大概是这个意思?
  4. mesh:二维显示频域函数
  5. view:调整观测角度

paddedsize函数实现

计算傅立叶滤波需要的填充大小

  1. function PQ = paddedsize(AB, CD, PARAM)
  2. %PADDEDSIZE Computes padded sizes useful for FFT-based filtering.
  3. % PQ = PADDEDSIZE(AB), where AB is a two-element size vector,
  4. % computes the two-element size vector PQ = 2*AB.
  5. %
  6. % PQ = PADDEDSIZE(AB, 'PWR2') computes the vector PQ such that
  7. % PQ(1) = PQ(2) = 2^nextpow2(2*m), where m is MAX(AB).
  8. %
  9. % PQ = PADDEDSIZE(AB, CD), where AB and CD are two-element size
  10. % vectors, computes the two-element size vector PQ. The elements
  11. % of PQ are the smallest even integers greater than or equal to
  12. % AB + CD - 1.
  13. %
  14. % PQ = PADDEDSIZE(AB, CD, 'PWR2') computes the vector PQ such that
  15. % PQ(1) = PQ(2) = 2^nextpow2(2*m), where m is MAX([AB CD]).
  16. % Copyright 2002-2004 R. C. Gonzalez, R. E. Woods, & S. L. Eddins
  17. % Digital Image Processing Using MATLAB, Prentice-Hall, 2004
  18. % $Revision: 1.5 $ $Date: 2003/08/25 14:28:22 $
  19. if nargin == 1
  20. PQ = 2*AB;
  21. elseif nargin == 2 & ~ischar(CD)
  22. PQ = AB + CD - 1;
  23. PQ = 2 * ceil(PQ / 2);
  24. elseif nargin == 2
  25. m = max(AB); % Maximum dimension.
  26. % Find power-of-2 at least twice m.
  27. P = 2^nextpow2(2*m);
  28. PQ = [P, P];
  29. elseif nargin == 3
  30. m = max([AB CD]); % Maximum dimension.
  31. P = 2^nextpow2(2*m);
  32. PQ = [P, P];
  33. else
  34. error('Wrong number of inputs.')
  35. end

gscale函数的实现

代码来自冈萨雷斯,输出和输入的图像格式一样。效果是规格化图像元素的像素值

  1. function g = gscale(f, varargin)
  2. %GSCALE Scales the intensity of the input image.
  3. % G = GSCALE(F, 'full8') scales the intensities of F to the full
  4. % 8-bit intensity range [0, 255]. This is the default if there is
  5. % only one input argument.
  6. %
  7. % G = GSCALE(F, 'full16') scales the intensities of F to the full
  8. % 16-bit intensity range [0, 65535].
  9. %
  10. % G = GSCALE(F, 'minmax', LOW, HIGH) scales the intensities of F to
  11. % the range [LOW, HIGH]. These values must be provided, and they
  12. % must be in the range [0, 1], independently of the class of the
  13. % input. GSCALE performs any necessary scaling. If the input is of
  14. % class double, and its values are not in the range [0, 1], then
  15. % GSCALE scales it to this range before processing.
  16. %
  17. % The class of the output is the same as the class of the input.
  18. % Copyright 2002-2004 R. C. Gonzalez, R. E. Woods, & S. L. Eddins
  19. % Digital Image Processing Using MATLAB, Prentice-Hall, 2004
  20. % $Revision: 1.5 $ $Date: 2003/11/21 14:36:09 $
  21. if length(varargin) == 0 % If only one argument it must be f.
  22. method = 'full8';
  23. else
  24. method = varargin{1};
  25. end
  26. if strcmp(class(f), 'double') & (max(f(:)) > 1 | min(f(:)) < 0)
  27. f = mat2gray(f);
  28. end
  29. % Perform the specified scaling.
  30. switch method
  31. case 'full8'
  32. g = im2uint8(mat2gray(double(f)));
  33. case 'full16'
  34. g = im2uint16(mat2gray(double(f)));
  35. case 'minmax'
  36. low = varargin{2}; high = varargin{3};
  37. if low > 1 | low < 0 | high > 1 | high < 0
  38. error('Parameters low and high must be in the range [0, 1].')
  39. end
  40. if strcmp(class(f), 'double')
  41. low_in = min(f(:));
  42. high_in = max(f(:));
  43. elseif strcmp(class(f), 'uint8')
  44. low_in = double(min(f(:)))./255;
  45. high_in = double(max(f(:)))./255;
  46. elseif strcmp(class(f), 'uint16')
  47. low_in = double(min(f(:)))./65535;
  48. high_in = double(max(f(:)))./65535;
  49. end
  50. % imadjust automatically matches the class of the input.
  51. g = imadjust(f, [low_in high_in], [low high]);
  52. otherwise
  53. error('Unknown method.')
  54. end

dftfilt函数

函数的效果是合并了傅立叶变换滤波中的
2. 填充原始图像并进行傅立叶变换
4. 进行滤波
5. 傅立叶反变换并获得实部
6. 剪切成原始尺寸

所以剩下需要做的就是生成一个合适尺寸的频域滤波器并移动规范化

  1. function g = dftfilt(f, H)
  2. %DFTFILT Performs frequency domain filtering.
  3. % G = DFTFILT(F, H) filters F in the frequency domain using the
  4. % filter transfer function H. The output, G, is the filtered
  5. % image, which has the same size as F. DFTFILT automatically pads
  6. % F to be the same size as H. Function PADDEDSIZE can be used to
  7. % determine an appropriate size for H.
  8. %
  9. % DFTFILT assumes that F is real and that H is a real, uncentered
  10. % circularly-symmetric filter function.
  11. % Copyright 2002-2004 R. C. Gonzalez, R. E. Woods, & S. L. Eddins
  12. % Digital Image Processing Using MATLAB, Prentice-Hall, 2004
  13. % $Revision: 1.5 $ $Date: 2003/08/25 14:28:22 $
  14. % Obtain the FFT of the padded input.
  15. F = fft2(f, size(H, 1), size(H, 2));
  16. % Perform filtering.
  17. g = real(ifft2(H.*F));
  18. % Crop to original size.
  19. g = g(1:size(f, 1), 1:size(f, 2));

空域生成频域的例子

  1. clear all;
  2. close all;
  3. f=imread('E:\资料\onedrive\code\test\image\Fig0409(a)(bld).tif');
  4. w=1;h=3;i=0;
  5. h=fspecial('sobel'); %梯度算子
  6. PQ=paddedsize(size(f));
  7. H3=freqz2(h,PQ(1),PQ(2)); %由空域滤波器生成频域滤波器;
  8. H1=fftshift(H3);
  9. g1=dftfilt(f,H1);
  10. g2=imfilter(double(f),h);
  11. myImshow(g1);myImshow(g2);myImshow(0);

运行结果

untitled.png
从结果可以看到空域滤波和频域滤波效果差不多;
但是时间上频域滤波用时:0.044662 seconds.
空域滤波用时.046712 seconds.
这便是频域滤波的优势所在。

直接生成频域滤波

理想低通滤波器
Paste_Image.png
Paste_Image.png

巴特沃兹低通滤波器
Paste_Image.png

高斯低通滤波器
Paste_Image.png

dftuv的实现

dftuv函数的作用是 生成频域的距离向量组。这个距离是距离频域中心的距离

  1. function [U, V] = dftuv(M, N)
  2. %DFTUV Computes meshgrid frequency matrices.
  3. % [U, V] = DFTUV(M, N) computes meshgrid frequency matrices U and
  4. % V. U and V are useful for computing frequency-domain filter
  5. % functions that can be used with DFTFILT. U and V are both
  6. % M-by-N.
  7. % Copyright 2002-2004 R. C. Gonzalez, R. E. Woods, & S. L. Eddins
  8. % Digital Image Processing Using MATLAB, Prentice-Hall, 2004
  9. % $Revision: 1.3 $ $Date: 2003/04/16 22:30:34 $
  10. % Set up range of variables.
  11. u = 0:(M - 1);
  12. v = 0:(N - 1);
  13. % Compute the indices for use in meshgrid.
  14. idx = find(u > M/2);
  15. u(idx) = u(idx) - M;
  16. idy = find(v > N/2);
  17. v(idy) = v(idy) - N;
  18. % Compute the meshgrid arrays.
  19. [V, U] = meshgrid(v, u);

直接用频域高斯低通滤波的例子

  1. clear all;
  2. close all;
  3. f=imread('E:\资料\onedrive\code\test\image\Fig0413(a)(original_test_pattern).tif');
  4. PQ=paddedsize(size(f));
  5. % 生成低通滤波器(注意大小) 移动
  6. [U,V]=dftuv(PQ(1),PQ(2));
  7. D0=0.05*PQ(2);
  8. H=exp(-1*(U.^2+V.^2)/(2*D0.^2));
  9. H1=fftshift(H);
  10. % 滤波输出结果
  11. g=dftfilt(f,H);
  12. f1=fft2(f);
  13. myImshow(f);myImshow(H1); myImshow(g);myImshow(0);

运行结果

untitled.png

直接生成频域高通滤波

高通滤波器可以直接从低通滤波器获得
Paste_Image.png

理想高通滤波器
Paste_Image.png

波特沃兹高通滤波器
Paste_Image.png

高斯高通滤波器
Paste_Image.png  

用频域波特沃兹高通滤波的例子

% % % % % % % % % % % % % % % %% % % % % % % % % % % % % % % %
% 一个频域高斯低通滤波的例子
% % % % % % % % % % % % % % % %% % % % % % % % % % % % % % % %
% 读取图片

clear all;
close all;
f=imread('E:\资料\onedrive\code\test\image\Fig0413(a)(original_test_pattern).tif');
% 生成高通滤波器
PQ=paddedsize(size(f));
[U,V]=dftuv(PQ(1),PQ(2));
D0=[15,30,80];

for i=1:numel(D0)
H=1-exp(-1*(U.^2+V.^2)/(2*D0(i).^2));
%myImshow(fftshift(H));
g=dftfilt(f,H);
myImshow(g);
end
myImshow(0);

运行结果

untitled.png

用高通滤波进行图像锐化

Paste_Image.png
用图像的原始频率同时在高频部分进行增强。(拉普拉斯检测边缘也是检测的高频部分其实)
% % % % % % % % % % % % % % % %% % % % % % % % % % % % % % % %
% 用高通滤波做频域增强
% % % % % % % % % % % % % % % %% % % % % % % % % % % % % % % %
% 读取图片

clear all;
close all;
f=imread('E:\资料\onedrive\code\test\image\Fig0419(a)(chestXray_original).tif');
% 生成波特沃兹滤波器
PQ=paddedsize(size(f));
[U,V]=dftuv(PQ(1),PQ(2));
D0=0.05*PQ(2);
n=[1,5,15,50];
% D0=[15,30,80];
myImshow(gscale(f));
for i=1:numel(n);
b=((U.^2+V.^2)/(D0.^2)).^n(i);
H=b/(b+1);
%myImshow(H);
% 按照公式进行运算
Hh=0.5+2*H;
g=dftfilt(f,Hh);
g=histeq(gscale(g),256);
myImshow(g);
end
myImshow(0);

运行结果

untitled.png

图像恢复

图像降指与恢复过程

Paste_Image.png

噪声模型

imnoise函数生成噪声

用之前需要先将f规范化

g = imnoise(f, type, parameters)

type:

'gussian' 'salt & pepper' 'motion'

parameters

生成特定分布的空域随机噪声

使用累积分布的逆函数,可以生成任意你想要的原始分布;这里假设w是均匀分布,可以得到z的分布;
Paste_Image.png
Paste_Image.png

CDF是从PDF积分得到的
Paste_Image.png

  1. % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
  2. % 模拟噪声
  3. % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % % %
  4. %
  5. close all;
  6. clear all;
  7. r={};
  8. r{1}=imnoise2('gaussian',100000,2,0,1);
  9. r{2}=imnoise2('uniform',100000,2,0,1);
  10. r{3}=imnoise2('rayleigh',100000,2,0,1);
  11. % for i=1:3
  12. % subplot(2,2,i);imshow(r{i},50);
  13. % end
  14. % figure;
  15. for i=1:numel(r)
  16. subplot(2,2,i);mesh(r{i});
  17. end
  18. figure;
  19. for i=1:numel(r)
  20. subplot(2,2,i);hist(r{i});
  21. size(r{i})
  22. end

只存在空域噪声的恢复

与图像增强的技术一致

均值滤波器

Paste_Image.png

统计排序与调和均值滤波器

Paste_Image.png

空域滤波基础函数Spfilt

  1. function f = spfilt(g, type, m, n, parameter)
  2. %SPFILT Performs linear and nonlinear spatial filtering.
  3. % F = SPFILT(G, TYPE, M, N, PARAMETER) performs spatial filtering
  4. % of image G using a TYPE filter of size M-by-N. Valid calls to
  5. % SPFILT are as follows:
  6. %
  7. % F = SPFILT(G, 'amean', M, N) Arithmetic mean filtering.
  8. % F = SPFILT(G, 'gmean', M, N) Geometric mean filtering.
  9. % F = SPFILT(G, 'hmean', M, N) Harmonic mean filtering.
  10. % F = SPFILT(G, 'chmean', M, N, Q) Contraharmonic mean
  11. % filtering of order Q. The
  12. % default is Q = 1.5.
  13. % F = SPFILT(G, 'median', M, N) Median filtering.
  14. % F = SPFILT(G, 'max', M, N) Max filtering.
  15. % F = SPFILT(G, 'min', M, N) Min filtering.
  16. % F = SPFILT(G, 'midpoint', M, N) Midpoint filtering.
  17. % F = SPFILT(G, 'atrimmed', M, N, D) Alpha-trimmed mean filtering.
  18. % Parameter D must be a
  19. % nonnegative even integer;
  20. % its default value is D = 2.
  21. %
  22. % The default values when only G and TYPE are input are M = N = 3,
  23. % Q = 1.5, and D = 2.
  24. % Copyright 2002-2004 R. C. Gonzalez, R. E. Woods, & S. L. Eddins
  25. % Digital Image Processing Using MATLAB, Prentice-Hall, 2004
  26. % $Revision: 1.6 $ $Date: 2003/10/27 20:07:00 $
  27. % Process inputs.
  28. if nargin == 2
  29. m = 3; n = 3; Q = 1.5; d = 2;
  30. elseif nargin == 5
  31. Q = parameter; d = parameter;
  32. elseif nargin == 4
  33. Q = 1.5; d = 2;
  34. else
  35. error('Wrong number of inputs.');
  36. end
  37. % Do the filtering.
  38. switch type
  39. case 'amean'
  40. w = fspecial('average', [m n]);
  41. f = imfilter(g, w, 'replicate');
  42. case 'gmean'
  43. f = gmean(g, m, n);
  44. case 'hmean'
  45. f = harmean(g, m, n);
  46. case 'chmean'
  47. f = charmean(g, m, n, Q);
  48. case 'median'
  49. f = medfilt2(g, [m n], 'symmetric');
  50. case 'max'
  51. f = ordfilt2(g, m*n, ones(m, n), 'symmetric');
  52. case 'min'
  53. f = ordfilt2(g, 1, ones(m, n), 'symmetric');
  54. case 'midpoint'
  55. f1 = ordfilt2(g, 1, ones(m, n), 'symmetric');
  56. f2 = ordfilt2(g, m*n, ones(m, n), 'symmetric');
  57. f = imlincomb(0.5, f1, 0.5, f2);
  58. case 'atrimmed'
  59. if (d <= 0) | (d/2 ~= round(d/2))
  60. error('d must be a positive, even integer.')
  61. end
  62. f = alphatrim(g, m, n, d);
  63. otherwise
  64. error('Unknown filter type.')
  65. end
  66. %-------------------------------------------------------------------%
  67. function f = gmean(g, m, n)
  68. % Implements a geometric mean filter.
  69. inclass = class(g);
  70. g = im2double(g);
  71. % Disable log(0) warning.
  72. warning off;
  73. f = exp(imfilter(log(g), ones(m, n), 'replicate')).^(1 / m / n);
  74. warning on;
  75. f = changeclass(inclass, f);
  76. %-------------------------------------------------------------------%
  77. function f = harmean(g, m, n)
  78. % Implements a harmonic mean filter.
  79. inclass = class(g);
  80. g = im2double(g);
  81. f = m * n ./ imfilter(1./(g + eps),ones(m, n), 'replicate');
  82. f = changeclass(inclass, f);
  83. %-------------------------------------------------------------------%
  84. function f = charmean(g, m, n, q)
  85. % Implements a contraharmonic mean filter.
  86. inclass = class(g);
  87. g = im2double(g);
  88. f = imfilter(g.^(q+1), ones(m, n), 'replicate');
  89. f = f ./ (imfilter(g.^q, ones(m, n), 'replicate') + eps);
  90. f = changeclass(inclass, f);
  91. %-------------------------------------------------------------------%
  92. function f = alphatrim(g, m, n, d)
  93. % Implements an alpha-trimmed mean filter.
  94. inclass = class(g);
  95. g = im2double(g);
  96. f = imfilter(g, ones(m, n), 'symmetric');
  97. for k = 1:d/2
  98. f = imsubtract(f, ordfilt2(g, k, ones(m, n), 'symmetric'));
  99. end
  100. for k = (m*n - (d/2) + 1):m*n
  101. f = imsubtract(f, ordfilt2(g, k, ones(m, n), 'symmetric'));
  102. end
  103. f = f / (m*n - d);
  104. f = changeclass(inclass, f);

逆谐波函数用于消除椒盐噪声

  1. % 逆调和均值滤波的例子
  2. gp = imread('E:\资料\onedrive\code\test\image\Fig0505(a)(ckt_pepper_only).tif');
  3. gme = spfilt(gp,'gmean',3,3);
  4. gch = spfilt(gp,'chmean',3,3,1.5); % 椒噪声用正的Q;
  5. gar = spfilt(gp,'amean',3,3);
  6. gme = spfilt(gp,'gmean',3,3);
  7. myImshow(gp,'椒噪');myImshow(gch,'逆调和除噪');myImshow(gar,'均值滤波');%gme
  8. myImshow(gme,'几何均值滤波');
  9. myImshow(0);
  10. figure;
  11. gs = imread('E:\资料\onedrive\code\test\image\Fig0505(b)(ckt_salt_only).tif');
  12. gch = spfilt(gs,'chmean',3,3,-1.5); % 椒噪声用正的Q;
  13. gar = spfilt(gs,'amean',3,3);
  14. gme = spfilt(gs,'gmean',3,3);
  15. myImshow(gs,'盐噪');myImshow(gch,'逆调和除噪');myImshow(gar,'均值滤波');
  16. myImshow(gme,'几何均值滤波');
  17. myImshow(0);

untitled.png

untitled.png

自适应噪声滤波器

  1. % 中值滤波与自适应中值滤波
  2. gsp = imread('E:\资料\onedrive\code\test\image\Fig0506(a)(ckt_salt_pep_pt25).tif');%Fig0506(a)(ckt_salt_pep_pt25).tif
  3. gm2 = medfilt2(gsp);% 中值滤波
  4. gch1 = spfilt(gsp,'chmean',3,3,1.5); % 椒噪声用正的Q;
  5. gch2 = spfilt(gsp,'chmean',3,3,-1.5); % 盐噪声用负的Q;
  6. gch3 = spfilt(gch1,'chmean',3,3,-1.5); % 除椒后除盐
  7. gadp = adpmedian(gsp,7);
  8. myImshow(gsp,'椒盐噪声');myImshow(gm2,'中值滤波');myImshow(gadp,'自适应中值');
  9. myImshow(gch1,'除椒');myImshow(gch2,'除盐');myImshow(gch3,'除椒后除盐');
  10. myImshow(0);

untitled.png

运动模型的退化函数

运动模型

在快门曝光的瞬间内,取景区与镜头发生了位移,使得原始图片f(x,y)变成了g(x,y)

Paste_Image.png
Paste_Image.png
Paste_Image.png

基础函数

  1. function B = pixeldup(A, m, n)
  2. %PIXELDUP Duplicates pixels of an image in both directions.
  3. % B = PIXELDUP(A, M, N) duplicates each pixel of A M times in the
  4. % vertical direction and N times in the horizontal direction.
  5. % Parameters M and N must be integers. If N is not included, it
  6. % defaults to M.
  7. % Copyright 2002-2004 R. C. Gonzalez, R. E. Woods, & S. L. Eddins
  8. % Digital Image Processing Using MATLAB, Prentice-Hall, 2004
  9. % $Revision: 1.4 $ $Date: 2003/06/14 16:29:54 $
  10. % Check inputs.
  11. if nargin < 2
  12. error('At least two inputs are required.');
  13. end
  14. if nargin == 2
  15. n = m;
  16. end
  17. % Generate a vector with elements 1:size(A, 1).
  18. u = 1:size(A, 1);
  19. % Duplicate each element of the vector m times.
  20. m = round(m); % Protect against nonintergers.
  21. u = u(ones(1, m), :);
  22. u = u(:);
  23. % Now repeat for the other direction.
  24. v = 1:size(A, 2);
  25. n = round(n);
  26. v = v(ones(1, n), :);
  27. v = v(:);
  28. B = A(u, v);

u = u(ones(1, m), :);这代码可以推广到一般的形式A(a,b);其中A为矩阵,a、b为向量,则a表示从A中取元素时的所取的行下标,而b则表示列下标,比如
A=[1,2,3;3,4,5] 则A([1,2],[2,3])=[2,3;4,5]
故最终结果有d(a)*d(b)个;d(a)表示a的维数。

模拟运动模型

  1. clear all;
  2. f=checkerboard(8);%生成一个8格的板子
  3. g=pixeldup(f,8);
  4. PSF = fspecial('motion',7,45);
  5. gb=imfilter(f,PSF,'circular');
  6. noise=imnoise(zeros(size(f)),'gaussian',0,0.001);
  7. gbn=gb+noise;
  8. myImshow(f,'拓展图');myImshow(gb,'运动后图像');myImshow(noise,'噪声图像');
  9. myImshow(gbn,'运动后噪声图像');myImshow(0);

untitled.png

维纳滤波(最小均方误差误差)

用来做图像复原。其中H(u,v)是降指模型的点扩散函数

Paste_Image.png
Paste_Image.png

 
  
第一部分完

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注