wrong results when accessing Mat
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
链接地址: http://www.djcxy.com/p/67012.html上一篇: 音频工具箱回放仅播放输出缓冲区的一部分
下一篇: 访问Mat时出错结果