如何将数组添加到CvSeq以及如何检索数组

时间:2012-02-13 06:24:39

标签: c++ c image-processing opencv

我正在创建一个double值数组,我想将创建的数组添加到CvSeq。我为此目的使用了cvSeqPush。但是当我检索这个数组时,它只显示数组的第一个元素,其余的是垃圾。代码如下..

int Ts = 45;
int count = 0;
//Creating a mtrix CL for clusters
CvMat * CL = cvCreateMat(1,newCP->cols,newCP->type);
//Array for storing the clusters
double * arrClust = (double*)malloc(3*sizeof(double));

//Copying 1st row of newCP to arrClust
arrClust[0] = cvmGet(newCP,0,0);
arrClust[1] = cvmGet(newCP,0,1);
arrClust[2] = cvmGet(newCP,0,2);

//array of distances
double * distance = (double*)malloc(1*sizeof(double));

//array of results
double * result = (double*)malloc(1*sizeof(double));

//Creating a seq to store various clusters
CvMemStorage * strgeClust = cvCreateMemStorage(0);
CvSeq * seqClust = cvCreateSeq(0,sizeof(CvSeq),sizeof(double),strgeClust);
CvSeq * s ;
for(int i=2 ; i<newCP->rows ; i++ )
{
    for(int j=0;j<=count;j++)
    {
        double a = arrClust[0] - cvmGet(newCP,i,0); //a = CL0-newCP0
        double b = arrClust[1] - cvmGet(newCP,i,1); //b = CL1-newCP1
        double c = arrClust[2] - cvmGet(newCP,i,2); //c = CL2-newCP2
        double sum = (a*a)+(b*b)+(c*c);             //(a^2 + b^2 + c^2)
        double sqrtSum = sqrt(sum);

        distance[j] = sqrtSum ;

        if(distance[j]<=Ts)
            result[j] = 0;
        else
            result[j] = 1;
    }
    //Checking for zero in result array 
    int isZero = 1;
    for(int k =0;k<=count;k++)
    {
        if(result[k] == 0)
        {
            isZero =0;
            break;
        }
    }
    if(isZero!=0)
    {
        count = count+1;
        cvSeqPush(seqClust,arrClust);
        arrClust = (double*)malloc(3*sizeof(double));
        arrClust[0] = cvmGet(newCP,i,0);
        arrClust[1] = cvmGet(newCP,i,1);
        arrClust[2] = cvmGet(newCP,i,2);

    }
    else
    {
        double minElement = fnSortForMin(distance,count+1); //Getting the minimum value from distance array
        int index = fnSearchIndexOfMin(distance,minElement,count+1); //Getting the index of minElement
        if(index == count)
        {
            arrClust[0] = (arrClust[0]+cvmGet(newCP,i,0))/2;
            arrClust[1] = (arrClust[1]+cvmGet(newCP,i,1))/2;
            arrClust[2] = (arrClust[2]+cvmGet(newCP,i,2))/2;
        }
        else
        {
            s = seqClust;
            for(int i = 1;i<index;i++)
            {
                s = seqClust->h_next; 
            }
            double * arr = CV_GET_SEQ_ELEM(double,s,index);
            arr[0] = (arr[0]+cvmGet(newCP,i,0))/2;
            arr[1] = (arr[1]+cvmGet(newCP,i,1))/2;
            arr[2] = (arr[2]+cvmGet(newCP,i,2))/2;
        }
    }//End of outer If Block        
}//End Of outer For loop

问题出现在我正在使用CV_GET_SEQ_ELEM的地方。任何人都可以告诉我问题在哪里?

1 个答案:

答案 0 :(得分:1)

问题是CV_GET_SEQ_ELEM一次只获得一个元素,并且您只通过传入索引1来指定第一个元素。

您希望使用类似的参数调用函数cvCvtSeqToArray cvCvtSeqToArray(s, arr, CV_WHOLE_SEQ);

请注意,数组arr通过第二个参数到此函数需要初始化

E.g。 CvPoint* arr = (CvPoint*)malloc( count*sizeof(CvPoint));其中count是数组中元素的数量。

CvSeq严格用于表示可增长的1d数组,而不是您尝试使用的2d数组。如果你想继续使用不是cvMat的多维数组,你可以尝试CvSeq的CvSeq,但我仍然认为你会遇到同样的问题。

因此,只调用CV_GET_SEQ_ELEM干净地访问你给它的内存中的单个指针,这是数组的第一个元素。

您也可以尝试使用CvSeq的std :: vector或CvSeq的数组,但我还没试过。