C++ 矩阵边缘化(边缘分布)

C++ 矩阵边缘化(边缘分布),c++,opencv,matrix,C++,Opencv,Matrix,我想从两个矩阵边缘化得到第三个矩阵。 基本上,我有两个大小为(4x1)的Mat对象,我希望将它们边缘化,以获得第三个行标准化的4x4矩阵。这是通过获取第一个Mat对象的第一行并与第二个Mat对象的每一行相乘来完成的,以填充第三个4x4 Mat的第一行,每行元素相乘除以该行的总和,如下图所示。还有。 下面是我到目前为止所采取的编码步骤,并得到了一个小堆栈。。。。 const int nStates=9; 注册int iii,jjj; float mMatrix[nStates][3]={1,2,

我想从两个矩阵边缘化得到第三个矩阵。 基本上,我有两个大小为(4x1)的Mat对象,我希望将它们边缘化,以获得第三个行标准化的4x4矩阵。这是通过获取第一个Mat对象的第一行并与第二个Mat对象的每一行相乘来完成的,以填充第三个4x4 Mat的第一行,每行元素相乘除以该行的总和,如下图所示。还有。 下面是我到目前为止所采取的编码步骤,并得到了一个小堆栈。。。。

const int nStates=9;
注册int iii,jjj;
float mMatrix[nStates][3]={1,2,3,4,5,6,7,8,9};
//像素级跃迁
Mat nPot1=Mat(nStates,1,CV_32FC1,mMatrix);
Mat nPot2=Mat(nStates,1,CV_32FC1,mMatrix);
Mat节点转换(nStates、nStates、CV_32FC1);NodeTransitions.setTo(标量(1.0f));
浮动fN1;
对于(iii=0;iii不能只计算外积,然后使用L1范数对每一行进行规格化

我将调用你的两个向量
x
y
。然后计算
R=x*y.t()
并沿每行对
R
进行归一化

在OpenCV中,有一个函数,但它仅为向量定义,矩阵需要它(当您将输入之一转换为1xn矩阵或行向量时)

这意味着,你必须手工完成。你也可以做这些矩阵乘法。考虑一下,如果你做了很多矩阵乘法运算,它们不代表图像等。

未测试代码:

const int nStates = 9;
float mMatrix[nStates][1] = {1,2,3,4,5,6,7,8,9};

//Pixelwise transitions
Mat nPot1 = Mat(nStates, 1, CV_32FC1,mMatrix );
Mat nPot2 = Mat(nStates, 1, CV_32FC1,mMatrix );
Mat NodeTransitions(nStates, nStates, CV_32FC1);
NodeTransitions.setTo(Scalar(1.0f)); // Why are you doing this?
float fN1;

// Pass one, compute outer product.
for (int row=0; row < nStates; row++) {
    for (int col=0; col < nStates; col++) {
        fN1 = nPot1.at<float>(row, 0) * nPot2.at<float>(col, 0);
        NodeTransitions.at<float>(row, col)  = fN1;
    }
}

// Pass two, normalise each row.
for (int row=0; row < nStates; row++) {
    // find sum of this row
    fN1 = 0; // using fN1 for sum now.
    for (int col=0; col < nStates; col++) {
        fN1 += NodeTransitions.at<float>(row, col);
    }
    // Now divide all elements in row by sum
    for (int col=0; col < nStates; col++) {
        // divide value at row,col by fN1.
        NodeTransitions.at<float>(row, col) /= fN1;
    }
}
const int nStates=9;
float mMatrix[nStates][1]={1,2,3,4,5,6,7,8,9};
//像素级跃迁
Mat nPot1=Mat(nStates,1,CV_32FC1,mMatrix);
Mat nPot2=Mat(nStates,1,CV_32FC1,mMatrix);
Mat节点转换(nStates、nStates、CV_32FC1);
NodeTransitions.setTo(标量(1.0f));//为什么要这样做?
浮动fN1;
//通过1,计算外积。
对于(int row=0;row
论效率
考虑到您的
nStates
非常小,此代码应该足够高效。看起来,您在尝试一次性完成所有这一切时遇到了困难。没有必要这样做。

使用这行“NodeTransitions.setTo(Scalar(1.0f));//您为什么要这样做?”我只是用一些值填充矩阵,使其不为空:)计算结果后,它不会为空。无需先填充值,然后立即覆盖它们。该算法非常有效。感谢您的帮助,最初我避免了许多循环,但我想这很好。再次感谢:)如果需要,请随意向上投票你觉得这很有帮助。我刚刚把代码插入我的主算法,计算效率大大降低。有更好的方法吗?
const int nStates = 9;
float mMatrix[nStates][1] = {1,2,3,4,5,6,7,8,9};

//Pixelwise transitions
Mat nPot1 = Mat(nStates, 1, CV_32FC1,mMatrix );
Mat nPot2 = Mat(nStates, 1, CV_32FC1,mMatrix );
Mat NodeTransitions(nStates, nStates, CV_32FC1);
NodeTransitions.setTo(Scalar(1.0f)); // Why are you doing this?
float fN1;

// Pass one, compute outer product.
for (int row=0; row < nStates; row++) {
    for (int col=0; col < nStates; col++) {
        fN1 = nPot1.at<float>(row, 0) * nPot2.at<float>(col, 0);
        NodeTransitions.at<float>(row, col)  = fN1;
    }
}

// Pass two, normalise each row.
for (int row=0; row < nStates; row++) {
    // find sum of this row
    fN1 = 0; // using fN1 for sum now.
    for (int col=0; col < nStates; col++) {
        fN1 += NodeTransitions.at<float>(row, col);
    }
    // Now divide all elements in row by sum
    for (int col=0; col < nStates; col++) {
        // divide value at row,col by fN1.
        NodeTransitions.at<float>(row, col) /= fN1;
    }
}