图像的各向异性滤波

图像的各向异性滤波

%E5%90%84%E5%90%91%E5%BC%82%E6%80%A7%E8%BF%87%E6%BB%A4.jpg?raw=true
非均向性(anisotropy),或作各向异性,与各向同性相反,指物体的全部或部分物理、化学等性质随方向的不同而有所变化的特性,例如石墨单晶的电导率在不同方向的差异可达数千倍,又如天文学上,宇宙微波背景辐射亦拥有些微的非均向性。许多的物理量都具有非均向性,如弹性模量、电导率、在酸中的溶解速度等。

各向异性扩散滤波主要是用来平滑图像的,克服了高斯模糊的缺陷,各向异性扩散在平滑图像时是保留图像边缘的,和双边滤波很像。

20171101160945965.png?raw=true

20171101161013559.png?raw=true

代码实现:

#include <opencv2/opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;
float k = 15;
float lambda = 0.25;
int N = 20;

void anisotropy_demo(Mat &image, Mat &result);
int main(int argc, char** argv) {
    Mat src = imread("D:/vcprojects/images/example.png");
    if (src.empty()) {
        printf("could not load image...\n");
        return -1;
    }
    namedWindow("input image", CV_WINDOW_AUTOSIZE);
    imshow("input image", src);
    vector<Mat> mv;
    vector<Mat> results;
    split(src, mv);
    for (int n = 0; n < mv.size(); n++) {
        Mat m = Mat::zeros(src.size(), CV_32FC1);
        mv[n].convertTo(m, CV_32FC1);
        results.push_back(m);
    }

    int w = src.cols;
    int h = src.rows;
    Mat copy = Mat::zeros(src.size(), CV_32FC1);
    for (int i = 0; i < N; i++) {
        anisotropy_demo(results[0], copy);
        copy.copyTo(results[0]);

        anisotropy_demo(results[1], copy);
        copy.copyTo(results[1]);

        anisotropy_demo(results[2], copy);
        copy.copyTo(results[2]);

    }
    Mat output;
    normalize(results[0], results[0], 0, 255, NORM_MINMAX);
    normalize(results[1], results[1], 0, 255, NORM_MINMAX);
    normalize(results[2], results[2], 0, 255, NORM_MINMAX);

    results[0].convertTo(mv[0], CV_8UC1);
    results[1].convertTo(mv[1], CV_8UC1);
    results[2].convertTo(mv[2], CV_8UC1);

    Mat dst;
    merge(mv, dst);

    imshow("result", dst);
    imwrite("D:/result.png", dst);
    waitKey(0);
    return 0;
}

void anisotropy_demo(Mat &image, Mat &result) {
    int width = image.cols;
    int height = image.rows;

    // 四邻域梯度
    float n = 0, s = 0, e = 0, w = 0; 
    // 四邻域系数
    float nc = 0, sc = 0, ec = 0, wc = 0; 
    float k2 = k*k;
    for (int row = 1; row < height -1; row++) {
        for (int col = 1; col < width -1; col++) {
            // gradient
            n = image.at<float>(row - 1, col) - image.at<float>(row, col);
            s = image.at<float>(row + 1, col) - image.at<float>(row, col);
            e = image.at<float>(row, col - 1) - image.at<float>(row, col);
            w = image.at<float>(row, col + 1) - image.at<float>(row, col);
            nc = exp(-n*n / k2);
            sc = exp(-s*s / k2);
            ec = exp(-e*e / k2);
            wc = exp(-w*w / k2);
            result.at<float>(row, col) = image.at<float>(row, col) + lambda*(n*nc + s*sc + e*ec + w*wc);
        }
    }
}

效果炸裂:
OJBK.gif?raw=true

参考:https://blog.csdn.net/jia20003/article/details/78415384

转载于:https://www.cnblogs.com/yoyo-sincerely/p/8966491.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值