如何将数组添加到 CvSeq 以及如何检索数组
我正在创建一个双精度值数组,我想将创建的数组添加到 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 的地方。有人能告诉我问题出在哪里吗?
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??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是
CV_GET_SEQ_ELEM
一次只能获取一个元素,并且您仅通过传入索引 1 来指定第一个元素。您想要调用函数
cvCvtSeqToArray
与像这样的论点cvCvtSeqToArray(s, arr, CV_WHOLE_SEQ);
请注意,将第二个参数传递给该函数的数组 arr 需要进行初始化
。
CvPoint* arr = (CvPoint*)malloc( count*sizeof(CvPoint));
其中 count 是数组中的元素数量。CvSeq 严格用于表示可增长的一维数组,而不是您尝试使用的二维数组。如果您想继续使用不是 cvMat 的多维数组,您可以尝试 CvSeq 的 CvSeq 但我仍然认为您会遇到同样的问题。
因此,调用 CV_GET_SEQ_ELEM 只会干净地访问您给它的内存中的单个指针,即数组的第一个元素。
您也可以尝试 std::CvSeq 向量或 CvSeq 数组,但我还没有尝试过。
Problem is the that
CV_GET_SEQ_ELEM
only 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
cvCvtSeqToArray
with 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.