Matlab中利用fspecial可以生成多种模板,如何生成圆形和环形的模板呢?
function [ c ] = genCircle(w,r)
%GENCIRCLE Summary of this function goes here
% Detailed explanation goes here
% w 是模板的大小
% r 圆形模板的半径
[rr cc] = meshgrid(1:w);
c = sqrt((rr-floor(w/2)).^2 + (cc-floor(w/2)).^2) <= r;
end
生成的模板如下图:
利用两个半径不同的圆形模板来生成一个环形模板。
function [ c ] = genRing( w,r1,r2 )
%GENRING Summary of this function goes here
% Detailed explanation goes here
% r1,r2 分别为外半径和内半径,r1应大于r2
c1 = genCircle(w,r1);
c2 = genCircle(w,r2);
c = c1 & ~c2;
end
生成的模板如下图: