我正在尝试使用OpenCV在我的C ++应用程序中实现公共空间模式(CSP)。我现在有我的CSPMat(14 x 14维)。我需要选择CSPMat的前两行和后两行并将其放在某个矩阵中,例如rMat(将是4 x 14维)。
double dat[196] = {-0.40643,0.56297,0.24768,-0.21843,0.0020409,-0.071199,
-0.074495,-0.28166,0.04003,0.036224,0.35441,0.015098,-0.42881,0.07902,0.032339,
-0.35331,-0.31707,0.19567,-0.074969,0.14364,-0.15196,-0.29226,0.1002,0.036617,0.71354,
0.11413,-0.0096681,-0.25867,-0.21026,0.20852,-0.21658,0.12999,0.19116,-0.24877,
-0.072983,0.058441,-0.046685,-0.44232,-0.20248,0.58385,0.11024,-0.38662,-0.32475,
-0.20537,-0.24724,0.14887,0.018617,0.24893,-0.26009,0.13939,0.23181,-0.31028,
-0.36689,0.19742,-0.17256,0.51606,-0.042443,-0.19961,0.42573,0.045363,0.056726,0.12472,
-0.24221,0.012676,0.10124,-0.8274,0.015785,-0.031468,-0.036927,-0.0038262,
-0.10933,-0.27092,0.051624,0.11862,0.47132,-0.25812,0.37111,-0.014466,-0.39592,
-0.34548,0.059974,-0.3252,-0.10813,0.27212,-0.1103,0.058941,0.020318,-0.16778,0.1312,
0.10562,0.26175,0.088419,-0.13917,0.89163,0.17807,0.063471,-0.0121,-0.028945,
-0.31825,0.039514,0.11289,0.035025,0.041933,-0.23419,-0.5758,0.50917,-0.19886,0.3074,
0.21067,-0.15481,0.10478,0.14968,-0.013939,0.107,-0.1227,-0.062843,0.03534,-0.0043759,
0.11149,-0.0075394,-0.027777,-0.96728,0.094132,-0.065276,0.059764,0.050088,0.088534,
0.091467,0.040229,-0.22834,-0.043955,0.71084,-0.49935,0.16458,-0.22962,-0.23088,
0.079861,0.037212,-0.0189,-0.17949,-0.037697,-0.090957,-0.30513,0.2583,-0.058026,
0.37724,-0.14635,-0.13089,-0.4395,0.36025,-0.11406,0.36466,0.071945,0.41491,0.46926,
0.018588,-0.40528,-0.42208,0.083901,-0.31978,0.2096,0.076437,-0.088993,-0.42626,
0.17476,0.18124,-0.158,0.0011488,0.27109,-0.0087883,-0.49613,0.68606,0.10353,0.04053,
-0.29863,0.029601,0.13528,0.2491,-0.15446,-0.03623,-0.039323,-0.010952, 0.12699,
0.040763,0.048881,-0.5927,0.29154,0.047348,-0.31823,-0.19709,0.2386,0.51098,-0.055752,
-0.11963,0.11053,0.2313};
cv::Mat CSPMat(14,14, cv::DataType<float>::type);
int Ai =0;
int Aj = 0;
for (Ai = 0; Ai < 14; Ai++){
for (Aj = 0; Aj < 14; Aj++){
CSPMat.at<float>(Ai, Aj) = dat[Aj+(14*Ai)];
}
}
如何构建rMat矩阵(4x14)获取CSPMat的前两行和后两行?
非常感谢任何帮助。
答案 0 :(得分:0)
首先,让您的生活更轻松,并为数据创建一个 float 数组,然后您可以使用它设置Mat构造函数,并保存复制循环。
然后你可以抓住一些Mat.row(i),将它们推入一个新的Mat,最后重塑为所需的行数:
float dat[196] = {-0.40643,0.56297,0.24768,-0.21843,0.0020409,-0.071199,
-0.074495,-0.28166,0.04003,0.036224,0.35441,0.015098,-0.42881,0.07902,0.032339,
-0.35331,-0.31707,0.19567,-0.074969,0.14364,-0.15196,-0.29226,0.1002,0.036617,0.71354,
0.11413,-0.0096681,-0.25867,-0.21026,0.20852,-0.21658,0.12999,0.19116,-0.24877,
-0.072983,0.058441,-0.046685,-0.44232,-0.20248,0.58385,0.11024,-0.38662,-0.32475,
-0.20537,-0.24724,0.14887,0.018617,0.24893,-0.26009,0.13939,0.23181,-0.31028,
-0.36689,0.19742,-0.17256,0.51606,-0.042443,-0.19961,0.42573,0.045363,0.056726,0.12472,
-0.24221,0.012676,0.10124,-0.8274,0.015785,-0.031468,-0.036927,-0.0038262,
-0.10933,-0.27092,0.051624,0.11862,0.47132,-0.25812,0.37111,-0.014466,-0.39592,
-0.34548,0.059974,-0.3252,-0.10813,0.27212,-0.1103,0.058941,0.020318,-0.16778,0.1312,
0.10562,0.26175,0.088419,-0.13917,0.89163,0.17807,0.063471,-0.0121,-0.028945,
-0.31825,0.039514,0.11289,0.035025,0.041933,-0.23419,-0.5758,0.50917,-0.19886,0.3074,
0.21067,-0.15481,0.10478,0.14968,-0.013939,0.107,-0.1227,-0.062843,0.03534,-0.0043759,
0.11149,-0.0075394,-0.027777,-0.96728,0.094132,-0.065276,0.059764,0.050088,0.088534,
0.091467,0.040229,-0.22834,-0.043955,0.71084,-0.49935,0.16458,-0.22962,-0.23088,
0.079861,0.037212,-0.0189,-0.17949,-0.037697,-0.090957,-0.30513,0.2583,-0.058026,
0.37724,-0.14635,-0.13089,-0.4395,0.36025,-0.11406,0.36466,0.071945,0.41491,0.46926,
0.018588,-0.40528,-0.42208,0.083901,-0.31978,0.2096,0.076437,-0.088993,-0.42626,
0.17476,0.18124,-0.158,0.0011488,0.27109,-0.0087883,-0.49613,0.68606,0.10353,0.04053,
-0.29863,0.029601,0.13528,0.2491,-0.15446,-0.03623,-0.039323,-0.010952, 0.12699,
0.040763,0.048881,-0.5927,0.29154,0.047348,-0.31823,-0.19709,0.2386,0.51098,-0.055752,
-0.11963,0.11053,0.2313};
cv::Mat CSPMat(14,14, cv::DataType<float>::type, dat);
Mat top_bot_2;
top_bot_2.push_back( CSPMat.row(0) );
top_bot_2.push_back( CSPMat.row(1) );
top_bot_2.push_back( CSPMat.row(12) );
top_bot_2.push_back( CSPMat.row(13) );
Mat res = top_bot_2.reshape(1,4);
cerr << res << endl;
[-0.40643001, 0.56296998, 0.24767999, -0.21843, 0.0020409001, -0.071199, -0.074495003, -0.28165999, 0.040029999, 0.036224, 0.35440999, 0.015098, -0.42881, 0.079020001;
0.032338999, -0.35330999, -0.31707001, 0.19566999, -0.074969001, 0.14364, -0.15196, -0.29225999, 0.1002, 0.036617, 0.71354002, 0.11413, -0.0096680997, -0.25867;
0.27109, -0.0087882997, -0.49612999, 0.68606001, 0.10353, 0.04053, -0.29863, 0.029601, 0.13528, 0.2491, -0.15446, -0.036230002, -0.039322998, -0.010952;
0.12699001, 0.040762998, 0.048881002, -0.5927, 0.29154, 0.047348, -0.31823, -0.19709, 0.2386, 0.51098001, -0.055752002, -0.11963, 0.11053, 0.2313]