0
votes

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.

1
Maybe uint8_t is causing some problems. I guess that if the input matrix is a histogram than it has some other type (wouldn't make sense to be able to store values less than 256). For better answers you should provide an SSCCE - Sigroad
yea, similar objection here. please check if in.type()==0; (else it's not uchar ) - berak
Thanks for your suggestions. I don't think I can make SSCCE of this code and I suspect nobody would read all of it if I tried looking for volunteer, but I've found it I think. For some weird reason using 'static_cast<int>(in->at<float>(0,k)' solved the problem. I don't really get why, because the histogram was buit taking single column of image and dividing pixels based on temperature using 256 levels (namely I wanted to use only a single byte and for that made 8 bit grayscale conversion)... - user3002166

1 Answers

0
votes

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