10 May 2010

Matlab 的fspecial函数用法


fspecial函数用于建立预定义的滤波算子,其语法格式为:
h = fspecial(type)
h = fspecial(type,para)
其中type指定算子的类型,para指定相应的参数;
type的类型有:
1、'average'
averaging filter
为均值滤波,参数为hsize代表模板尺寸,默认值为[3,3]。
H = FSPECIAL('average',HSIZE) returns an averaging filter H of size

HSIZE. HSIZE can be a vector specifying the number of rows and columns in

H or a scalar, in which case H is a square matrix.
The default HSIZE is [3 3].
2、 'disk'
circular averaging filter
为圆形区域均值滤波,参数为radius代表区域半径,默认值为5.
H = FSPECIAL('disk',RADIUS) returns a circular averaging filter

(pillbox) within the square matrix of side 2*RADIUS+1.

The default RADIUS is 5.
3、'gaussian'
Gaussian lowpass filter

为高斯低通滤波,有两个参数,hsize表示模板尺寸,默认值为【3 3】,sigma为滤波器的标准值,单位为像素,默认值为0.5.
H = FSPECIAL('gaussian',HSIZE,SIGMA) returns a rotationally

symmetric Gaussian lowpass filter
of size HSIZE with standard

deviation SIGMA (positive). HSIZE can be a vector specifying the

number of rows and columns in H or a scalar, in which case H is a

square matrix.

The default HSIZE is [3 3], the default SIGMA is 0.5.
4、'laplacian' filter approximating the 2-D Laplacian operator
为拉普拉斯算子,参数alpha用于控制算子形状,取值范围为【0,1】,默认值为0.2.

H = FSPECIAL('laplacian',ALPHA) returns a 3-by-3 filter

approximating the shape of the two-dimensional Laplacian

operator. The parameter ALPHA controls the shape of the

Laplacian and must be in the range 0.0 to 1.0.

The default ALPHA is 0.2.
5、'log'
Laplacian of Gaussian filter
为拉普拉斯高斯算子,有两个参数,hsize表示模板尺寸,默认值为【3 3】,sigma为滤波器的标准差,单位为像素,默认值为0.5.
H = FSPECIAL('log',HSIZE,SIGMA) returns a rotationally symmetric

Laplacian of Gaussian filter of size HSIZE with standard deviation

SIGMA (positive). HSIZE can be a vector specifying the number of rows

and columns in H or a scalar, in which case H is a square matrix.

The default HSIZE is [5 5], the default SIGMA is 0.5.
6、'motion'
motion filter
为运动模糊算子,有两个参数,表示摄像物体逆时针方向以theta角度运动了len个像素,len的默认值为9,theta的默认值为0;
H = FSPECIAL('motion',LEN,THETA) returns a filter to approximate, once

convolved with an image, the linear motion of a camera by LEN pixels,

with an angle of THETA degrees in a counter-clockwise direction. The

filter becomes a vector for horizontal and vertical motions.
The

default LEN is 9, the default THETA is 0, which corresponds to a

horizontal motion of 9 pixels.
7、'prewitt'
Prewitt horizontal edge-emphasizing filter
用于边缘增强,大小为【3 3】,无参数
H = FSPECIAL('prewitt') returns 3-by-3 filter that emphasizes

horizontal edges by approximating a vertical gradient. If you need to

emphasize vertical edges, transpose the filter H: H'.


[1 1 1;0 0 0;-1 -1 -1].
8、'sobel'
Sobel horizontal edge-emphasizing filter
用于边缘提取,无参数
H = FSPECIAL('sobel') returns 3-by-3 filter that emphasizes

horizontal edges utilizing the smoothing effect by approximating a

vertical gradient. If you need to emphasize vertical edges, transpose

the filter H: H'.


[1 2 1;0 0 0;-1 -2 -1].
9、'unsharp'
unsharp contrast enhancement filter
为对比度增强滤波器。参数alpha用于控制滤波器的形状,范围为【0,1】,默认值为0.2.
H = FSPECIAL('unsharp',ALPHA) returns a 3-by-3 unsharp contrast


enhancement filter. FSPECIAL creates the unsharp filter from the

negative of the Laplacian filter with parameter ALPHA. ALPHA controls

the shape of the Laplacian and must be in the range 0.0 to 1.0.

The default ALPHA is 0.2.

7 May 2010

matlab数字图像小波分解(2)


更新:
指定function name和N,可以做n级小波分解

%% wavelet study demo2
% xenoszhATgmail.com
% 2010-05-06

function nWaveletTest(wname,n)

close all;

% get input image
[filename,pathname] = uigetfile('*.jpg;*.jpeg;*.bmp;*.png','Please pick up a picture:');
if ~filename
 disp('No input file.');
 return;
end
img = imread(strcat(pathname,filename));

% get grayscale image
img = rgb2gray(img); 

figure;
imshow(waveRec(img,n,wname));

end

%% sub function to reconstruct n level image
function recimage = waveRec(img,n,wname)
if nargin==3
 % get wavelet decomposition filter coefficients, using haar function
 [lod,hid] = wfilters(wname,'d'); 

 % n level wavelet decomposition
 [c,s] = wavedec2(img,n,lod,hid);
 
 % get the top level approximate signal
 an = appcoef2(c,s,wname,n);
 an = double2uint8(an);
 
 % reconstruct image recursively
 a = an;
 for i=n:-1:1
  curimg = curRec(a,c,s,i);
  a = curimg;
 end
 
 % return recontruct image
 recimage = curimg;
else
 error('please check the input');
end
end

%% sub function to get current level reconstruct image
function curRecimg = curRec(a,c,s,n)
if nargin==4
 h = detcoef2('h',c,s,n);
 v = detcoef2('v',c,s,n);
 d = detcoef2('d',c,s,n);
 a = imresize(a,size(h));
 curRecimg = [a,h; v,d];
else
 error('please check the input');
end
end
%% sub function to convert double image to uint8 image
function uint8image = double2uint8(dblimage)
if nargin==1 && isa(dblimage,'double')
 uint8image = 255*(dblimage-min(min(dblimage)))/(max(max(dblimage))-min(min(dblimage)));
 uint8image = uint8(uint8image);
else
 error('input image must be double');
end
end

6 May 2010

matlab数字图像小波分解



小波的定义和原理随便找本书都有讲解,简言之,小波就是将信号X

分解为近似信号A(Approximate signal)和细节信号D(Details signal)的

过程.matlab中的小波工具箱内置了丰富的函数,让复杂的小波处理

变得非常简单.(鉴于数字图像都是2维,只记录下2维相关的函数)

分解过程如:


wfilters: 指定小波函数,有很多.具体help
wavedec2: N级二维小波分解
appcoef2: 获得指定级别的近似信号A
detcoef2: 获得指定级别的细节(高频)信号h,v,或者d.具体与第一个参数有关. (得到某个高频分量,注意和下一个函数区别)
wrcoef2: 利用某个高频分量重建图像(与原图像图等大小)

测试如下:
%% wavelet study demo
% xenoszhATgmail.com
% 2010-05-06

function waveletTest()

% get input image
[filename,pathname] = uigetfile('testPics\*.jpg;*.jpeg;*.bmp;*.png','Please pick up a picture:');
if ~filename
 disp('No input file.');
 return;
end
img = imread(strcat(pathname,filename));

% get grayscale image
img = rgb2gray(img); 

size(img)

% get wavelet decomposition filter coefficients, using haar function
[lod,hid] = wfilters('haar','d'); 

% 2 level wavelet decomposition
[c,s] = wavedec2(img,2,lod,hid);

% get 2nd level approximate signal
a2 = appcoef2(c,s,'haar',2);
% convert it convert it to uint8 image in [0 255]
a2 = double2uint8(a2);

% get 2nd level horizontal high frequency signal
h2 = detcoef2('h',c,s,2);
h2 = double2uint8(h2);

% get 2nd level vertical high frequency signal
v2 = detcoef2('v',c,s,2);
v2 = double2uint8(v2);

% get 2nd level digonal high frequency signal
d2 = detcoef2('d',c,s,2);
d2 = double2uint8(d2);

% compose 2nd level 
wave2 = [a2,h2;v2,d2];

% get 1st lavel horizontal high frequency signal
h1 = detcoef2('h',c,s,1);
h1 = double2uint8(h1);

% get 1st level vertical high frequency signal
v1 = detcoef2('v',c,s,1);
v1 = double2uint8(v1);

% get 1st level digonal high frequency signal
d1 = detcoef2('d',c,s,1);
d1 = double2uint8(d1);

% make sure they have the same size
wave2 = imresize(wave2,size(h1));

% compose 1st level (original size)
wave1 = [wave2, h1; v1, d1];

% show image
figure; imshow(img);

% show wavelet decomposition
figure; imshow(wave1);

end

%% sub function to convert double image to uint8 image
function uint8image = double2uint8(dblimage)
if nargin==1 && isa(dblimage,'double')
 uint8image = 255*(dblimage-min(min(dblimage)))/(max(max(dblimage))-min(min(dblimage)));
 uint8image = uint8(uint8image);
else
 error('input image must be double');
end
end



结果为:

28 Apr 2010

MATLAB函数之- bwmorph

bwmorph
功能: 提取二进制图像的轮廓.
语法: BW2 = bwmorph(BW1,operation) ;
BW2 = bwmorph(BW1,operation,n) ; n为次数;
Operation的参数可以有多种选择,现归纳如下:
‘bother’:闭包运算,即先腐蚀,在膨胀,然后减去原图像;
‘bridge’:做连接运算;
‘clean’:去除孤立的亮点;
‘close’:进行二值闭运算;
‘diag’:采用对角线填充来去除8邻域的背景;
‘dilate’:采用结构元素ones(3)做膨胀运算;
‘erode’:采用结构元素ones(3)作腐蚀运算;
‘fill’:填充孤立的黑点; ‘hbreak’:断开H形连接;
‘Majority’:若像素的8邻域中有大于或等于5的元素为1,则像素为1,否则为0;
‘open’:执行二值开运算;
‘remove’:去掉内点,即若像素的4邻域都为1,则像素为0;
‘shrink’n=inf:做收缩运算,这样没有孔的物体收缩为一个点,而含孔的物体收缩为一个相连的环,环的位置在孔和物体外边缘的中间,收缩运算保持欧拉数不变,
‘skel’n=inf: 提取物体的骨架,即去除物体外边缘的点,但是保持物体不发生断裂,它也保持欧拉数不变。 ‘spur’:去除物体小的分支;
‘thicken’n=inf;对物体进行粗化,即对物体的外边缘增加像素,知道原来为连接的物体按照8邻域被连接起来。粗化保持欧拉数不变。
‘thin’n=inf:对物体进行细化,使得没有孔的物体收缩为最小连接棒,而含有孔的物体收缩为一个连接的环,同样细化保持欧拉数不变。
‘tophat’:用原图减去开运算后的图像;

27 Apr 2010

matlab 中序列求极值的方法


坛子里的几个经典方法:

利用数学里驻点和极值点定义来求。
--------------------------------------ilxy的方法 -------------------------------------------------------------------
>> aa=randperm(20)
aa =
16 13 17 7 19 18 14 8 11 2 20 3 6 4 15 1 12 10 5 9
>> bb=intersect(find(diff(aa)>0)+1,find(diff(aa)<0))
bb =
3 5 9 11 13 15 17
----------------------------------------------------------------------------------------------------------------------
----------------------------------hyperelastic 的方法----------------------------------------------------------
N = 100;
aa=randperm(N);
index = find(((aa(2:N-1)>aa(1:N-2))+(aa(2:N-1)>aa(3:N)))==2)+1;
bb = aa(index);
-------------------bainhome 的方法 -----------------------------------------------------------------------------
a=rand(1,100); find(diff(sign(diff(a)))>0)+1
------------------------------------------------------------------------------------------------------------------------
以上代码都是求极大值,略微变换后可以得到极小值。

------------------------------------eight版主给的方法 ------------------------------------------------------------
d=diff(x);
n=length(d);
d1=d(1:n-1);
d2=d(2:n);
indmin=find(d1.*d2<0 & d1<0)+1;
indmax=find(d1.*d2<0 & d1>0)+1;
------------------------------------------------------------------------------------------------------------------------
虽然基本原理一样,不过几位的方法都各有特色。都是前辈们智慧的结晶,在此深表感谢。
小代码体现大智慧的典范!

我在实验中所搜到的方法,可惜这次没搜索到:

假设待求的数列为data,则find(diff(sign(diff(data)))== - 2)+1 可求极大值。find(diff(sign(diff(data)))== 2)+1 可求极小值。

Uninstalling Visual Studio 2010

Uninstall Visual Studio 2010
Uninstall Visual Studio Office 2010
Uninstall other 2010 stuff...

Search on Framework
Uninstall .NET Framework 4.0 Extended
if there's errors,Rename the crashed program (always in the v4.0 frameworks directory in my case) to have a *.OLD extension
Uninstall the .NET Framework 4 Client Profile

Done!