//Find all the matched points void findPairs( const CvSeq* objectKeypoints, const CvSeq* objectDescriptors, const CvSeq* imageKeypoints, const CvSeq* imageDescriptors, std::vector<int>& ptpairs ) { int i; CvSeqReader reader, kreader; cvStartReadSeq( objectKeypoints, &kreader ); cvStartReadSeq( objectDescriptors, &reader ); ptpairs.clear(); for( i = 0; i < objectDescriptors->total; i++ ) { const CvSURFPoint* kp = (const CvSURFPoint*)kreader.ptr; const float* descriptor = (const float*)reader.ptr; CV_NEXT_SEQ_ELEM( kreader.seq->elem_size, kreader ); CV_NEXT_SEQ_ELEM( reader.seq->elem_size, reader ); int nearest_neighbor = naiveNearestNeighbor( descriptor, kp->laplacian, imageKeypoints, imageDescriptors ); if( nearest_neighbor >= 0 ) { ptpairs.push_back(i); ptpairs.push_back(nearest_neighbor); } } }
void findPairs( const CvSeq* objectKeypoints, const CvSeq* objectDescriptors, const CvSeq* imageKeypoints, const CvSeq* imageDescriptors, struct pairList * ptpairs ) { CvSeqReader reader, kreader; cvStartReadSeq( objectKeypoints, &kreader , 0 ); cvStartReadSeq( objectDescriptors, &reader , 0 ); struct pairList * pairs = initializePairList(ptpairs,objectDescriptors->total); if (pairs==0) { fprintf(stderr,"Could not allocate enough memory for pointer pairs\n"); return ; } int i; for( i = 0; i < objectDescriptors->total; i++ ) { const CvSURFPoint* kp = (const CvSURFPoint*)kreader.ptr; const float* descriptor = (const float*)reader.ptr; CV_NEXT_SEQ_ELEM( kreader.seq->elem_size, kreader ); CV_NEXT_SEQ_ELEM( reader.seq->elem_size, reader ); int nearest_neighbor = naiveNearestNeighbor( descriptor, kp->laplacian, imageKeypoints, imageDescriptors ); if( nearest_neighbor >= 0 ) { pairs->item[i].p1 = i; pairs->item[i].p2 = nearest_neighbor; } } destroyPairList(&pairs); }
void CvFaceElement::Energy() { CvSeqReader reader, reader2; cvStartReadSeq( m_seqRects, &reader ); for (int i = 0; i < m_seqRects->total; i++) { CvTrackingRect* pRect = (CvTrackingRect*)(reader.ptr); // outside and inside rects cvStartReadSeq( m_seqRects, &reader2 ); for (int j = 0; j < m_seqRects->total; j++) { CvTrackingRect* pRect2 = (CvTrackingRect*)(reader2.ptr); if (i != j) { if (RectInRect(pRect2->r, pRect->r)) pRect->nRectsInThis ++; else if (pRect2->r.y + pRect2->r.height <= pRect->r.y) pRect->nRectsOnTop ++; else if (pRect2->r.y >= pRect->r.y + pRect->r.height) pRect->nRectsOnBottom ++; else if (pRect2->r.x + pRect2->r.width <= pRect->r.x) pRect->nRectsOnLeft ++; else if (pRect2->r.x >= pRect->r.x + pRect->r.width) pRect->nRectsOnRight ++; } CV_NEXT_SEQ_ELEM( sizeof(CvTrackingRect), reader2 ); } // energy pRect->Energy(m_trPrev); CV_NEXT_SEQ_ELEM( sizeof(CvTrackingRect), reader ); } }//void CvFaceElement::Energy()
int naiveNearestNeighbor( const float* vec, int laplacian, const CvSeq* model_keypoints, const CvSeq* model_descriptors ) { int length = (int)(model_descriptors->elem_size/sizeof(float)); int i, neighbor = -1; double d, dist1 = 1e6, dist2 = 1e6; CvSeqReader reader, kreader; cvStartReadSeq( model_keypoints, &kreader, 0 ); cvStartReadSeq( model_descriptors, &reader, 0 ); for( i = 0; i < model_descriptors->total; i++ ) { const CvSURFPoint* kp = (const CvSURFPoint*)kreader.ptr; const float* mvec = (const float*)reader.ptr; CV_NEXT_SEQ_ELEM( kreader.seq->elem_size, kreader ); CV_NEXT_SEQ_ELEM( reader.seq->elem_size, reader ); if( laplacian != kp->laplacian ) continue; d = compareSURFDescriptors( vec, mvec, dist2, length ); if( d < dist1 ) { dist2 = dist1; dist1 = d; neighbor = i; } else if ( d < dist2 ) dist2 = d; } if ( dist1 < 0.6*dist2 ) return neighbor; return -1; }
/** * Write out a Protocol to YAML file, given an initialized CvFileStorage */ void WriteProtocol(Protocol* myP, CvFileStorage* fs){ if (fs==0){ printf("fs is zero! Could you have specified the wrong directory?\n"); return; } /** Write out Protocol **/ cvStartWriteStruct(fs,"Protocol",CV_NODE_MAP,NULL); if (myP->Filename!=NULL) cvWriteString(fs,"Filename",myP->Filename); if (myP->Description!=NULL) cvWriteString(fs,"Description",myP->Description); cvStartWriteStruct(fs,"GridSize",CV_NODE_MAP,NULL); cvWriteInt(fs,"height",myP->GridSize.height); cvWriteInt(fs,"width",myP->GridSize.width); cvEndWriteStruct(fs); //printf("yo\n"); /** Write Out Steps **/ cvStartWriteStruct(fs,"Steps",CV_NODE_SEQ,NULL); int j; int jtot=myP->Steps->total; CvSeqReader StepReader; cvStartReadSeq(myP->Steps,&StepReader,0); for (j = 0; j < jtot; ++j) { //printf("About to write step number %d\n",j); CvSeq** CurrentMontagePtr = (CvSeq**) StepReader.ptr; CvSeq* CurrentMontage=*CurrentMontagePtr; assert(CurrentMontage!=NULL); // printf("ping\n"); // printf("CurrentMontage->total=%d",CurrentMontage->total); cvStartWriteStruct(fs,NULL,CV_NODE_SEQ,NULL); int k; int ktot=CurrentMontage->total; // printf("ktot=%d\n",ktot); CvSeqReader MontageReader; cvStartReadSeq(CurrentMontage,&MontageReader); for (k = 0; k < ktot; ++k) { // printf("About to write polygon number %d\n",k); WormPolygon** CurrentPolygonPtr= (WormPolygon**) MontageReader.ptr; WormPolygon* CurrentPolygon=*CurrentPolygonPtr; cvWrite(fs,NULL,CurrentPolygon->Points); CV_NEXT_SEQ_ELEM(CurrentMontage->elem_size,MontageReader); } CurrentMontagePtr=NULL; CurrentMontage=NULL; cvEndWriteStruct(fs); /** Loop to Next Step **/ CV_NEXT_SEQ_ELEM(myP->Steps->elem_size,StepReader); } cvEndWriteStruct(fs); cvEndWriteStruct(fs); }
int VerifyProtocol(Protocol* p){ printf("\n\n========== VERIFYING PROTOCOL============\n"); if (p==NULL){ printf("Protocol is NULL\n"); return -1; } printf("Protocol description: %s\n", p->Description); printf("Filename= %s\n",p->Filename); printf("Total number of steps: p->Steps->total=%d\n",p->Steps->total); CvSeqReader StepReader; cvStartReadSeq(p->Steps, &StepReader,0); int numsteps=p->Steps->total; /** Let's loop through all of the steps **/ for (int i= 0; i< numsteps; ++i) { printf("Step i=%d\n",i); CvSeq* CurrMontage= *( (CvSeq**) StepReader.ptr); printf("\tCurrMontage has %d polygons\n",CurrMontage->total); int numPolygons=CurrMontage->total; int j; /** Let's loop through the polygons **/ CvSeqReader MontageReader; cvStartReadSeq(CurrMontage, &MontageReader); for (j = 0; j < numPolygons; ++j) { WormPolygon* poly= *( (WormPolygon**) MontageReader.ptr ); int numpts=poly->Points->total; printf(" numpts=%d\n",numpts); //PrintPointsOfSeq(poly->Points); CV_NEXT_SEQ_ELEM( CurrMontage->elem_size,MontageReader); } /** Progress to the next step **/ CV_NEXT_SEQ_ELEM( p->Steps->elem_size, StepReader ); } printf("========================================\n"); return 0; }
void matchingORB(const CvSeq* sd, const CvSeq* id, std::vector<int> &pairs) { int i, j, k, d1, d2, jj, dist, i_total, s_total; unsigned char *i_ptr, *s_ptr; CvSeqReader i_reader; CvSeqReader s_reader; i_total = id->total; s_total = sd->total; cvStartReadSeq(sd, &s_reader, 0); s_ptr = (unsigned char*) s_reader.ptr; for(i=0; i<s_total; i++) { d1 = d2 = 0; jj = -1; cvStartReadSeq(id, &i_reader, 0); i_ptr = (unsigned char*) i_reader.ptr; for(j=0; j<i_total; j++) { dist = 0; for(k=0; k<32; k++) dist += (s_ptr[k]==i_ptr[k]) ? 1 : 0; if(dist>d1) { d2 = d1; d1 = dist; jj = j; } else if(dist>d2) d2 = dist; CV_NEXT_SEQ_ELEM(i_reader.seq->elem_size, i_reader); i_ptr = (unsigned char*) i_reader.ptr; } if((jj>=0) && (d2 < 0.7*d1)) { pairs.push_back(i); pairs.push_back(jj); } CV_NEXT_SEQ_ELEM(s_reader.seq->elem_size, s_reader); s_ptr = (unsigned char*) s_reader.ptr; } }
CV_IMPL void cvClearSubdivVoronoi2D( CvSubdiv2D * subdiv ) { int elem_size; int i, total; CvSeqReader reader; CV_FUNCNAME( "cvClearVoronoi2D" ); __BEGIN__; if( !subdiv ) CV_ERROR( CV_StsNullPtr, "" ); /* clear pointers to voronoi points */ total = subdiv->edges->total; elem_size = subdiv->edges->elem_size; cvStartReadSeq( (CvSeq *) (subdiv->edges), &reader, 0 ); for( i = 0; i < total; i++ ) { CvQuadEdge2D *quadedge = (CvQuadEdge2D *) reader.ptr; quadedge->pt[1] = quadedge->pt[3] = 0; CV_NEXT_SEQ_ELEM( elem_size, reader ); } /* remove voronoi points */ total = subdiv->total; elem_size = subdiv->elem_size; cvStartReadSeq( (CvSeq *) subdiv, &reader, 0 ); for( i = 0; i < total; i++ ) { CvSubdiv2DPoint *pt = (CvSubdiv2DPoint *) reader.ptr; /* check for virtual point. it is also check that the point exists */ if( pt->flags & CV_SUBDIV2D_VIRTUAL_POINT_FLAG ) { cvSetRemoveByPtr( (CvSet*)subdiv, pt ); } CV_NEXT_SEQ_ELEM( elem_size, reader ); } subdiv->is_geometry_valid = 0; __END__; }
void CvFaceElement::MergeRects(int d) { int nRects = m_seqRects->total; CvSeqReader reader, reader2; cvStartReadSeq( m_seqRects, &reader ); int i, j; for (i = 0; i < nRects; i++) { CvTrackingRect* pRect1 = (CvTrackingRect*)(reader.ptr); cvStartReadSeq( m_seqRects, &reader2 ); cvSetSeqReaderPos(&reader2, i + 1); for (j = i + 1; j < nRects; j++) { CvTrackingRect* pRect2 = (CvTrackingRect*)(reader2.ptr); if (abs(pRect1->ptCenter.y - pRect2->ptCenter.y) < d && abs(pRect1->r.height - pRect2->r.height) < d) { CvTrackingRect rNew; rNew.iColor = (pRect1->iColor + pRect2->iColor + 1) / 2; rNew.r.x = min(pRect1->r.x, pRect2->r.x); rNew.r.y = min(pRect1->r.y, pRect2->r.y); rNew.r.width = max(pRect1->r.x + pRect1->r.width, pRect2->r.x + pRect2->r.width) - rNew.r.x; rNew.r.height = min(pRect1->r.y + pRect1->r.height, pRect2->r.y + pRect2->r.height) - rNew.r.y; if (rNew.r != pRect1->r && rNew.r != pRect2->r) { rNew.ptCenter = Center(rNew.r); cvSeqPush(m_seqRects, &rNew); } } CV_NEXT_SEQ_ELEM( sizeof(CvTrackingRect), reader2 ); } CV_NEXT_SEQ_ELEM( sizeof(CvTrackingRect), reader ); } // delete equal rects for (i = 0; i < m_seqRects->total; i++) { CvTrackingRect* pRect1 = (CvTrackingRect*)cvGetSeqElem(m_seqRects, i); int j_begin = i + 1; for (j = j_begin; j < m_seqRects->total;) { CvTrackingRect* pRect2 = (CvTrackingRect*)cvGetSeqElem(m_seqRects, j); if (pRect1->r == pRect2->r) cvSeqRemove(m_seqRects, j); else j++; } } }//void CvFaceElement::MergeRects(int d)
CV_IMPL void cvStartReadRawData( const CvFileStorage* fs, const CvFileNode* src, CvSeqReader* reader ) { int node_type; CV_CHECK_FILE_STORAGE( fs ); if( !src || !reader ) CV_Error( CV_StsNullPtr, "Null pointer to source file node or reader" ); node_type = CV_NODE_TYPE(src->tag); if( node_type == CV_NODE_INT || node_type == CV_NODE_REAL ) { // emulate reading from 1-element sequence reader->ptr = (schar*)src; reader->block_max = reader->ptr + sizeof(*src)*2; reader->block_min = reader->ptr; reader->seq = 0; } else if( node_type == CV_NODE_SEQ ) { cvStartReadSeq( src->data.seq, reader, 0 ); } else if( node_type == CV_NODE_NONE ) { memset( reader, 0, sizeof(*reader) ); } else CV_Error( CV_StsBadArg, "The file node should be a numerical scalar or a sequence" ); }
CV_IMPL void cvStartReadChainPoints( CvChain * chain, CvChainPtReader * reader ) { int i; CV_FUNCNAME( "cvStartReadChainPoints" ); __BEGIN__; if( !chain || !reader ) CV_ERROR( CV_StsNullPtr, "" ); if( chain->elem_size != 1 || chain->header_size < (int)sizeof(CvChain)) CV_ERROR_FROM_STATUS( CV_BADSIZE_ERR ); cvStartReadSeq( (CvSeq *) chain, (CvSeqReader *) reader, 0 ); CV_CHECK(); reader->pt = chain->origin; for( i = 0; i < 8; i++ ) { reader->deltas[i][0] = (char) icvCodeDeltas[i].x; reader->deltas[i][1] = (char) icvCodeDeltas[i].y; } __END__; }
// the function draws all the squares in the image void drawSquares( IplImage* img, CvSeq* squares ) { CvSeqReader reader; IplImage* cpy = cvCloneImage( img ); int i; // initialize reader of the sequence cvStartReadSeq( squares, &reader, 0 ); // read 4 sequence elements at a time (all vertices of a square) for( i = 0; i < squares->total; i += 4 ){ CvPoint* rect = pt; int count = 4; // read 4 vertices memcpy( pt, reader.ptr, squares->elem_size ); CV_NEXT_SEQ_ELEM( squares->elem_size, reader ); memcpy( pt + 1, reader.ptr, squares->elem_size ); CV_NEXT_SEQ_ELEM( squares->elem_size, reader ); memcpy( pt + 2, reader.ptr, squares->elem_size ); CV_NEXT_SEQ_ELEM( squares->elem_size, reader ); memcpy( pt + 3, reader.ptr, squares->elem_size ); CV_NEXT_SEQ_ELEM( squares->elem_size, reader ); // draw the square as a closed polyline cvPolyLine( cpy, &rect, &count, 1, 1, CV_RGB(0,255,0), 3, CV_AA, 0 ); if(i == 0){ sort_vertex(rect); //get the four vertex and sort them break; } } // show the resultant image cvShowImage( wndname, cpy ); cvReleaseImage( &cpy ); }
void paint_voronoi( CvSubdiv2D* subdiv, IplImage* img ) { CvSeqReader reader; int i, total = subdiv->edges->total; int elem_size = subdiv->edges->elem_size; cvCalcSubdivVoronoi2D( subdiv ); cvStartReadSeq( (CvSeq*)(subdiv->edges), &reader, 0 ); for( i = 0; i < total; i++ ) { CvQuadEdge2D* edge = (CvQuadEdge2D*)(reader.ptr); if( CV_IS_SET_ELEM( edge )) { CvSubdiv2DEdge e = (CvSubdiv2DEdge)edge; // left draw_subdiv_facet( img, cvSubdiv2DRotateEdge( e, 1 )); // right draw_subdiv_facet( img, cvSubdiv2DRotateEdge( e, 3 )); } CV_NEXT_SEQ_ELEM( elem_size, reader ); } }
/** Draws the identified Tetris pieces on the given image. */ void Camera::drawTetris( IplImage* img, CvSeq* tetrisPieces ) { CvSeqReader reader; int i; // initialize reader of the sequence cvStartReadSeq( tetrisPieces, &reader, 0 ); // read the pieces sequence elements at a time (all vertices of the piece) for( i = 0; i < tetrisPieces->total; i += 6 ) { CvPoint pt[6], *rect = pt; int count = 6; // read 6 vertices CV_READ_SEQ_ELEM( pt[0], reader ); CV_READ_SEQ_ELEM( pt[1], reader ); CV_READ_SEQ_ELEM( pt[2], reader ); CV_READ_SEQ_ELEM( pt[3], reader ); CV_READ_SEQ_ELEM( pt[4], reader ); CV_READ_SEQ_ELEM( pt[5], reader ); // draw the piece as a closed polyline cvPolyLine( img, &rect, &count, 1, 1, CV_RGB(255,0,0), 3, CV_AA, 0 ); } return; }
void Blob::copy_edges(const CvSeq* _edges) { CV_FUNCNAME( "Blob::copy_edges" ); __BEGIN__; cvClearSeq(this->edges_); //- copy the given sequence CvSeqReader seq_reader; CvSeqWriter seq_writer; CvPoint current_edge; int i; CV_CALL( cvStartReadSeq( _edges, &seq_reader) ); CV_CALL( cvStartAppendToSeq( this->edges_, &seq_writer ) ); for( i = 0; i < _edges->total; i++) { CV_READ_SEQ_ELEM ( current_edge , seq_reader); CV_WRITE_SEQ_ELEM( current_edge , seq_writer ); } CV_CALL( cvEndWriteSeq( &seq_writer ) ); __END__; __ISL_CHECK_ERROR__; }
static void icvSeqElemsClearMask( CvSeq* seq, int offset, int clear_mask ) { CV_FUNCNAME("icvStartScanGraph"); __BEGIN__; CvSeqReader reader; int i, total, elem_size; if( !seq ) CV_ERROR_FROM_STATUS( CV_NULLPTR_ERR ); elem_size = seq->elem_size; total = seq->total; if( (unsigned)offset > (unsigned)elem_size ) CV_ERROR_FROM_STATUS( CV_BADARG_ERR ); CV_CALL( cvStartReadSeq( seq, &reader )); for( i = 0; i < total; i++ ) { int* flag_ptr = (int*)(reader.ptr + offset); *flag_ptr &= ~clear_mask; CV_NEXT_SEQ_ELEM( elem_size, reader ); } __END__; }
std::list<int*>* vpKeyPointSurf::matchPoint(std::list<float*> descriptorList, std::list<int> laplacianList) { std::list<int*>* pairPoints = new std::list<int*>; if(descriptorList.size() != laplacianList.size()){ vpTRACE("Error, the two lists have different number of element"); return pairPoints; } CvSeqReader reader; cvStartReadSeq( ref_descriptors, &reader ); std::list<float*>::const_iterator descriptorListIter = descriptorList.begin(); std::list<int>::const_iterator laplacianListIter = laplacianList.begin(); descriptorList.front(); int indexList = 0; while(descriptorListIter != descriptorList.end()){ float* descriptor = *descriptorListIter; int nearest_neighbor = naiveNearestNeighbor( descriptor, *laplacianListIter, ref_keypoints, ref_descriptors); if(nearest_neighbor >= 0){ int* tab; tab = new int[2]; tab[0] = nearest_neighbor; tab[1] = indexList; pairPoints->push_back(tab); } indexList++; ++descriptorListIter; ++laplacianListIter; } return pairPoints; }
void drawSquares(IplImage* img, CvSeq* squares, PointMatrix &mat) { CvSeqReader reader; IplImage* cpy = cvCloneImage(img); cvStartReadSeq(squares, &reader, 0); for (int i = 0; i < squares->total; i++) { CvPoint pt[4], *rect = pt; int count = 4; CV_READ_SEQ_ELEM(pt[0], reader); CV_READ_SEQ_ELEM(pt[1], reader); CV_READ_SEQ_ELEM(pt[2], reader); CV_READ_SEQ_ELEM(pt[3], reader); cvLine(cpy, pt[0], pt[2], CV_RGB(0, 0, 0), 1); cvLine(cpy, pt[1], pt[3], CV_RGB(255, 255, 0), 1); MyCvPoint myCvPoint( (pt[0].x + pt[2].x) /2, (pt[1].y + pt[2].y)/2, img->width, img->height); mat.AddMem(myCvPoint); cvPolyLine(cpy, &rect, &count, 1, 1, CV_RGB(0, 255, 255), 1, 8, 0); } // cvShowImage("After Modify", cpy); cvReleaseImage(&cpy); }
virtual void operator()(const cv::BlockedRange& range) const { #ifdef HAVE_TBB tbb::spin_mutex::scoped_lock lock; #endif CvSeqReader reader; int begin = range.begin(); int end = range.end(); int weak_count = end - begin; CvDTree* tree; for (int i=0; i<k; ++i) { float tmp_sum = 0.0f; if ((weak[i]) && (weak_count)) { cvStartReadSeq( weak[i], &reader ); cvSetSeqReaderPos( &reader, begin ); for (int j=0; j<weak_count; ++j) { CV_READ_SEQ_ELEM( tree, reader ); tmp_sum += shrinkage*(float)(tree->predict(sample, missing)->value); } } #ifdef HAVE_TBB lock.acquire(SumMutex); sum[i] += tmp_sum; lock.release(); #else sum[i] += tmp_sum; #endif } } // Tree_predictor::operator()
/*! \deprecated This method is deprecated, you should use matchPoint(std::list<float*> , std::list<int> ) instead. Computes the SURF points given by their descriptor and laplacian and try to match them with the points in the reference list. Only the matched points are stored. The two lists must have the same number of element while they correspond the same unique list of point. \warning The list returned contains allocated data (2 int per element). Must be deleted to avoid memory leak. \param descriptorList : The list of the descriptor \param laplacianList : The list of laplacian \return the list of the pair, the first element contains the index in the reference sequence and the second element contains the index in the list given in parameter. */ vp_deprecated vpList<int*>* vpKeyPointSurf::matchPoint(vpList<float*> descriptorList, vpList<int> laplacianList) { vpList<int*>* pairPoints = new vpList<int*>; if(descriptorList.nb != laplacianList.nb){ vpTRACE("Error, the two lists have different number of element"); return pairPoints; } CvSeqReader reader; cvStartReadSeq( ref_descriptors, &reader ); descriptorList.front(); pairPoints->front(); laplacianList.front(); int indexList = 0; while(!descriptorList.outside()){ float* descriptor = descriptorList.value(); int nearest_neighbor = naiveNearestNeighbor( descriptor, laplacianList.value(), ref_keypoints, ref_descriptors); if(nearest_neighbor >= 0){ int* tab; tab = new int[2]; tab[0] = nearest_neighbor; tab[1] = indexList; pairPoints->addRight(tab); } indexList++; descriptorList.next(); laplacianList.next(); } return pairPoints; }
// the function draws all the squares in the image void drawSquares( IplImage* img, CvSeq* squares ) { CvSeqReader reader; IplImage* cpy = cvCloneImage( img ); int i; // initialize reader of the sequence cvStartReadSeq( squares, &reader, 0 ); // read 4 sequence elements at a time (all vertices of a square) for( i = 0; i < squares->total; i += 4 ) { CvPoint pt[4], *rect = pt; int count = 4; // read 4 vertices CV_READ_SEQ_ELEM( pt[0], reader ); CV_READ_SEQ_ELEM( pt[1], reader ); CV_READ_SEQ_ELEM( pt[2], reader ); CV_READ_SEQ_ELEM( pt[3], reader ); // draw the square as a closed polyline cvPolyLine( cpy, &rect, &count, 1, 1, CV_RGB(0,255,0), 3, CV_AA, 0 ); } // show the resultant image cvShowImage( wndname, cpy ); cvReleaseImage( &cpy ); }
// This function reads data and responses from the file <filename> static int read_num_class_data( const char* filename, int var_count, CvMat** data, CvMat** responses ) { const int M = 1024; FILE* f = fopen( filename, "rt" ); CvMemStorage* storage; CvSeq* seq; char buf[M+2]; float* el_ptr; CvSeqReader reader; int i, j; if( !f ) return 0; el_ptr = new float[var_count+1]; storage = cvCreateMemStorage(); seq = cvCreateSeq( 0, sizeof(*seq), (var_count+1)*sizeof(float), storage ); for(;;) { char* ptr; if( !fgets( buf, M, f ) || !strchr( buf, ',' ) ) break; el_ptr[0] = buf[0]; ptr = buf+2; for( i = 1; i <= var_count; i++ ) { int n = 0; sscanf( ptr, "%f%n", el_ptr + i, &n ); ptr += n + 1; } if( i <= var_count ) break; cvSeqPush( seq, el_ptr ); } fclose(f); *data = cvCreateMat( seq->total, var_count, CV_32F ); *responses = cvCreateMat( seq->total, 1, CV_32F ); cvStartReadSeq( seq, &reader ); for( i = 0; i < seq->total; i++ ) { const float* sdata = (float*)reader.ptr + 1; float* ddata = data[0]->data.fl + var_count*i; float* dr = responses[0]->data.fl + i; for( j = 0; j < var_count; j++ ) ddata[j] = sdata[j]; *dr = sdata[-1]; CV_NEXT_SEQ_ELEM( seq->elem_size, reader ); } cvReleaseMemStorage( &storage ); delete[] el_ptr; return 1; }
double BlobGetMaxYatMinX::operator()(Blob &blob) { double result = LONG_MIN; CvSeqReader reader; CvPoint actualPoint; BlobContour::t_PointList externContour; externContour = blob.GetExternalContour()->GetContourPoints(); if( !externContour ) return result; cvStartReadSeq( externContour, &reader); for( int i=0; i< externContour->total; i++) { CV_READ_SEQ_ELEM( actualPoint, reader); if( (actualPoint.x == blob.MinX()) && (actualPoint.y > result) ) { result = actualPoint.y; } } return result; }
void EyeTracker::drawSquares(CvSeq* squares) { CvSeqReader reader; int i; // initialize reader of the sequence cvStartReadSeq(squares, &reader, 0); CvPoint pt[4]; CvPoint* rect; // read 4 sequence elements at a time (all vertices of a square) for(i = 0; i < squares->total; i += 4) { rect = pt; int count = 4; // read 4 vertices CV_READ_SEQ_ELEM(pt[0], reader); CV_READ_SEQ_ELEM(pt[1], reader); CV_READ_SEQ_ELEM(pt[2], reader); CV_READ_SEQ_ELEM(pt[3], reader); cvPolyLine(graySceneImagePts, &rect, &count, 1, 1, CV_RGB(255, 255, 255), 3, CV_AA, 0); } CvFont font; cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX | CV_FONT_ITALIC, 1.0, 1.0, 0, 1); char s[20]; sprintf(s, "Threshold = %d", squareThreshold); cvPutText(graySceneImagePts, s, cvPoint(30, 30), &font, cvScalar(255, 255, 0)); }
int ChoiceTrackingFace3(CvFaceTracker* pTF, const int nElements, const CvFaceElement* big_face, CvTrackingRect* face, int& new_energy) { CvTrackingRect* curr_face[NUM_FACE_ELEMENTS] = {NULL}; CvTrackingRect* new_face[NUM_FACE_ELEMENTS] = {NULL}; new_energy = 0x7fffffff; int curr_energy = 0x7fffffff; int found = 0; int N = 0; CvSeqReader reader_m, reader_l, reader_r; cvStartReadSeq( big_face[MOUTH].m_seqRects, &reader_m ); for (int i_mouth = 0; i_mouth < big_face[MOUTH].m_seqRects->total && i_mouth < nElements; i_mouth++) { curr_face[MOUTH] = (CvTrackingRect*)(reader_m.ptr); cvStartReadSeq( big_face[LEYE].m_seqRects, &reader_l ); for (int i_left = 0; i_left < big_face[LEYE].m_seqRects->total && i_left < nElements; i_left++) { curr_face[LEYE] = (CvTrackingRect*)(reader_l.ptr); if (curr_face[LEYE]->r.y + curr_face[LEYE]->r.height < curr_face[MOUTH]->r.y) { cvStartReadSeq( big_face[REYE].m_seqRects, &reader_r ); for (int i_right = 0; i_right < big_face[REYE].m_seqRects->total && i_right < nElements; i_right++) { curr_face[REYE] = (CvTrackingRect*)(reader_r.ptr); if (curr_face[REYE]->r.y + curr_face[REYE]->r.height < curr_face[MOUTH]->r.y && curr_face[REYE]->r.x > curr_face[LEYE]->r.x + curr_face[LEYE]->r.width) { curr_energy = GetEnergy(curr_face, pTF->face, pTF->ptTempl, pTF->rTempl); if (curr_energy < new_energy) { for (int elem = 0; elem < NUM_FACE_ELEMENTS; elem++) new_face[elem] = curr_face[elem]; new_energy = curr_energy; found = 1; } N++; } } } } } if (found) { for (int elem = 0; elem < NUM_FACE_ELEMENTS; elem++) face[elem] = *(new_face[elem]); } return found; } // int ChoiceTrackingFace3(const CvTrackingRect* tr_face, CvTrackingRect* new_face, int& new_energy)
void flannFindPairs( const CvSeq*, const CvSeq* objectDescriptors, const CvSeq*, const CvSeq* imageDescriptors, vector<int>& ptpairs ) { int length = (int)(objectDescriptors->elem_size/sizeof(float)); cv::Mat m_object(objectDescriptors->total, length, CV_32F); cv::Mat m_image(imageDescriptors->total, length, CV_32F); // copy descriptors CvSeqReader obj_reader; float* obj_ptr = m_object.ptr<float>(0); cvStartReadSeq( objectDescriptors, &obj_reader ); for(int i = 0; i < objectDescriptors->total; i++ ) { const float* descriptor = (const float*)obj_reader.ptr; CV_NEXT_SEQ_ELEM( obj_reader.seq->elem_size, obj_reader ); memcpy(obj_ptr, descriptor, length*sizeof(float)); obj_ptr += length; } CvSeqReader img_reader; float* img_ptr = m_image.ptr<float>(0); cvStartReadSeq( imageDescriptors, &img_reader ); for(int i = 0; i < imageDescriptors->total; i++ ) { const float* descriptor = (const float*)img_reader.ptr; CV_NEXT_SEQ_ELEM( img_reader.seq->elem_size, img_reader ); memcpy(img_ptr, descriptor, length*sizeof(float)); img_ptr += length; } // find nearest neighbors using FLANN cv::Mat m_indices(objectDescriptors->total, 2, CV_32S); cv::Mat m_dists(objectDescriptors->total, 2, CV_32F); cv::flann::Index flann_index(m_image, cv::flann::KDTreeIndexParams(4)); // using 4 randomized kdtrees flann_index.knnSearch(m_object, m_indices, m_dists, 2, cv::flann::SearchParams(64) ); // maximum number of leafs checked int* indices_ptr = m_indices.ptr<int>(0); float* dists_ptr = m_dists.ptr<float>(0); for (int i=0;i<m_indices.rows;++i) { if (dists_ptr[2*i]<0.6*dists_ptr[2*i+1]) { ptpairs.push_back(i); ptpairs.push_back(indices_ptr[2*i]); } } }
CV_IMPL double cvContourPerimeter( CvSeq *contour, CvSlice slice ) { double perimeter = 0; int i, j = 0, count; const int N = 16; float buffer[N]; CvSeqReader reader; CV_FUNCNAME("cvCalcContourPerimeter"); __BEGIN__; if( !contour ) CV_ERROR_FROM_STATUS( CV_NULLPTR_ERR ); if( !CV_IS_SEQ_POLYLINE( contour )) CV_ERROR_FROM_STATUS( CV_BADFLAG_ERR ); if( contour->total > 1 ) { CvPoint pt1, pt2; cvStartReadSeq( contour, &reader, 0 ); cvSetSeqReaderPos( &reader, slice.startIndex ); count = icvSliceLength( slice, contour ); CV_ADJUST_EDGE_COUNT( count, contour ); /* scroll the reader by 1 point */ CV_READ_EDGE( pt1, pt2, reader ); for( i = 0; i < count; i++ ) { int dx, dy; int edge_length; CV_READ_EDGE( pt1, pt2, reader ); dx = pt2.x - pt1.x; dy = pt2.y - pt1.y; edge_length = dx * dx + dy * dy; buffer[j] = (float)edge_length; if( ++j == N || i == count - 1 ) { cvbSqrt( buffer, buffer, j ); for( ; j > 0; j-- ) perimeter += buffer[j-1]; } } } __CLEANUP__ __END__ return perimeter; }
void PlanarSubdivisionGetTriangles(CvSubdiv2D* subdiv, Triangle2DF* triangles, int* triangleCount, int includeVirtualPoint) { UniquePointSet pointSet; CvSet* subdivEdges = subdiv->edges; CvSeqReader reader; cvStartReadSeq((CvSeq*) subdivEdges, &reader); schar* start = reader.ptr; Triangle2DF* currentTriangle = triangles; Triangle2DF t; if (includeVirtualPoint) { while(CV_IS_SET_ELEM(reader.ptr)) { CvQuadEdge2D* edge = (CvQuadEdge2D*)reader.ptr; PlanarSubdivisionEdgeToTriangle( edge->next[0], &t); if (pointSet.insert(TriangleVertexSum(&t)).second) *currentTriangle++ = t; PlanarSubdivisionEdgeToTriangle( edge->next[2], &t); if (pointSet.insert(TriangleVertexSum(&t)).second) *currentTriangle++ = t; CV_NEXT_SEQ_ELEM(subdivEdges->elem_size, reader); // prevent infinite loop if(reader.ptr == start) break; } } else { while(CV_IS_SET_ELEM(reader.ptr)) { CvQuadEdge2D* edge = (CvQuadEdge2D*)reader.ptr; PlanarSubdivisionEdgeToTriangle( edge->next[0], &t); if (pointSet.insert(TriangleVertexSum(&t)).second && TriangleInRegion(t, subdiv)) *currentTriangle++ = t; PlanarSubdivisionEdgeToTriangle( edge->next[2], &t); if (pointSet.insert(TriangleVertexSum(&t)).second && TriangleInRegion(t, subdiv)) *currentTriangle++ = t; CV_NEXT_SEQ_ELEM(subdivEdges->elem_size, reader); // prevent infinite loop if(reader.ptr == start) break; } } *triangleCount = (currentTriangle - triangles) ; }
/** - FUNCTION: FillBlob - FUNCTIONALITY: - Fills the blob with a specified colour - PARAMETERS: - imatge: where to paint - color: colour to paint the blob - RESULT: - modifies input image and returns the seed point used to fill the blob - RESTRICTIONS: - AUTHOR: Ricard Borr� - CREATION DATE: 25-05-2005. - MODIFICATION: Date. Author. Description. */ void CBlob::FillBlob( IplImage *imatge, CvScalar color, int offsetX /*=0*/, int offsetY /*=0*/) const { //verifiquem que existeixi el blob i que tingui cantonades if( edges == NULL || edges->total == 0 ) return; CvPoint edgeactual, pt1, pt2; CvSeqReader reader; vectorPunts vectorEdges = vectorPunts( edges->total ); vectorPunts::iterator itEdges, itEdgesSeguent; bool dinsBlob; int yActual; // passem els punts del blob a un vector de punts de les STL cvStartReadSeq( edges, &reader); itEdges = vectorEdges.begin(); while( itEdges != vectorEdges.end() ) { CV_READ_SEQ_ELEM( edgeactual ,reader); *itEdges = edgeactual; itEdges++; } // ordenem el vector per les Y's i les X's d'esquerra a dreta std::sort( vectorEdges.begin(), vectorEdges.end(), comparaCvPoint() ); // recorrem el vector ordenat i fem linies entre punts consecutius itEdges = vectorEdges.begin(); itEdgesSeguent = vectorEdges.begin() + 1; dinsBlob = true; while( itEdges != (vectorEdges.end() - 1)) { yActual = (*itEdges).y; if( ( (*itEdges).x != (*itEdgesSeguent).x ) && ( (*itEdgesSeguent).y == yActual ) ) { if( dinsBlob ) { pt1 = *itEdges; pt1.x += offsetX; pt1.y += offsetY; pt2 = *itEdgesSeguent; pt2.x += offsetX; pt2.y += offsetY; cvLine( imatge, pt1, pt2, color ); } dinsBlob =! dinsBlob; } itEdges++; itEdgesSeguent++; if( (*itEdges).y != yActual ) dinsBlob = true; } vectorEdges.clear(); }
int _cvConstructLCM(CvLCM* LCM) { CvVoronoiSite2D* pSite = 0; CvVoronoiEdge2D* pEdge = 0, *pEdge1; CvVoronoiNode2D* pNode, *pNode1; CvVoronoiEdge2D* LinkedEdges[10]; CvVoronoiSite2D* LinkedSites[10]; CvSeqReader reader; CvLCMData LCMdata; int i; for(CvSet* SiteSet = LCM->VoronoiDiagram->sites; SiteSet != NULL; SiteSet = (CvSet*)SiteSet->h_next) { cvStartReadSeq((CvSeq*)SiteSet, &reader); for(i = 0; i < SiteSet->total; i++) { _CV_READ_SEQ_ELEM(pSite,reader,CvVoronoiSite2D*); if(pSite->node[0] == pSite->node[1]) continue; pEdge = CV_LAST_VORONOIEDGE2D(pSite); pNode = CV_VORONOIEDGE2D_BEGINNODE(pEdge,pSite); if(pNode->radius > LCM->maxWidth) goto PREPARECOMPLEXNODE; pEdge1 = CV_PREV_VORONOIEDGE2D(pEdge,pSite); pNode1 = CV_VORONOIEDGE2D_BEGINNODE(pEdge1,pSite); if(pNode1->radius > LCM->maxWidth) goto PREPARECOMPLEXNODE; if(pNode1->radius == 0) continue; if(_cvNodeMultyplicity(pSite, pEdge,pNode,LinkedEdges,LinkedSites) == 1) goto PREPARESIMPLENODE; } // treate triangle or regular polygon _CV_INITIALIZE_CVLCMDATA(&LCMdata,pSite,pEdge,CV_VORONOIEDGE2D_ENDNODE(pEdge,pSite)); if(!_cvTreatExeptionalCase(LCM,&LCMdata)) return 0; continue; PREPARECOMPLEXNODE: _CV_INITIALIZE_CVLCMDATA(&LCMdata,pSite,pEdge,CV_VORONOIEDGE2D_ENDNODE(pEdge,pSite)); if(!_cvConstructLCMComplexNode(LCM,NULL,&LCMdata)) return 0; continue; PREPARESIMPLENODE: _CV_INITIALIZE_CVLCMDATA(&LCMdata,pSite,pEdge,CV_VORONOIEDGE2D_ENDNODE(pEdge,pSite)); if(!_cvConstructLCMSimpleNode(LCM,NULL,&LCMdata)) return 0; continue; } return 1; }//end of _cvConstructLCM