单通道 OpenCV Mat.at<> 给出了错误的值
Single Channel OpenCV Mat.at<> gives out wrong values
声明 Mat 图像,并赋值。
Mat magnitude = Mat(gradient_columns.cols, gradient_columns.rows, CV_64FC1);
for(int i = 0; i < gradient_columns.cols; i++)
{
for(int j = 0; j < gradient_columns.rows; j++)
{
magnitude.at<double>(Point(i,j)) = (double)hypot(gradient_columns.at<double>(Point(i,j)), gradient_rows.at<double>(Point(i,j)));
}
}
正在打印上面的垫子:
cout << "M = " << magnitude << endl;
结果:
M = [0, 0, 0.1257399049164523, 12.36316814720732, 12.50461780753356, 0.2674320707434279, 10.39230484541326, 12.03299037437945, 5.430256687374658,
12.03299037437945, 4.684492386402418, 4.72934192083521, 12.16431633381293, 5.397674732957373, 12.30042244512288, 10.25834261322606, 0.3944487245360109,
12.16431633381293, 11.84297951775486, 12.44187210544911, 12.10132213098092,
0.4088817310696626, 10.15078660267586, 12.09573607646389, 2.076275433221507, 0, 0.1257399049164523, 0, 0.1257399049164523, 0;
.....
.....]
以上结果完全正确,符合预期。
但是,如果我尝试打印单个值,我会得到错误的结果:
cout.precision(20);
cout << "CHANNELS: " << magnitude.channels() << endl;
cout << magnitude.at<double>(Point(0, 2)) << endl;
cout << magnitude.at<double>(Point(0, 3)) << endl;
cout << magnitude.at<double>(Point(0, 4)) << endl;
cout << magnitude.at<double>(Point(0, 5)) << endl;
Result | Actual Value:
CHANNELS: 1
0.062870 | 0.1257399049164523,
0.000000 | 12.36316814720732,
0.031404 | 12.50461780753356,
0.000000 | 0.2674320707434279
我理解它的一些数据类型转换问题,但是否有人可以提出任何解决方案?
谢谢。
我相信你的矩阵没有问题。你只是没有打印你认为的那样。
当写 magnitude.at<double>(Point(0,2))
时,您没有按预期打印第 0 行和第 2 列中的数字,而是第 2 行和第 0 列中的数字。尝试改为写 magnitude.at<double>(0,2)
。
对于遇到类似问题但@Sunreef 的回答不起作用的人,请检查您是否不小心使用了 mat.at<float>(_,_)
而不是 mat.at<double>(_,_)
声明 Mat 图像,并赋值。
Mat magnitude = Mat(gradient_columns.cols, gradient_columns.rows, CV_64FC1);
for(int i = 0; i < gradient_columns.cols; i++)
{
for(int j = 0; j < gradient_columns.rows; j++)
{
magnitude.at<double>(Point(i,j)) = (double)hypot(gradient_columns.at<double>(Point(i,j)), gradient_rows.at<double>(Point(i,j)));
}
}
正在打印上面的垫子:
cout << "M = " << magnitude << endl;
结果:
M = [0, 0, 0.1257399049164523, 12.36316814720732, 12.50461780753356, 0.2674320707434279, 10.39230484541326, 12.03299037437945, 5.430256687374658,
12.03299037437945, 4.684492386402418, 4.72934192083521, 12.16431633381293, 5.397674732957373, 12.30042244512288, 10.25834261322606, 0.3944487245360109,
12.16431633381293, 11.84297951775486, 12.44187210544911, 12.10132213098092,
0.4088817310696626, 10.15078660267586, 12.09573607646389, 2.076275433221507, 0, 0.1257399049164523, 0, 0.1257399049164523, 0;
.....
.....]
以上结果完全正确,符合预期。
但是,如果我尝试打印单个值,我会得到错误的结果:
cout.precision(20);
cout << "CHANNELS: " << magnitude.channels() << endl;
cout << magnitude.at<double>(Point(0, 2)) << endl;
cout << magnitude.at<double>(Point(0, 3)) << endl;
cout << magnitude.at<double>(Point(0, 4)) << endl;
cout << magnitude.at<double>(Point(0, 5)) << endl;
Result | Actual Value:
CHANNELS: 1
0.062870 | 0.1257399049164523,
0.000000 | 12.36316814720732,
0.031404 | 12.50461780753356,
0.000000 | 0.2674320707434279
我理解它的一些数据类型转换问题,但是否有人可以提出任何解决方案?
谢谢。
我相信你的矩阵没有问题。你只是没有打印你认为的那样。
当写 magnitude.at<double>(Point(0,2))
时,您没有按预期打印第 0 行和第 2 列中的数字,而是第 2 行和第 0 列中的数字。尝试改为写 magnitude.at<double>(0,2)
。
对于遇到类似问题但@Sunreef 的回答不起作用的人,请检查您是否不小心使用了 mat.at<float>(_,_)
而不是 mat.at<double>(_,_)