gpt4 book ai didi

opencv - 模拟各向同性线性扩散平滑

转载 作者:太空宇宙 更新时间:2023-11-03 22:54:52 25 4
gpt4 key购买 nike

我想应用我在标题中命名的基于以下等式的去噪滤波器:

enter image description here

其中 d = 1 是标量常数扩散参数,I(x, y) 是初始噪声图像,u(x, y, t) 是扩散时间 t 之后获得的图像,比方说 5、10 和 30。但是,为了在 OpenCV 中实现这一点,我对使用哪个函数以及如何使用感到很困惑。我觉得这很简单,但出于某种原因我很困惑。有人有想法吗?

这是一个示例图片:

enter image description here

然后我想将它与根据以下内容的高斯滤波方法进行比较:

enter image description here

其中 G√2t (x, y) 是高斯核。这证明在 d = 1 的时间 t 上执行各向同性线性扩散完全等同于在 σ = √(2t) 上执行高斯平滑>

我有一个应用高斯滤波的函数:

void gaussian_2D_convolution(const cv::Mat& src, cv::Mat& dst, const float sigma, const int ksize_x = 0, const int ksize_y = 0)
{
int ksize_x_ = ksize_x, ksize_y_ = ksize_y;

// Compute an appropriate kernel size according to the specified sigma
if (sigma > ksize_x || sigma > ksize_y || ksize_x == 0 || ksize_y == 0)
{
ksize_x_ = (int)ceil(2.0f*(1.0f + (sigma - 0.8f) / (0.3f)));
ksize_y_ = ksize_x_;
}

// The kernel size must be and odd number
if ((ksize_x_ % 2) == 0)
{
ksize_x_ += 1;
}

if ((ksize_y_ % 2) == 0)
{
ksize_y_ += 1;
}

// Perform the Gaussian Smoothing
GaussianBlur(src, dst, Size(ksize_x_, ksize_y_), sigma, sigma, BORDER_DEFAULT);

// show result
std::ostringstream out;
out << std::setprecision(1) << std::fixed << sigma;
String title = "sigma: " + out.str();
imshow(title, dst);
imwrite("gaussian/" + title + ".png", dst);

waitKey(260);
}

但我在实现第一个案例时遇到困难。

最佳答案

这应该按预期工作。这是基于:

代码:

#include <opencv2\opencv.hpp>
using namespace cv;

void ilds(const Mat1b& src, Mat1b& dst, int iter = 10, double diffusivity = 1.0, double lambda = 0.1)
{
Mat1f img;
src.convertTo(img, CV_32F);
lambda = fmax(0.001, std::fmin(lambda, 0.25)); // something in [0, 0.25] by default should be 0.25
while (iter--)
{
Mat1f lap;
Laplacian(img, lap, CV_32F);
img += lambda * diffusivity * lap;
}

img.convertTo(dst, CV_8U);
}

int main() {

Mat1b img = imread("path_to_image", IMREAD_GRAYSCALE);
Mat1b res_ilds;
ilds(img, res_ilds);

imshow("ILDS", res_ilds);
waitKey();

return 0;
}

结果:

enter image description here

让我知道它是否适合你

关于opencv - 模拟各向同性线性扩散平滑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32531968/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com