【发布时间】:2018-07-07 08:13:25
【问题描述】:
我有一个合成图像。为了某些边缘检测目的,我想对其局部结构张量(LST)进行特征值分解。我使用LST的特征值l1、l2和特征向量e1、e2为图像的每个像素生成一个自适应椭圆。不幸的是,我得到不相等的特征值 l1 , l2 ,因此对于我的图形的均匀区域,椭圆的半轴长度不相等:
我不知道我的代码有什么问题:
function [H,e1,e2,l1,l2] = LST_eig(I,sigma1,rw)
% LST_eig - compute the structure tensor and its eigen
% value decomposition
%
% H = LST_eig(I,sigma1,rw);
%
% sigma1 is pre smoothing width (in pixels).
% rw is filter bandwidth radius for tensor smoothing (in pixels).
%
n = size(I,1);
m = size(I,2);
if nargin<2
sigma1 = 0.5;
end
if nargin<3
rw = 0.001;
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% pre smoothing
J = imgaussfilt(I,sigma1);
% compute gradient using Sobel operator
Sch = [-3 0 3;-10 0 10;-3 0 3];
%h = fspecial('sobel');
gx = imfilter(J,Sch,'replicate');
gy = imfilter(J,Sch','replicate');
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% compute tensors
gx2 = gx.^2;
gy2 = gy.^2;
gxy = gx.*gy;
% smooth
gx2_sm = imgaussfilt(gx2,rw); %rw/sqrt(2*log(2))
gy2_sm = imgaussfilt(gy2,rw);
gxy_sm = imgaussfilt(gxy,rw);
H = zeros(n,m,2,2);
H(:,:,1,1) = gx2_sm;
H(:,:,2,2) = gy2_sm;
H(:,:,1,2) = gxy_sm;
H(:,:,2,1) = gxy_sm;
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% eigen decomposition
l1 = zeros(n,m);
l2 = zeros(n,m);
e1 = zeros(n,m,2);
e2 = zeros(n,m,2);
for i = 1:n
for j = 1:m
Hmat = zeros(2);
Hmat(:,:) = H(i,j,:,:);
[V,D] = eigs(Hmat);
D = abs(D);
l1(i,j) = D(1,1); % eigen values
l2(i,j) = D(2,2);
e1(i,j,:) = V(:,1); % eigen vectors
e2(i,j,:) = V(:,2);
end
end
感谢任何帮助。
这是我的椭圆绘制代码:
% determining ellipse parameteres from eigen value decomposition of LST
M = input('Enter the maximum allowed semi-major axes length: ');
I = input('Enter the input data: ');
row = size(I,1);
col = size(I,2);
a = zeros(row,col);
b = zeros(row,col);
cos_phi = zeros(row,col);
sin_phi = zeros(row,col);
for m = 1:row
for n = 1:col
a(m,n) = (l2(m,n)+eps)/(l1(m,n)+l2(m,n)+2*eps)*M;
b(m,n) = (l1(m,n)+eps)/(l1(m,n)+l2(m,n)+2*eps)*M;
cos_phi1 = e1(m,n,1);
sin_phi1 = e1(m,n,2);
len = hypot(cos_phi1,sin_phi1);
cos_phi(m,n) = cos_phi1/len;
sin_phi(m,n) = sin_phi1/len;
end
end
%% plot elliptic structuring elements using parametric equation and superimpose on the image
figure; imagesc(I); colorbar; hold on
t = linspace(0,2*pi,50);
for i = 10:10:row-10
for j = 10:10:col-10
x0 = j;
y0 = i;
x = a(i,j)/2*cos(t)*cos_phi(i,j)-b(i,j)/2*sin(t)*sin_phi(i,j)+x0;
y = a(i,j)/2*cos(t)*sin_phi(i,j)+b(i,j)/2*sin(t)*cos_phi(i,j)+y0;
plot(x,y,'r','linewidth',1);
hold on
end
end
这是axis equal的新情节:
【问题讨论】:
-
@Cris Luengo:谢谢。我为
rw测试了 0.5、1、5、8,但我没有得到好的结果。是的,它不是 Sobel 过滤器,而是 Scharr 过滤器。你确定高斯导数更好吗?你知道,那些椭圆是我用于我的目的的结构元素,我画它们是为了理解它们是否足够适应?我想我应该使用eigs作为张量。 -
我用
eig代替eigs,但结果没有改变,只是运行时间减少了。 matlab中的高斯导数函数是什么? -
查看您的图像,我注意到您的 x 和 y 轴的缩放方式不同。这是故意的吗?
axis equal将它们的缩放设置相同。这或许可以解释为什么其中一些椭圆与图像中的结构不一致。 -
我觉得我的椭圆绘制代码是对的。我将它添加到问题中。我使用高斯导数内核运行代码,得到了更可接受的答案。我也将新情节添加到问题中。
-
看起来不错。同样,使用
axis equal,您将获得更好的椭圆表示,它们比您在图像中看到的更圆。你仍然有一条沿着图像中间向下延伸的高各向同性带,但也许这在数据中?在曲线水平的地方,我看到垂直拉长的椭圆,也不知道为什么会这样。在测试图像上运行您的代码,我得到了很好的结果(即使使用 Scharr 过滤器和eigs)。
标签: matlab image-processing structure tensor eigenvalue