简体   繁体   中英

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++).

I am analyzing a 3D volume image by calculating for each pixel an Hessian matrix. 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).

The matrices are of type Matrix3d. The problem is, that for most of the sums (and obviously for the averages as well) i obtain something like :

Elements analyzed : 28215

Elements summed : 28215

Subvolume sum :

5143.76 | nan | -2778.05

5402.07 | 16011.9 | -inf

-2778.05 | -8716.86 | 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)

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). 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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