简体   繁体   English

矩阵的本征和得出NaN和-inf值

[英]Eigen sum of matrices resulting in NaN and -inf values

I am having a strange issue with using Eigen (Tuxfamily) in my software (in c++). 我在软件(C ++)中使用Eigen(Tuxfamily)遇到了一个奇怪的问题。

I am analyzing a 3D volume image by calculating for each pixel an Hessian matrix. 我正在通过为每个像素计算Hessian矩阵来分析3D体积图像。 The volume (approx 800x800x600) is divided in subvolumes and for each subvolume i sum up all the obtained matrices and then divide them by the amount to obtain the average (and then i do the same summing up all the averages and dividing by the number of subvolumes to obtain the average for the full volume). 将体积(大约800x800x600)划分为多个子体积,对于每个子体积,我将所有获得的矩阵求和,然后将它们除以数量,得到平均值(然后,我对所有平均值求和,并除以子体积以获取全部体积的平均值)。

The matrices are of type Matrix3d. 矩阵的类型为Matrix3d。 The problem is, that for most of the sums (and obviously for the averages as well) i obtain something like : 问题是,对于大多数总和(显然也包括平均值),我得到类似以下内容:

Elements analyzed : 28215 分析元素:28215

Elements summed : 28215 元素合计:28215

Subvolume sum : 子卷总和:

5143.76 | 5143.76 | nan | nan | -2778.05 -2778.05

5402.07 | 5402.07 | 16011.9 | 16011.9 | -inf -INF

-2778.05 | -2778.05 | -8716.86 | -8716.86 | 7059.32 7059.32

I sum them this way : 我这样总结:

    for(int i = 0;i<(int)OuterVector.size();i++){
    AverageProduct+=OuterVector[i];
}

Due to the nature of the matrices i know that they should be symmetrical on the diagonal, so the correct value is calculated for some of them. 由于矩阵的性质,我知道它们应该在对角线上对称,因此可以为其中的一些计算出正确的值。 Any idea on why the others might be failing? 关于其他为什么会失败的任何想法? (and consider that it's always the same two position of the matrix giving me nan and -inf) (并考虑到矩阵总是两个相同的位置,给我nan和-inf)

Ok, using a mix of the suggestions you guys gave me in the comments, i tried a couple of random fixes and i solved the problem. 好的,混合使用你们在评论中给我的建议,我尝试了几次随机修复,然后我解决了问题。

When i was creating the Eigen::Matrix3d object, i was not initializing the values, so somehow as soon as i was adding the first OuterVector[i] those two values were going wild (the (0,1) was going to nan and the (1,2) was going to inf). 当我创建Eigen :: Matrix3d对象时,我没有初始化值,所以以某种方式,当我添加第一个OuterVector [i]时,这两个值就变得疯狂了((0,1)会变成nan和(1,2)将要移至inf)。 Strange that it was only happening only for those two specific values and in the same identical way every time. 奇怪的是,它仅针对这两个特定值并且每次都以相同的相同方式发生。

So doing (at initialization time) 这样做(在初始化时)

Matrix3d AverageProduct << 0,0,0,0,0,0,0,0,0;

was enough to fix it. 足以修复它。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM