采用结构光进行扫描检测时,需要提取激光条纹的中心线,本文采用经典的Steger算法提取光条中心。
Steger算法基于Hessian矩阵,能够实现光条中心亚像素精度定位:首先通过Hessian矩阵能够得到光条的法线方向,然后在法线方向利用泰勒展开得到亚像素位置。
对于图像中激光条纹上的任意一点 (x,y) ,Hessian矩阵可以表示为:
根据Steger算法的原理,借助opencv的Mat数据结构,实现Steger算法如下:
void StegerLine()
{
Mat img0 = imread("image_0.png", 1);
Mat img;
cvtColor(img0, img0, CV_BGR2GRAY);
img = img0.clone();
//高斯滤波
img.convertTo(img, CV_32FC1);
GaussianBlur(img, img, Size(0, 0), 6, 6);
//一阶偏导数
Mat m1, m2;
m1 = (Mat_<float>(1, 2) << 1, -1); //x偏导
m2 = (Mat_<float>(2, 1) << 1, -1); //y偏导
Mat dx, dy;
filter2D(img, dx, CV_32FC1, m1);
filter2D(img, dy, CV_32FC1, m2);
//二阶偏导数
Mat m3, m4, m5;
m3 = (Mat_<float>(1, 3) << 1, -2, 1); //二阶x偏导
m4 = (Mat_<float>(3, 1) << 1, -2, 1); //二阶y偏导
m5 = (Mat_<float>(2, 2) << 1, -1, -1, 1); //二阶xy偏导
Mat dxx, dyy, dxy;
filter2D(img, dxx, CV_32FC1, m3);
filter2D(img, dyy, CV_32FC1, m4);
filter2D(img, dxy, CV_32FC1, m5);
//hessian矩阵
double maxD = -1;
int imgcol = img.cols;
int imgrow = img.rows;
vector<double> Pt;
for (int i=0;ifor (int j=0;jif (img0.at(j,i)>200)
{
Mat hessian(2, 2, CV_32FC1);
hessian.at<float>(0, 0) = dxx.at<float>(j, i);
hessian.at<float>(0, 1) = dxy.at<float>(j, i);
hessian.at<float>(1, 0) = dxy.at<float>(j, i);
hessian.at<float>(1, 1) = dyy.at<float>(j, i);
Mat eValue;
Mat eVectors;
eigen(hessian, eValue, eVectors);
double nx, ny;
double fmaxD = 0;
if (fabs(eValue.at<float>(0,0))>= fabs(eValue.at<float>(1,0))) //求特征值最大时对应的特征向量
{
nx = eVectors.at<float>(0, 0);
ny = eVectors.at<float>(0, 1);
fmaxD = eValue.at<float>(0, 0);
}
else
{
nx = eVectors.at<float>(1, 0);
ny = eVectors.at<float>(1, 1);
fmaxD = eValue.at<float>(1, 0);
}
double t = -(nx*dx.at<float>(j, i) + ny*dy.at<float>(j, i)) / (nx*nx*dxx.at<float>(j,i)+2*nx*ny*dxy.at<float>(j,i)+ny*ny*dyy.at<float>(j,i));
if (fabs(t*nx)<=0.5 && fabs(t*ny)<=0.5)
{
Pt.push_back(i);
Pt.push_back(j);
}
}
}
}
for (int k = 0;k2;k++)
{
Point rpt;
rpt.x = Pt[2 * k + 0];
rpt.y = Pt[2 * k + 1];
circle(img0, rpt, 1, Scalar(0, 0, 255));
}
imshow("result", img0);
waitKey(0);
}