使用OpenCV实现Halcon算法(9)EdgesColorSubpix的Canny方法实现

声明:本篇仅仅是分享网上的开源项目,算法非本人原创。

文章内容转载于:

关于Halcon中EdgesColorSubpix的Canny版本C++实现_爱吃鱼的猫博客-CSDN博客最近实现了EdgesSubpix 算法发现它还有个彩色版本 于是就用OpenCV实现了它的Canny模式以下为它的C++实现void DerivateGauss(Mat &Input,Mat &Output,double Sigma);void NonMaxSuppresion();void LinkLine();void caclSubPixPosition();void HalconCannyEdgesColorAmp(Mat &Input,Mat &Outhttps://blog.csdn.net/qq_39969166/article/details/119277349

void DerivateGauss(Mat &Input,Mat &Output,double Sigma);
void NonMaxSuppresion();
void LinkLine();
void caclSubPixPosition();
void HalconCannyEdgesColorAmp(Mat &Input,Mat &Output,double Sigma) {

	if (Input.channels() != 3) {
		return;
	}

	//Mat B, G, R;
	std::vector<Mat> bgrs;
	split(Input, bgrs);

	Mat Bx, By, Rx, Ry, Gx, Gy;
	//求高斯偏导数X和Y
	DerivateGauss(bgrs[0], Bx, Sigma, "x");
	DerivateGauss(bgrs[0], By, Sigma, "y");
	DerivateGauss(bgrs[1], Gx, Sigma, "x");
	DerivateGauss(bgrs[1], Gy, Sigma, "y");
	DerivateGauss(bgrs[2], Rx, Sigma, "x");
	DerivateGauss(bgrs[2], Ry, Sigma, "y");

	Mat gxx = Bx.mul(Bx) + Rx.mul(Rx) + Gx.mul(Gx);
	Mat gyy = By.mul(By) + Ry.mul(Ry) + Gy.mul(Gy);
	Mat gxy = Bx.mul(By) + Rx.mul(Ry) + Gx.mul(Gy);
	
	Mat temp, eValuesMat, eVectorsMat;
	double myArray[2][2];
	Output.create(Input.size(), CV_32F);
	for (int i = 0; i < Input.rows; i++)
	{
		for (int j = 0; j < Input.cols; j++)
		{
			myArray[0][0] = gxx.ptr<float>(i)[j];
			myArray[0][1] = gxy.ptr<float>(i)[j];
			myArray[1][0] = gxy.ptr<float>(i)[j];
			myArray[1][1] = gyy.ptr<float>(i)[j];
			//获得图像中像素位置的一个2X2梯度矩阵,矩阵中每个数据都表示一种梯度
			temp = cv::Mat(2, 2, CV_32F, myArray);
			//求该梯度矩阵的特征值,使用特征值的最大值代替表示该矩阵
			cv::eigen(temp, eValuesMat, eVectorsMat);
			//xld->attribute "response"
			Output.ptr<float>(i)[j] =(float) eValuesMat.ptr<double>(0)[0]; 
			//特征值最大的特征向量 atan2f(-dy,dx)=->xld attribute "angle"			
		}
	}
}

猜你喜欢

转载自blog.csdn.net/libaineu2004/article/details/122485241