尝试在OpenCV中设置矩阵的值时指针错误

时间:2012-01-13 01:02:57

标签: c++ eclipse pointers ubuntu opencv

我的代码出现问题。我的代码片段如下所述

这些是我读过的一些示例值

116 117 77 0.170833333333333 0.341880341880342 

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(traindata,&traindata1,0,trainsamplecount);
            cvGetRows(trainclasses,&trainclasses1,0,trainsamplecount);
            cvGetRows(samplewts,&samplewts1,0,trainsamplecount);
            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));

            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;
                cvSetReal2D(traindata,i,0,(float)td[i][0]);
                cvSetReal2D(traindata,i,1,(float)td[i][1]);
                cvSetReal2D(traindata,i,2,(float)td[i][2]);
                cvSetReal2D(traindata,i,3,(float)td[i][3]);
                cvSetReal2D(traindata,i,4,(float)td[i][4]);
                cvSetReal2D(traindata,i,5,(float)td[i][5]);
                cvSet1D(&trainclasses1,i,cvRealScalar(1));
                cvSet1D(&samplewts1,i,cvRealScalar(1));

            }
            }

它在cvSetReal2D上出错。 这会出现以下错误:

OpenCV Error: Bad argument (unrecognized or unsupported array type) in cvPtr2D, file /usr/local/src/OpenCV-2.3.0/modules/core/src/array.cpp, line 1830
terminate called after throwing an instance of 'cv::Exception'

我在OpenCV中的eclipse上使用Ubuntu。 请帮我解决这个问题。

1 个答案:

答案 0 :(得分:0)

以下是您尝试执行的操作的简化示例。这在使用OpenCV 2.3.1的我的机器上正常工作:

#define SAMPLE_COUNT 5

int main(int argc, char** argv)
{
    CvMat* data = cvCreateMat(SAMPLE_COUNT, 6, CV_32FC1);

    for (int i = 0; i < SAMPLE_COUNT; i++)
    {
        cout << "Adding i = " << i << endl;
        cvSetReal2D(data, i, 0, (float)i);
        cvSetReal2D(data, i, 1, (float)i);
        cvSetReal2D(data, i, 2, (float)i);
        cvSetReal2D(data, i, 3, (float)i);
        cvSetReal2D(data, i, 4, (float)i);
        cvSetReal2D(data, i, 5, (float)i);
    }


    return 0;
}

但是,我不确定你用这些方法想要实现的目标:

cvGetRows(data, &data1, 0, samplecount);
cvGetRows(classes, &classes1, 0, samplecount);

您将整个矩阵作为“子矩阵”抓取,因此data1等同于data;同样,classes1相当于classes。那么,为什么不直接使用dataclasses指针?

我认为需要更多信息才能彻底解决您的问题......