源于 :http://zhidao.baidu.com/question/89228643.html 百度中的
>>
M=imread('fly.bmp') %读取MATLAB中的名为cameraman的图像
subplot(3,3,1)
imshow(M) %显示原始图像
title('original')
P1=imnoise(M,'gaussian',0.02) %加入高斯躁声
subplot(3,3,2)
imshow(P1) %加入高斯躁声后显示图像
title('gaussian noise');
>> P2=imnoise(M,'salt&pepper',0.02)%加入椒盐噪声
??? Error using ==> imnoise>ParseInputs
Unknown noise type: 'salt&pepper'. 修改为 'salt & pepper'
Error in ==> imnoise at 85
[a, code, classIn, classChanged, p3, p4] = ParseInputs(varargin{:});
P2=imnoise(M,'salt & pepper',0.02) %加入椒盐躁声
>>P2=imnoise(M,'salt & pepper',0.02) %加入椒盐躁声
>> subplot(3,3,3)
>> imshow(P2)
>> title('salt & pepper noise');
>> g=medfilt2(P1)%
??? Function ORDFILT2 expected its first input, A, to be two-dimensional.
Error in ==> ordfilt2>ParseInputs at 135
iptcheckinput(A, {'numeric','logical'}, {'2d','real'}, mfilename, 'A', 1);
Error in ==> ordfilt2 at 51
[A,order,domain,s,padopt,msg] = ParseInputs(varargin{:});
Error in ==> medfilt2 at 53
b = ordfilt2(a, order, domain, padopt);
就是这个中值滤波函数medfilt2的接口是不符合这幅彩色图的
对于这个问题,google: 彩色图像 中值滤波
方案是 对每个图层滤波,然后叠加在一起.涉及2个问题:1如何图像分离rgb,2如何合成bmp图像
修改为
g1=medfilt2(P1(:,:,1));
g2=medfilt2(P1(:,:,2));
g3=medfilt2(P1(:,:,3));
>> g(:,:,1)=g1;
>> g(:,:,2)=g2;
>> g(:,:,3)=g3;
>> subplot(3,3,5)
>> imshow(g)
>> title('medfilter gaussian') 成功了
>> h1=medfilt2(P2(:,:,1)) ;
>> h2=medfilt2(P2(:,:,2)) ;
>> h3=medfilt2(P2(:,:,3)) ;
>> h(:,:,1)=h1;
>> h(:,:,2)=h2;
>> h(:,:,3)=h3;
>> subplot(3,3,6)
>> imshow(h)
>> title('medfilter salt & pepper noise')
>> l=[ 1 1 1
1 1 1
1 1 1 ];
>> l=l/9;
Warning: CONV2 on values of class UINT8 is obsolete.
Use CONV2(DOUBLE(A),DOUBLE(B)) or CONV2(SINGLE(A),SINGLE(B)) instead.
> In uint8.conv2 at 11
>> k2=conv2(P1(:,:,2),l) ;
Warning: CONV2 on values of class UINT8 is obsolete.
Use CONV2(DOUBLE(A),DOUBLE(B)) or CONV2(SINGLE(A),SINGLE(B)) instead.
> In uint8.conv2 at 11
>> k3=conv2(P1(:,:,3),l) ;
Warning: CONV2 on values of class UINT8 is obsolete.
Use CONV2(DOUBLE(A),DOUBLE(B)) or CONV2(SINGLE(A),SINGLE(B)) instead.
> In uint8.conv2 at 11
>> k(:,:,1)=k1;
>> k(:,:,2)=k2;
>> k(:,:,3)=k3;
>> subplot(3,3,8)
>> imshow(k,[])
title('arithmeticfilter gaussian')
>> imshow(k)
>> title('arithmeticfilter gaussian')
效果一片空白
修改如下
>> k1=conv2(double(P1(:,:,1)),l) ;
>> k2=conv2(double(P1(:,:,2)),l) ;
>> k3=conv2(double(P1(:,:,3)),l) ;
>> k(:,:,1)=k1;
>> k(:,:,2)=k2;
>> k(:,:,3)=k3;
>> subplot(3,3,8)
>> imshow(k,[])
title('arithmeticfilter gaussian') 效果任然一片空白,估计是预处理不恰当,所以必须改正一下
为了获取实验的真实情况:
>> imshow(k1),
效果就是实验效果1图中的空白,这说明在这个地方就已经是空白了
实验效果1:
所以,我在想,是不是,先用灰度图像进行分析,看看conv效果如何
1
>> u=P1(:,:,1);
>> figure,imshow(u);
>> info=imfinfo(u)
??? Error using ==> imfinfo
Filename must be a string.
>> k=conv2(u,l);
Warning: CONV2 on values of class UINT8 is obsolete.
Use CONV2(DOUBLE(A),DOUBLE(B)) or CONV2(SINGLE(A),SINGLE(B)) instead.
> In uint8.conv2 at 11
>> figure,imshow(k);
>>
显然不是理想的目标
>> imwrite(u,'fly_r.bmp','bmp');
>> info=imfinfo('fly_r.bmp')
info =
Filename: 'fly_r.bmp'
FileModDate: '25-May-2009 16:18:16'
FileSize: 286518
Format: 'bmp'
FormatVersion: [1x33 char]
Width: 640
Height: 446
BitDepth: 8
ColorType: 'indexed'
FormatSignature: 'BM'
NumColormapEntries: 256
Colormap: [256x3 double]
RedMask: []
GreenMask: []
BlueMask: []
ImageDataOffset: 1078
BitmapHeaderSize: 40
NumPlanes: 1
CompressionType: 'none'
BitmapSize: 285440
HorzResolution: 0
VertResolution: 0
NumColorsUsed: 256
NumImportantColors: 0
>>
我调用了一个函数,mat2gray,得到了一个相对而言满意的效果,不过,不知道这样是否有意义并且对一开始的操作的空白结果的原因仍然不解
>> T1=mat2gray(P1(:,:,1));
>> k1=conv2(T1,l);
>> T2=mat2gray(P1(:,:,2));
>> k2=conv2(T2,l);
>> T3=mat2gray(P1(:,:,3));
>> k3=conv2(T3,l);
>> k(:,:,1)=k1;
>> k(:,:,2)=k2;
>> k(:,:,3)=k3;
>> figure,imshow(k);
>> figure,imshow(P1);
注意到处理后的图像边框有一条明显的黑色痕迹
>> figure,imshow(P1);
>> U1=mat2gray(P2(:,:,1));
>> d1=conv2(U1,l);
>> U2=mat2gray(P2(:,:,2));
>> d2=conv2(U2,l);
>> U3=mat2gray(P2(:,:,3));
>> d3=conv2(U3,l);
>> d(:,:,1)=d1;
>> d(:,:,2)=d2;
>> d(:,:,3)=d3;
>> figure,imshow(P2);
>> figure,imshow(d);