在线结构光视觉传感器中,由线激光器发射出的线结构光,在本质上为一个连续且具有一定厚度的空间光平面,而在目标表面上所形成的具有一定宽度的光条特征,即为该光平面与目标表面相交而成的交线。在该空间光平面的厚度方向上,光强近似服从高斯分布,因而在摄像机采集到的光条图像中,在沿着光条宽度的方向或光条的法线方向上,其灰度也会呈现出类似的高斯分布特点,即光条中心的灰度值大而光条边缘的灰度值小,如图1所示,因而光条中心线的提取任务就是要找到图像中的光条灰度的高斯分布中心。
采用结构光进行扫描检测时,需要提取激光条纹的中心线,本文采用经典的Steger算法提取光条中心。
Steger算法原理
Steger算法基于Hessian矩阵,能够实现光条中心亚像素精度定位:首先通过Hessian矩阵能够得到光条的法线方向,然后在法线方向利用泰勒展开得到亚像素位置。
对于图像中激光条纹上的任意一点(x,y)(x,y),Hessian矩阵可以表示为:
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;i<imgcol;i++){for (int j=0;j<imgrow;j++){if (img0.at<uchar>(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;k<Pt.size()/2;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);
}
https://blog.csdn.net/CharmsLUO/article/details/122312450