0
$\begingroup$

I've this implementation of the anisotropic filter:

int anisotropicDiffusionFilterTest(int argc, char **argv) {
    if (argc != 2) {
        std::cout << "Need filename..." << std::endl;
        return 1;
    }

    Mat im = imread(argv[1], CV_LOAD_IMAGE_GRAYSCALE);
    Mat imfloat;
    im.convertTo(imfloat, CV_32FC1);

    int X = im.rows;
    int Y = im.cols;

    Mat u(imfloat), g(imfloat.size(),imfloat.type());
    Mat dx, dy;
    const int k_max = 10;
    const float lambda = 20;
    float gij, gip1j, gijp1, dxg, dyg, uij, uijp1, uip1j, dxu, dyu;
    for (auto k = 0; k < k_max; ++k) {
        //compute diffusion term image
        Sobel(u, dx, CV_32F, 1, 0);
        Sobel(u, dy, CV_32F, 0, 1);
        cv::sqrt(1.0f/(1.0f + (dx.mul(dx) + dy.mul(dy))/(lambda*lambda)),g);
        for (auto y = 0; y < Y; ++y) {
            for (auto x = 0; x < X; ++x) {
                gij = g.at<float>(y, x);
                gip1j = g.at<float>((y + 1) % Y, x);
                gijp1 = g.at<float>(y, (x + 1) % X);
                dyg = sqrt(gij*gip1j);
                dxg = sqrt(gij*gijp1);
                uij = u.at<float>(y, x);
                uip1j = u.at<float>((y + 1) % Y, x);
                uijp1 = u.at<float>(y , (x + 1) % X);
                dxu = uijp1 - uij;
                dyu = uip1j - uij;
                u.at<float>(y, x) = u.at<float>(y, x) +
                    dxg*uij + gij*dxu +
                    dyg*uij + gij*dyu;
            }
        }
    }

    Mat output(Size(2 * X, Y), CV_8UC1);
    im.copyTo(output(Rect(0,0,X,Y)));
    u.convertTo(u, CV_8UC1);
    u.copyTo(output(Rect(X, 0, X, Y)));

    namedWindow("Original vs Anisotropic diffusion", WINDOW_AUTOSIZE);
    imshow("Original vs Anisotropic diffusion", output);

    waitKey(0);

    return 1;
}

I'm basically trying to replicate the results shown here, my discretization is slightly different, but I don't think that's the problem (but please correct me if I'm wrong).

Basically my discretization is based on forward differences.

The results I'm getting for this filter is overly bright, which I think might be due to the boundary conditions maybe? But I have no clue how to impose them.

$\endgroup$

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Browse other questions tagged or ask your own question.