本文介绍了各向异性扩散的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我转换了各向异性扩散代码到C ++,但我没有得到所需的输出。我得到的只是一个黑色的图像。有人可以检查我的代码并提出任何建议吗?下面是我的代码:

I have converted this Matlab Anisotropic Diffusion code to C++ but I am not getting the desired output. All I am getting is a black image. Can someone please check my code and give any suggestions? Below is my code:

const double lambda = 1 / 7;
const double k = 30;
const int iter = 1;


int ahN[3][3] = { {0, 1, 0}, {0, -1, 0}, {0, 0, 0} };
int ahS[3][3] = { {0, 0, 0}, {0, -1, 0}, {0, 1, 0} };
int ahE[3][3] = { {0, 0, 0}, {0, -1, 1}, {0, 0, 0} };
int ahW[3][3] = { {0, 0, 0}, {1, -1, 0}, {0, 0, 0} };
int ahNE[3][3] = { {0, 0, 1}, {0, -1, 0}, {0, 0, 0} };
int ahSE[3][3] = { {0, 0, 0}, {0, -1, 0}, {0, 0, 1} };
int ahSW[3][3] = { {0, 0, 0}, {0, -1, 0}, {1, 0, 0} };
int ahNW[3][3] = { {1, 0, 0}, {0, -1, 0}, {0, 0, 0} };

Mat hN = Mat(3, 3, CV_32FC1, &ahN);
Mat hS = Mat(3, 3, CV_32FC1, &ahS);
Mat hE = Mat(3, 3, CV_32FC1, &ahE);
Mat hW = Mat(3, 3, CV_32FC1, &ahW);
Mat hNE = Mat(3, 3, CV_32FC1, &ahNE);
Mat hSE = Mat(3, 3, CV_32FC1, &ahSE);
Mat hSW = Mat(3, 3, CV_32FC1, &ahSW);
Mat hNW = Mat(3, 3, CV_32FC1, &ahNW);

void anisotropicDiffusion(Mat &output, int width, int height) {

//mat initialisation
Mat nablaN, nablaS, nablaW, nablaE, nablaNE, nablaSE, nablaSW, nablaNW;
Mat cN, cS, cW, cE, cNE, cSE, cSW, cNW;

//depth of filters
int ddepth = -1;

//center pixel distance
double dx = 1, dy = 1, dd = sqrt(2);
double idxSqr = 1.0 / (dx * dx), idySqr = 1.0 / (dy * dy), iddSqr = 1 / (dd * dd);

for (int i = 0; i < iter; i++) {

    //filters
    filter2D(output, nablaN, ddepth, hN);
    filter2D(output, nablaS, ddepth, hS);
    filter2D(output, nablaW, ddepth, hW);
    filter2D(output, nablaE, ddepth, hE);
    filter2D(output, nablaNE, ddepth, hNE);
    filter2D(output, nablaSE, ddepth, hSE);
    filter2D(output, nablaSW, ddepth, hSW);
    filter2D(output, nablaNW, ddepth, hNW);

    //exponential flux
    cN = nablaN / k;
    cN.mul(cN);
    cN = 1.0 / (1.0 + cN);
    //exp(-cN, cN);

    cS = nablaS / k;
    cS.mul(cS);
    cS = 1.0 / (1.0 + cS);
    //exp(-cS, cS);

    cW = nablaW / k;
    cW.mul(cW);
    cW = 1.0 / (1.0 + cW);
    //exp(-cW, cW);

    cE = nablaE / k;
    cE.mul(cE);
    cE = 1.0 / (1.0 + cE);
    //exp(-cE, cE);

    cNE = nablaNE / k;
    cNE.mul(cNE);
    cNE = 1.0 / (1.0 + cNE);
    //exp(-cNE, cNE);

    cSE = nablaSE / k;
    cSE.mul(cSE);
    cSE = 1.0 / (1.0 + cSE);
    //exp(-cSE, cSE);

    cSW = nablaSW / k;
    cSW.mul(cSW);
    cSW = 1.0 / (1.0 + cSW);
    //exp(-cSW, cSW);

    cNW = nablaNW / k;
    cNW.mul(cNW);
    cNW = 1.0 / (1.0 + cNW);
    //exp(-cNW, cNW);

    output = output + lambda * (idySqr * cN.mul(nablaN) + idySqr * cS.mul(nablaS) +
                                idxSqr * cW.mul(nablaW) + idxSqr * cE.mul(nablaE) +
                                iddSqr * cNE.mul(nablaNE) + iddSqr * cSE.mul(nablaSE) +
                                iddSqr * cNW.mul(nablaNW) + iddSqr * cSW.mul(nablaSW));
    }
}


推荐答案

看起来就像你需要分配乘法结果一样:

Looks like you need to assign multiplication result:

Mat C = A.mul(B);

int ahN[3][3] ....

应为

float ahN[3][3] ....

这篇关于各向异性扩散的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 15:25