I am creating an array of double values,I want to add the created array to the CvSeq. I have used cvSeqPush for this purpose.But when I am retrieving this array it only shows the first element of the array and rest is garbage. The code is as follows..
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
The problem occurs in line where I am using CV_GET_SEQ_ELEM.Can anybody tell me where is the problem??
Problem is the that
CV_GET_SEQ_ELEMonly gets one element at a time, and you have only specified the first element by passing in the index of 1.You want to call the function
cvCvtSeqToArraywith arguments likecvCvtSeqToArray(s, arr, CV_WHOLE_SEQ);Please note that the array arr passed the second paramter to this function will need to be intialised
E.g.
CvPoint* arr = (CvPoint*)malloc( count*sizeof(CvPoint));where count is the number of elements in the array.CvSeq is strictly used to represent growable 1d arrays, as opposed to the 2d array you are trying to use. If you want to continue use multidimensional arrays that aren’t cvMat, you could try CvSeq of CvSeq but I still think you would have the same problem.
Hence calling CV_GET_SEQ_ELEM only cleanly accesses the single pointer in memory that you give it, which is the first element of the array.
You could also try std::vector of CvSeq or an array of CvSeq but I haven’t tried that.