问题描述
- 请教大家一个关于vector动态存储的问题
-
我现在正在写一个关于kinect的小程序,定义了一个vector<double>inputvector(12)
,也就是inputvector里面有12个元素,全部初始化为0,现在是我要把我从kinect里面获得的骨骼数据流,总共12个数据每帧这样送到inputvector中去,然后将所有获取到的数据保存到txt文档中,下面我贴出我的代码,现在有一个问题是我只能获取最后一帧的数据保存到txt中,之前所有的帧数据都没有保存进去,求大神帮忙看一下!我基础不好,底子薄 这个问题困扰我一天了!万分感谢!
bool getSkeletonImage(HANDLE &skeletonEvent, Mat &skeletonImage, Mat &colorImage) { UINT gestureLabel = 1; trainingData.setNumDimensions(12); trainingData.setDatasetName("sensorData"); trainingData.setInfoText("this data contains some sensor data"); NUI_SKELETON_FRAME skeletonFrame = { 0 }; bool bfoundskeleton = false; if (NuiSkeletonGetNextFrame(0, &skeletonFrame) == S_OK) { for (int i = 0; i < NUI_SKELETON_COUNT; i++) { if (skeletonFrame.SkeletonData[i].eTrackingState == NUI_SKELETON_TRACKED) { bfoundskeleton = true; break;//跳出for循环 } } } else { cout << "没有找到合适的骨骼" << endl; return FALSE; } if (!bfoundskeleton) { return FALSE; } NuiTransformSmooth(&skeletonFrame, NULL); skeletonImage.setTo(0); for (int i = 0; i < NUI_SKELETON_COUNT; i++) { if (skeletonFrame.SkeletonData[i].eTrackingState == NUI_SKELETON_TRACKED&&skeletonFrame.SkeletonData[i].eSkeletonPositionTrackingState[NUI_SKELETON_POSITION_SHOULDER_CENTER] != NUI_SKELETON_POSITION_NOT_TRACKED) { float fx, fy; for (int j = 0; j < NUI_SKELETON_POSITION_COUNT; j++) { NuiTransformSkeletonToDepthImage(skeletonFrame.SkeletonData[i].SkeletonPositions[j], &fx, &fy);//骨骼坐标转换到深度图像中去 skeletonPoint[i][j].x = (int)fx; skeletonPoint[i][j].y = (int)fy; } for (int j = 0; j < NUI_SKELETON_POSITION_COUNT; j++) { if (skeletonFrame.SkeletonData[i].eSkeletonPositionTrackingState[j] != NUI_SKELETON_POSITION_NOT_TRACKED) { LONG colorx, colory; //从深度空间中获取颜色空间对应像素点的坐标 NuiImageGetColorPixelCoordinatesFromDepthPixel(NUI_IMAGE_RESOLUTION_640x480, 0, skeletonPoint[i][j].x, skeletonPoint[i][j].y, 0, &colorx, &colory); colorPoint[i][j].x = int(colorx); colorPoint[i][j].y = int(colory); circle(skeletonImage, skeletonPoint[i][j], 4, cvScalar(0, 255, 255), 1, 8, 0); circle(colorImage, colorPoint[i][j], 4, cvScalar(0,0,255), 1, 8, 0); } } drawskeleton(colorImage, colorPoint[i], i);//在彩色图像上画上骨骼 drawskeleton(skeletonImage, skeletonPoint[i], i);//画出骨骼图像信息 inputvector[0] = skeletonFrame.SkeletonData[i].SkeletonPositions[NUI_SKELETON_POSITION_HAND_LEFT].x; inputvector[1] = skeletonFrame.SkeletonData[i].SkeletonPositions[NUI_SKELETON_POSITION_HAND_LEFT].y; inputvector[2] = skeletonFrame.SkeletonData[i].SkeletonPositions[NUI_SKELETON_POSITION_HAND_LEFT].z; inputvector[3] = skeletonFrame.SkeletonData[i].SkeletonPositions[NUI_SKELETON_POSITION_WRIST_LEFT].x; inputvector[4] = skeletonFrame.SkeletonData[i].SkeletonPositions[NUI_SKELETON_POSITION_WRIST_LEFT].y; inputvector[5] = skeletonFrame.SkeletonData[i].SkeletonPositions[NUI_SKELETON_POSITION_WRIST_LEFT].z; inputvector[6] = skeletonFrame.SkeletonData[i].SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].x; inputvector[7] = skeletonFrame.SkeletonData[i].SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].y; inputvector[8] = skeletonFrame.SkeletonData[i].SkeletonPositions[NUI_SKELETON_POSITION_HAND_RIGHT].z; inputvector[9] = skeletonFrame.SkeletonData[i].SkeletonPositions[NUI_SKELETON_POSITION_WRIST_RIGHT].x; inputvector[10] = skeletonFrame.SkeletonData[i].SkeletonPositions[NUI_SKELETON_POSITION_WRIST_RIGHT].y; inputvector[11] = skeletonFrame.SkeletonData[i].SkeletonPositions[NUI_SKELETON_POSITION_WRIST_RIGHT].z; cout << "KinectLeftHandData:" << "X=" << inputvector[0] << " Y=" << inputvector[1] << " Z=" << inputvector[2] << endl; cout << "KinectLeftWristData: " << "X=" << inputvector[3] << "Y=" << inputvector[4] << "Z=" << inputvector[5] << endl; cout << "KinectRightHandData:" << "X=" << inputvector[6] << " Y=" << inputvector[7] << " Z=" << inputvector[8] << endl; cout << "KinectRightWristData:" << "X=" << inputvector[9] << " Y=" << inputvector[10] << " Z=" << inputvector[11] << endl; //cout << inputvector.size() << endl; trainingData.addSample(gestureLabel, inputvector); } if (!trainingData.save("TrainingData.txt")) { cout << "error:failed to save dateset to file!" << endl; return EXIT_FAILURE; } string datasetName = trainingData.getDatasetName(); string infoText = trainingData.getInfoText(); UINT numSamples = trainingData.getNumSamples(); UINT numDimensions = trainingData.getNumDimensions(); UINT numClasses = trainingData.getNumClasses(); ClassificationData testData = trainingData.partition(80); if (!trainingData.merge(testData)) { //cout << "failed to save merge datasets" << endl; return EXIT_FAILURE; } trainingData.clear(); } }
解决方案
我自己的观点是:是不是因为我申请的这个inputvector(12),这个空间的大小就是12啊 然后我每帧送过来的数据存进去,
下一帧数据过来之后就会覆盖掉我上一帧的数据,所以我最后存入的只能是最后一帧的数据
解决方案二:
vector会自动扩展空间,当超过当前空间以后,会把数据继续插入,不会覆盖
你要看你的逻辑是不是把数据覆盖了
解决方案三:
vector会自动扩展空间,当超过当前空间以后,会把数据继续插入,不会覆盖 你要看你的逻辑是不是把数据覆盖了
您看一下我代码里面有一行:
trainingData.addSample(gestureLabel, inputvector);
这一行的意思就是每产生一帧数据,我就把该帧数据送到traingData中去,这一句话是写在if条件语句中的,也就是只要我一直有骨骼流数据送进来,那么我就一直把数据放到traingData里面去,直到if条件不满足的时候,再把trainingData中的数据保存到txt文档中,逻辑好像没错,我下面再贴出一个代码是跟这个类似的 您可以看一下就是他的那个 trainingData.addSample是怎么使用的:
//For now we will just add some random data
Random random;
for(UINT i=0; i<100; i++){
sample[0] = random.getRandomNumberUniform(-1.0,1.0);
sample[1] = random.getRandomNumberUniform(-1.0,1.0);
sample[2] = random.getRandomNumberUniform(-1.0,1.0);
//Add the sample to the training data
trainingData.addSample( gestureLabel, sample );
}
//After recording your training data you can then save it to a file
if( !trainingData.save( "TrainingData.grt" ) ){
cout << "ERROR: Failed to save dataset to file!n";
return EXIT_FAILURE;
}
上面代码完整的例子我就不贴出来了,主要就是他写了一个随机数产生器,是一百组的数据,然后每一次for循环产生一组,然后用 trainingData.addSample来保存起来,结束循环之后,将其保存到txt中,这个是可行的 我确实得到了所有的数据,就是不知道为什么我的不行呢?求指教!
解决方案四:
我顶下,别沉啊!赶快来人啊
解决方案五:
人呢?怎么没人出来 没人帮忙吗