基于hessian矩阵的光斑中心亚像素提取(matlab)

clear all;close all;
file='';%%自己加地址
im=imread(file);
im=im2double(im);

m=0;n=0;m1=0;n1=0;
for i=1:size(im,1)
    for j=1:size(im,2)
        m=m+i*im(i,j);m1=m1+im(i,j);
        n=n+j*im(i,j);n1=n1+im(i,j);
    end
end
m0=m/m1;
n0=n/n1;
m00=0;n00=0;m10=0;n10=0;
for i=1:size(im,1)
    for j=1:size(im,2)
        m00=m00+i*im(i,j).^2;m10=m10+im(i,j).^2;
        n00=n00+j*im(i,j).^2;n10=n10+im(i,j).^2;
    end
end
m000=m00/m10;
n000=n00/n10;
figure
imshow(im);hold on;
plot(m0,n0,'x');hold on;%质心法
plot(m000,n000,'+');hold on;%加权质心法

% 下面用hessian矩阵实现
Sigma =3/sqrt(3);
[a,b,c,d,e]=Hessian2D(im,Sigma);
for i=1:size(im,1)
    for j=1:size(im,2)
            [V,D]=eig([c(i,j),d(i,j);d(i,j),e(i,j)]);
            im2(i,j)=D(1,1)*D(2,2);
    end
end
im3=im2;
for ii=1:60
    [X Y] = find (im3 == max(max(im3)));
for nnn=1:size(X,1)
    i=X(nnn,1);j=Y(nnn,1);
s=(b(i,j)*d(i,j)-a(i,j)*e(i,j))/(c(i,j)*e(i,j)-d(i,j)^2);
t=(a(i,j)*d(i,j)-b(i,j)*c(i,j))/(c(i,j)*e(i,j)-d(i,j)^2);
if abs(s)<=1/2&&abs(t)<=1/2
    sss(1,1)=i+s;sss(1,2)=j+t;
plot(sss(:,1),sss(:,2),'*');hold on;
break
else
im3(X,Y)=0;
end
end
end
f1=0;f2=0;f3=0;g=0;%%判断标准
for i=1:60
    for j=1:60
if im(i,j)>200/255
    f3=f3+sqrt((i-sss(1))^2+(j-sss(2))^2);
        f1=f1+sqrt((i-m0)^2+(j-n0)^2);    
        f2=f2+sqrt((i-m000)^2+(j-n000)^2);
    g=g+1;
end
    end
end
lv1=f1/g;
lv2=f2/g;
lv3=f3/g;
% hessian子函数
function [Dx,Dy,Dxx,Dxy,Dyy] = Hessian2D(I,Sigma)
[X,Y]   = ndgrid(-round(3*Sigma):round(3*Sigma));
Dx=( - X / (Sigma*Sigma)).*exp(-1 * (X.^2 + Y.^2) / (2 * Sigma*Sigma))*(1 / (2 * pi*Sigma^2));
Dy=( - Y / (Sigma*Sigma)).*exp(-1 * (X.^2 + Y.^2) / (2 * Sigma*Sigma))*(1 / (2 * pi*Sigma^2));
DGaussxx = 1/(2*pi*Sigma^4) * (X.^2/Sigma^2 - 1) .* exp(-(X.^2 + Y.^2)/(2*Sigma^2));
DGaussxy = 1/(2*pi*Sigma^6) * (X .* Y)           .* exp(-(X.^2 + Y.^2)/(2*Sigma^2));
DGaussyy = DGaussxx';
Dx = imfilter(I,Dx,'conv');
Dy = imfilter(I,Dy,'conv');
Dxx = imfilter(I,DGaussxx,'conv');
Dxy = imfilter(I,DGaussxy,'conv');
Dyy = imfilter(I,DGaussyy,'conv');
end

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Steger算法是一种基于Hessian矩阵的边缘检测算法,可以用于亚像素中心线提取。下面是Python实现的步骤: 1. 导入相关库 ```python import numpy as np from scipy.ndimage import gaussian_filter, maximum_filter from skimage import io, img_as_float ``` 2. 读入图像 ```python img = img_as_float(io.imread('image.jpg', as_gray=True)) ``` 3. 计算Hessian矩阵 ```python sigma = 2 # 高斯滤波参数 img_smooth = gaussian_filter(img, sigma) Ix, Iy = np.gradient(img_smooth) Ixx = gaussian_filter(Ix ** 2, sigma) Ixy = gaussian_filter(Ix * Iy, sigma) Iyy = gaussian_filter(Iy ** 2, sigma) ``` 4. 计算中心线强度 ```python alpha = 0.06 # Hessian矩阵参数 det = Ixx * Iyy - Ixy ** 2 trace = Ixx + Iyy response = det - alpha * trace ** 2 response_max = maximum_filter(response, size=20) response_max[response != response_max] = 0 response_binary = response_max > 0 ``` 5. 中心线提取 ```python def extract_centerline(binary): # 定义八个方向 directions = [(1, 0), (1, 1), (0, 1), (-1, 1), (-1, 0), (-1, -1), (0, -1), (1, -1)] centerline = [] rows, cols = binary.shape for i in range(rows): for j in range(cols): if binary[i, j]: # 计算中心线方向 tangent = np.zeros(2) for d in directions: if i + d[0] >= 0 and i + d[0] < rows and \ j + d[1] >= 0 and j + d[1] < cols and \ binary[i + d[0], j + d[1]]: tangent += d if np.linalg.norm(tangent) > 0: tangent /= np.linalg.norm(tangent) centerline.append((j, i, tangent[0], tangent[1])) return centerline centerline = extract_centerline(response_binary) ``` 6. 输出中心线直线方程 ```python for line in centerline: x, y, tx, ty = line a = -ty / tx b = 1 c = -(a * x + b * y) print('Line equation: {}x + {}y + {} = 0'.format(a, b, c)) ``` 这样就可以得到中心线的直线方程。需要注意的是,在实际应用中,可能需要对中心线进行进一步的筛选和优化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值