实施结构化张量

时间:2018-02-23 14:51:33

标签: c++ image-processing tensorflow computer-vision

我正在尝试实施一篇名为基于结构化张量的图像插值的论文。在本文中,它使用结构张量将图像中的每个像素分类为基于结构化张量的特征值的三个不同类别(均匀,角落和边缘)。

为实现这一目标,我编写了以下代码:

void tensorComputation(Mat dx, Mat dy, Mat magnitude)
{

    Mat dx2, dy2, dxy; 
    GaussianBlur(magnitude, magnitude, Size(3, 3), 0, 0, BORDER_DEFAULT); 
    // Calculate image derivatives 
    multiply(dx, dx, dx2);
    multiply(dy, dy, dy2);
    multiply(dx, dy, dxy);

    Mat t(2, 2, CV_32F); // tensor matrix

    // Insert values to the tensor matrix.
    t.at<float>(0, 0) = sum(dx2)[0];
    t.at<float>(0, 1) = sum(dxy)[0];
    t.at<float>(1, 0) = sum(dxy)[0];
    t.at<float>(1, 1) = sum(dy2)[0];

    // eigen decomposition to get the main gradient direction. 
    Mat eigVal, eigVec;
    eigen(t, eigVal, eigVec);

    // This should compute the angle of the gradient direction based on the first eigenvector. 
    float* eVec1 = eigVec.ptr<float>(0);
    float* eVec2 = eigVec.ptr<float>(1);
    cout << fastAtan2(eVec1[0], eVec1[1]) << endl;
    cout << fastAtan2(eVec2[0], eVec2[1]) << endl;
}

此处dxdymagnitude分别是x轴的导数,y轴的导数和图像的大小。

我所知道的是我找到了整个图像的结构化张量。但我的问题是我需要为图像中的每个像素计算结构化张量。怎么做到这一点?

1 个答案:

答案 0 :(得分:1)

在您的代码中,您会模糊magnitude,但不要使用它。你根本不需要这么大的数量。

您可以正确构建结构张量,但是您可以对整个图像进行平均。您要做的是应用本地平均值。对于每个像素,结构张量是邻域中像素的矩阵平均值。您可以通过对张量的每个组件应用高斯模糊来计算:dx2dy2dxy

高斯的西格玛越大,你平均的邻域越大。您可以获得更多的正则化(对噪声不太敏感),但也会降低分辨率(对小变化和短边不敏感)。玩弄参数,直到得到你需要的东西。西格玛在2到5之间非常普遍。

接下来,您需要计算每个像素的特征分解。我不知道OpenCV是否能让这一切变得简单。我建议您改用DIPlib 3。它具有计算和使用结构张量的正确基础结构。 See here how easy it can be.