基于能量公式的laplacian平滑迭代处理

代码网址:https://download.csdn.net/download/lykymy/10799376

视频网址:https://www.bilibili.com/video/av36468064/

laplacian平滑处理

Mat laplacian_smooth(Mat & image, int time)
{
    char energy_name[50] = "laplacian smooth energy";
	//创建grad_x和grad_y 、abs_grad_x和abs_grad_y矩阵
	Mat grad_x; 
	Mat grad_y;
	//创建sobel算子处理显示图片
	Mat sobel_img = Mat::zeros(image.size(), CV_16S);
	int scale = 1;
	int delta = 0;
	int ddepth = CV_16S;
	//求X方向梯度
	Sobel(image, grad_x, ddepth, 1, 0, 3, scale, delta, BORDER_DEFAULT);
	//求Y方向梯度
	Sobel(image, grad_y, ddepth, 0, 1, 3, scale, delta, BORDER_DEFAULT);
	//构建流场
	for (int i = 0; i < image.rows; i++)
	{
		for (int j = 0; j < image.cols; j++)
		{
			if (grad_x.at<ushort>(i, j) != 0)
			{
				sobel_img.at<short>(i, j) = atan(fabs(grad_y.at<ushort>(i, j) / grad_x.at<ushort>(i, j)));
			}
			else
			{
				sobel_img.at<short>(i, j) = 90;
			}
		}
	}
	
	Mat smooth_img;
	for (int k = 0; k < time; k++)
	{
	  Mat laplacian_img = Mat::zeros(sobel_img.size(), CV_16S);
	  //计算利用laplacian算子进行平滑
	  for (int i = 1; i < sobel_img.rows - 1; i++)
	  {
 		 for (int j = 1; j < sobel_img.cols - 1; j++)
		 {
		 	laplacian_img.at<short>(i, j) = sobel_img.at<short>(i - 1 , j) + sobel_img.at<short>(i + 1, j) + sobel_img.at<short>(i, j + 1) + sobel_img.at<short>(i, j - 1);
	     }
	  }
	 //构造N行1列的结果矩阵Y
	 Mat result_mat = laplacian_img.reshape(0, laplacian_img.rows * laplacian_img.cols);
	 //将Opencv的Mat转化为Matrix
	 MatrixXf result_matrix(result_mat.rows, result_mat.cols);
	 cv2eigen(result_mat, result_matrix);
	 MatrixXf coefficient = MatrixXf::Identity(laplacian_img.rows * laplacian_img.cols, laplacian_img.rows * laplacian_img.cols);
	 coefficient = coefficient * 4;
	 VectorXf result_vector = coefficient.lu().solve(result_matrix);
	 //构造显示图像的大小
	 eigen2cv(result_vector, smooth_img);
	 smooth_img = smooth_img.reshape(1, laplacian_img.rows).clone();
	 energy_solve(smooth_img , energy_name, k);
	 smooth_img.convertTo(sobel_img, CV_16S);
	}
	return smooth_img;
}

效果

原始处理图片

猜你喜欢

转载自blog.csdn.net/LYKymy/article/details/84331175