Briefly, I would like to sum up values in a Mat matrix. For now I have a 256 by 1 Mat(which is actually a vector, but the Mat format will be important later on). In order to see what happens I'm trying to print it in the loop

`
calcFractile(Mat* in){

cout << "Input = " << *in << endl;
cout << "?!?! ";
for(int k = 0; k<in->rows; k++){
    cout << static_cast<int>(in->at<uint8_t>(0,k)) << ", ";
    sum += (int)in->at<uint8_t>(0,k);
}
cout <<endl;
}

`

I totally don't understand why, but the cout << "Input[...] line produces correct result, but the loop accessing individual elements fails giving different results It's not just the value issue, but also positions in the array.

The Mat I'm passing is a histogram of an image, the histogram is single channel, 256 bins.

有帮助吗?

解决方案

The problem came from type casting, or rather lack of it. As soon as I wrote

static_cast(in->at(0,k)

and it worked. Also just (int)(in->at(0,k) seems to working fine as well.

Lesson I learned - *always check type casting * not that it's something new, but perhaps someone will benefit

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top