在opencv中训练神经网络时出错

时间:2012-01-14 00:57:34

标签: c ubuntu opencv neural-network

我一直在研究这个问题很长一段时间但是我无法在这里找出问题。我正在使用eclipse在Opencv中使用Ubuntu。 我正在尝试用一些值训练神经网络。一些样本值是 62 63 45 0.175925925925926 0.285714285714286 0.247058823529412 1 49 51 37 0.190476190476190 0.274509803921569 0.200000000000000 1

这是神经网络训练的整个循环。  FILE * fid = fopen(“treewall”,“r”);

            while (fscanf(fid,"%f",&a)==1)
            {
                    printf("%f\n",a);
                    trainsamplecount=trainsamplecount+1;
            }
            cout<<trainsamplecount<<endl;
            rewind(fid);
            float td[2000][7];
            int numoftestpoints;
            CvMat* traindata=cvCreateMat(trainsamplecount,6,CV_32FC1);
            CvMat* trainclasses=cvCreateMat(trainsamplecount,1,CV_32FC1);
            CvMat* samplewts=cvCreateMat(trainsamplecount,1,CV_32FC1);
            CvMat* neurallayers=cvCreateMat(4,1,CV_32SC1);
            CvMat traindata1,trainclasses1,neurallayers1,samplewts1;


            cvGetRows(neurallayers,&neurallayers1,0,4);

            cvSet1D(&neurallayers1, 0, cvScalar(6));
            cvSet1D(&neurallayers1,1,cvScalar(3));
            cvSet1D(&neurallayers1,2,cvScalar(3));
            cvSet1D(&neurallayers1, 3, cvScalar(1));

         // CvMat* outdata=Mat::ones(trainsamplecount/7,1,CV_8U);
         // CvMat* testdata(numoftestpoints,7,CV_32FC1);
            cout<<traindata->rows<<endl;
            cout<<traindata->cols<<endl;
            int i=0,j=0;

            if(fid)
            {
            for(int i=0;i<trainsamplecount;i++)
                fscanf(fid,"%f %f %f %f %f %f %f ",&td[i][0],&td[i][1],&td[i][2],&td[i][3],&td[i][4],&td[i][5],&td[i][6]);


            for (i=0;i<trainsamplecount;i++)
            {
                cout<<i<<endl;
                cvGetRow(traindata,&traindata1,i);
                cvSetReal2D(&traindata1,i,0,(float)td[i][0]);
                cvSetReal2D(&traindata1,i,1,(float)td[i][1]);
                cvSetReal2D(&traindata1,i,2,(float)td[i][2]);
                cvSetReal2D(&traindata1,i,3,(float)td[i][3]);
                cvSetReal2D(&traindata1,i,4,(float)td[i][4]);
                cvSetReal2D(&traindata1,i,5,(float)td[i][5]);
                cvGetRow(trainclasses,&trainclasses1,i);
                cvSet1D(&trainclasses1,i,cvRealScalar(1));
                cvGetRow(samplewts,&samplewts1,i);


                cvSet1D(samplewts,i,cvRealScalar(1));

            }
            }

但是这个循环终止于语句cvGetRow(trainclasses,&amp; trainclasses1,i); 之前它曾经在cvSet1D行显示错误的参数错误(samplewts,i,cvRealScalar(1));

请帮我解决这个问题,因为我无法想到解决方案。

1 个答案:

答案 0 :(得分:0)

我认为该行

cvSet1D(&trainclasses1,i,cvRealScalar(1));

应该是

cvSet1D(&trainclasses1,0,cvRealScalar(1));

,因为trainclasses1将有1行和1列。