OpenCV:将float数组转换为Mat

时间:2018-01-31 17:02:36

标签: java arrays opencv

我正在开发 java 项目,我必须将float数组转换为opencv mat。 这是我到目前为止所做的:

System.out.println(Arrays.toString(floats));
Mat imgf = new Mat(2, 100, CvType.CV_32F);
imgf.put(2, 100, floats);
System.out.println(imgf.dump());

变量floats返回此数组(代码的第一行):

[255.0, 255.0, 255.0, 255.0, 255.0, 255.0, 255.0, 255.0, 255.0, 255.0, 0.0, 127.5, 0.0, 0.0, 0.0, 0.0, 0.0, 255.0, 255.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 127.5, 255.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 127.5, 255.0, 127.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 127.5, 255.0, 127.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 127.5, 255.0, 127.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 255.0, 127.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 255.0, 255.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 255.0, 255.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 255.0, 255.0, 127.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 12.1125, 255.0, 255.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 255.0, 255.0, 255.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 251.8125, 60.5625, 255.0, 255.0, 0.0, 0.0, 0.0, 0.0, 0.0, 255.0, 97.537506, 0.0, 255.0, 255.0, 0.0, 0.0, 0.0, 0.0, 255.0, 135.7875, 0.0, 0.0, 255.0, 255.0, 0.0, 0.0, 0.0, 255.0, 168.9375, 0.0, 0.0, 0.0, 255.0, 191.25, 0.0, 0.0, 255.0, 255.0, 255.0, 255.0, 255.0, 255.0, 255.0, 255.0, 255.0, 255.0, 105.1875, 0.0, 0.0, 0.0, 0.0, 0.0, 255.0, 191.25, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 255.0, 255.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 12.1125, 255.0, 255.0, 0.0, 0.0]

但我无法进入垫子imgf

我尝试打印垫子的最后一行有时没有得到固定的结果:

[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0;
 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]

有时这个:

[0, 6.7501031e-07, 6.6356984e-07, 9.3232773e-09, 1.4586571e-19, 2.5751176e-12, 6.7500696e-07, 6.6356984e-07, 9.3232773e-09, 1.4586571e-19, 2.5751176e-12, 6.7500696e-07, 6.6356984e-07, 5.8270483e-10, 1.0310071e-08, 2.5747301e-12, 1.458489e-19, 5.8270455e-10, 2.5028963e-12, 1.458489e-19, 2.5751176e-12, 6.7500696e-07, 2.5028963e-12, 1.458489e-19, 5.8270455e-10, 2.5028963e-12, 1.458489e-19, 2.3308182e-09, 6.4900371e-07, 6.6356984e-07, 5.8270483e-10, 2.5028963e-12, 1.458489e-19, 5.8270455e-10, 2.5028963e-12, 1.458489e-19, 4.1653962e-11, 9.3232773e-09, 1.4586571e-19, 4.1653962e-11, 5.8270483e-10, 2.5028963e-12, 1.458489e-19, 5.8270455e-10, 2.5028963e-12, 1.0621065e-05, 1.4586562e-19, 2.5751176e-12, 1.0621065e-05, 1.4586562e-19, 5.8270455e-10, 2.5028963e-12, 1.458489e-19, 5.8270455e-10, 1.0310071e-08, 2.5747301e-12, 6.7500696e-07, 1.0310071e-08, 2.5747301e-12, 1.458489e-19, 5.8270455e-10, 2.5028963e-12, 1.458489e-19, 5.8270455e-10, 6.6356984e-07, 2.3308193e-09, 6.4900371e-07, 2.5028963e-12, 1.458489e-19, 5.8270455e-10, 2.5028963e-12, 1.458489e-19, 9.3232728e-09, 1.4586571e-19, 2.5751176e-12, 1.458489e-19, 5.8270455e-10, 2.5028963e-12, 1.458489e-19, 5.8270455e-10, 6.6356984e-07, 9.3232773e-09, 1.4586571e-19, 5.8270455e-10, 2.5028963e-12, 1.458489e-19, 5.8270455e-10, 2.5028963e-12, 6.7500696e-07, 6.6356984e-07, 2.3308193e-09, 6.4900371e-07, 2.5028963e-12, 1.458489e-19, 5.8270455e-10, 2.5028963e-12, 1.458489e-19, 1.1708638e-19, 5.8270455e-10, 2.5028963e-12;
 1.458489e-19, 2.3308182e-09, 2.5783149e-09, 1.4586568e-19, 2.5751176e-12, 6.7500696e-07, 2.5028963e-12, 1.458489e-19, 5.8270455e-10, 2.5028963e-12, 1.458489e-19, 9.3232728e-09, 1.4586571e-19, 2.5751176e-12, 6.7500696e-07, 2.5028963e-12, 1.458489e-19, 5.8270455e-10, 2.5028963e-12, 1.458489e-19, 4.0292387e-11, 6.638179e-07, 6.6256933e-10, 1.0605886e-08, 9.3232773e-09, 1.4586571e-19, 2.5751176e-12, 1.458489e-19, 5.8270455e-10, 2.5028963e-12, 1.458489e-19, 2.5751176e-12, 4.166012e-11, 6.8247328e-07, 1.4586896e-19, 9.3232728e-09, 1.4586571e-19, 2.5751176e-12, 1.458489e-19, 5.8270455e-10, 2.5028963e-12, 6.7500696e-07, 4.1240284e-08, 4.3673634e-05, 2.5182753e-12, 1.458489e-19, 9.3232728e-09, 1.4586571e-19, 2.5751176e-12, 1.458489e-19, 5.8270455e-10, 6.6356984e-07, 2.3308193e-09, 0.00016614873, 2.5752289e-12, 1.458489e-19, 5.8270455e-10, 6.6356984e-07, 2.3308193e-09, 1.0139325e-08, 5.8270483e-10, 2.5028963e-12, 6.7500696e-07, 6.6356984e-07, 9.3232773e-09, 1.4586571e-19, 2.5751176e-12, 6.7500696e-07, 6.6356984e-07, 9.3232773e-09, 1.4586571e-19, 2.5751176e-12, 6.7500696e-07, 6.6356984e-07, 2.3308193e-09, 2.5350566e-09, 2.57523e-12, 1.458489e-19, 5.8270455e-10, 2.5028963e-12, 1.458489e-19, 2.5751176e-12, 2.6948968e-09, 2.5749502e-12, 1.458489e-19, 5.8270455e-10, 2.5028963e-12, 1.458489e-19, 5.8270455e-10, 2.5028963e-12, 6.7500696e-07, 6.6356984e-07, 5.8270483e-10, 2.5028963e-12, 1.458489e-19, 5.8270455e-10, 2.5028963e-12, 1.458489e-19, 2.5348863e-09, 4.8852866e-39]

我的目标是返回与float数组相同的值,如何解决?

1 个答案:

答案 0 :(得分:1)

您提供给imgf.put(2,100,floats)的参数不正确。 您想从第一个Matrix元素开始,因此为rowcol

提供0
float floats[] = { 255.0, 255.0, 255.0, 255.0, 255.0, 255.0, 255.0, 255.0, ... };
Mat imgf = new Mat(2, 100, CvType.CV_32F);
int row = 0, col = 0;
imgf.put( row, col, floats );