void FaceDetection::AddContours2Rect(CvSeq *seq, int color, int iLayer)
{
    assert(m_mstgRects != NULL);
    assert(m_seqRects != NULL);

    CvContourRect cr;
    for (CvSeq* external = seq; external; external = external->h_next)
    {
        cr.r = cvContourBoundingRect(external, 1 );
        cr.pCenter.x = cr.r.x + cr.r.width / 2;
        cr.pCenter.y = cr.r.y + cr.r.height / 2;
        cr.iNumber = iLayer;
        cr.iType = 6;
        cr.iFlags = 0;
        cr.seqContour = external;
        cr.iContourLength = external->total;
        cr.iColor = color;
        cvSeqPush(m_seqRects, &cr);
        for (CvSeq* internal = external->v_next; internal; internal = internal->h_next)
        {
            cr.r = cvContourBoundingRect(internal, 0);    
            cr.pCenter.x = cr.r.x + cr.r.width / 2;
            cr.pCenter.y = cr.r.y + cr.r.height / 2;
            cr.iNumber = iLayer;
            cr.iType = 12;
            cr.iFlags = 0;
            cr.seqContour = internal;
            cr.iContourLength = internal->total;
            cr.iColor = color;
            cvSeqPush(m_seqRects, &cr);
        }
    }
}// void FaceDetection::AddContours2Rect(CvSeq *seq, int color, int iLayer)
예제 #2
0
// the function draws all the squares in the image
void drawSquares(IplImage* imgSrc, CvSeq* squares)
{
	CvSeqReader reader;
	IplImage* imgCopy = cvCloneImage(imgSrc);
	int i;

	// initialize reader of the sequence
	cvStartReadSeq(squares, &reader, 0);

	// read 4 sequence elements at a time (all vertices of a square)
	printf("Found %d rectangles in image\n", squares->total / 4);

	for (i = 0; i < squares->total; i += 4)
	{
		CvPoint* pntRect = gPnt;
		int pntCount = 4;
		CvSeq* seqRect = cvCreateSeq(CV_32SC2, sizeof(CvSeq), sizeof(CvPoint), gStorage);

		// read 4 vertices
		memcpy(gPnt, reader.ptr, squares->elem_size);
		CV_NEXT_SEQ_ELEM(squares->elem_size, reader);
		cvSeqPush(seqRect, &pntRect[0]);

		memcpy(gPnt + 1, reader.ptr, squares->elem_size);
		CV_NEXT_SEQ_ELEM(squares->elem_size, reader);
		cvSeqPush(seqRect, &pntRect[1]);

		memcpy(gPnt + 2, reader.ptr, squares->elem_size);
		CV_NEXT_SEQ_ELEM(squares->elem_size, reader);
		cvSeqPush(seqRect, &pntRect[2]);

		memcpy(gPnt + 3, reader.ptr, squares->elem_size);
		CV_NEXT_SEQ_ELEM(squares->elem_size, reader);
		cvSeqPush(seqRect, &pntRect[3]);

		// draw the square as a closed polyline
		cvPolyLine(imgCopy, &pntRect, &pntCount, 1, 1, CV_RGB(0, 255, 0), 1, CV_AA, 0);

		// draw the min outter rect
		CvBox2D box = cvMinAreaRect2(seqRect, NULL);
	    CvPoint2D32f ptBox[4];
	    cvBoxPoints(box, ptBox);
	    for(int i = 0; i < 4; ++i) {
	        cvLine(imgCopy, cvPointFrom32f(ptBox[i]), cvPointFrom32f(ptBox[((i+1)%4)?(i+1):0]), CV_RGB(255,0,0));
	    }

	}

	// show the resultant image
	cvShowImage(wndname, imgCopy);
	cvReleaseImage(&imgCopy);
}
void FaceDetection::CreateResults(CvSeq * lpSeq)
{
    
    Face * tmp;
    
    double Max  = 0;
    double CurStat = 0;
    
    FaceData tmpData;
    if (m_bBoosting)
    {
        tmp = m_pFaceList->GetData();
        tmp->CreateFace(&tmpData);

        CvFace tmpFace;
        tmpFace.MouthRect = tmpData.MouthRect;
        tmpFace.LeftEyeRect = tmpData.LeftEyeRect;
        tmpFace.RightEyeRect = tmpData.RightEyeRect;

        cvSeqPush(lpSeq,&tmpFace);

    }else
    {
        while ( (tmp = m_pFaceList->GetData()) != 0 )
        {
            CurStat = tmp->GetWeight();
            if (CurStat > Max)
                Max = CurStat;
        }
        
        while ( (tmp = m_pFaceList->GetData()) != 0 )
        {
            tmp->CreateFace(&tmpData);
            CurStat = tmp->GetWeight();
            
            if (CurStat == Max)
            {
                CvFace tmpFace;
                tmpFace.MouthRect = tmpData.MouthRect;
                tmpFace.LeftEyeRect = tmpData.LeftEyeRect;
                tmpFace.RightEyeRect = tmpData.RightEyeRect;
                cvSeqPush(lpSeq,&tmpFace);

                
            }
        }
    }
}// void FaceDetection::DrawResult(IplImage* img)
예제 #4
0
void CalcFourierDescriptorCoeff(CvSeq* seq_pts, int n_fourier,CvSeq* seq_fourier)
{
	int count = seq_pts->total;
	double *coeff_cos, *coeff_sin;
	coeff_cos = (double*)malloc(count*sizeof(double));
	coeff_sin = (double*)malloc(count*sizeof(double));
	int i;
	for(i = 0; i < count; i++)
	{
		coeff_sin[i] = sin(2*i*CV_PI/count);
		coeff_cos[i] = cos(2*i*CV_PI/count);
	}

	cvClearSeq(seq_fourier);
	for(int u = 0; u < n_fourier; u++)
	{
		CvPoint2D32f point_coeff = cvPoint2D32f(0, 0);
		for(i = 0; i < count; i+=4)
		{
			CvPoint* pt = (CvPoint*)cvGetSeqElem(seq_pts, i);
			point_coeff.x += (float)(pt->x*coeff_cos[(i*u)%count] + pt->y*coeff_sin[(i*u)%count]);
			point_coeff.y += (float)(pt->y*coeff_cos[(i*u)%count] - pt->x*coeff_sin[(i*u)%count]);
		}
		//point_coeff.x/=count;
		//point_coeff.y/=count;
		cvSeqPush(seq_fourier, &point_coeff);
	} 
	free(coeff_cos);
	free(coeff_sin);
}
예제 #5
0
void CalcBoundary(CvSeq* seq_fourier, int n_Pts, CvSeq* seq_pts)
{
	int count = seq_fourier->total;
	double *coeff_cos, *coeff_sin;
	coeff_cos = (double*)malloc(n_Pts*sizeof(double));
	coeff_sin = (double*)malloc(n_Pts*sizeof(double));
	int i;
	for(i = 0; i < n_Pts; i++)
	{
		coeff_sin[i] = sin(2*i*CV_PI/n_Pts);
		coeff_cos[i] = cos(2*i*CV_PI/n_Pts);
	}
	cvClearSeq(seq_pts);
	for(i = 0; i < n_Pts; i++)
	{
		CvPoint pt = cvPoint(0, 0);
		double sumx = 0, sumy = 0;
		for(int u = 0; u < count; u++)
		{
			CvPoint2D32f* point_coeff = (CvPoint2D32f*)cvGetSeqElem(seq_fourier, u);
			sumx += point_coeff->x*coeff_cos[(i*u)%n_Pts] - point_coeff->y*coeff_sin[(i*u)%n_Pts];
			sumy += point_coeff->y*coeff_cos[(i*u)%n_Pts] + point_coeff->x*coeff_sin[(i*u)%n_Pts];
		}
		pt.x = cvRound(sumx/count);
		pt.y = cvRound(sumy/count);
		cvSeqPush(seq_pts, &pt);
	}
	free(coeff_cos);
	free(coeff_sin);
}
예제 #6
0
VALUE
rb_seq_push(VALUE self, VALUE args, int flag)
{
  CvSeq *seq = CVSEQ(self);
  VALUE klass = seqblock_class(seq), object;
  void *buffer = NULL;
  for (int i = 0; i < RARRAY_LEN(args); i++) {
    object = RARRAY_PTR(args)[i];
    if (CLASS_OF(object) == klass) {
      if (flag == CV_FRONT)
	cvSeqPushFront(seq, DATA_PTR(object));
      else
	cvSeqPush(seq, DATA_PTR(object));
    }
    else if (rb_obj_is_kind_of(object, rb_klass) && CLASS_OF(rb_first(object)) == klass) { // object is CvSeq
      buffer = cvCvtSeqToArray(CVSEQ(object), cvAlloc(CVSEQ(object)->total * CVSEQ(object)->elem_size));
      cvSeqPushMulti(seq, buffer, CVSEQ(object)->total, flag);
      cvFree(&buffer);
    }
    else {
      rb_raise(rb_eTypeError, "arguments should be %s or %s which includes %s.",
	       rb_class2name(klass), rb_class2name(rb_klass), rb_class2name(klass));
    }
  }
  return self;
}
예제 #7
0
CvSeq *reghand::filthull2(CvSeq *filted_elimhull)
{
    //CvSeq *filtedhullseq=cvCloneSeq(filted_elimhull);
    float maxdis=0;CvPoint **fingpt;CvScalar mean,std=cvScalarAll(0);
    CvMat *dismat=cvCreateMat(1,filted_elimhull->total,CV_32FC1);
    CvPoint2D32f center=minrect.center;
    for (int i=0;i<filted_elimhull->total;i++)
    {
        CvPoint **data=CV_GET_SEQ_ELEM(CvPoint*,filted_elimhull,i);
        CvPoint pt=**data;
        float dis=sqrt(pow(pt.x-center.x,2)+pow(pt.y-center.y,2));
        dismat->data.fl[i]=dis;
        if(dis>maxdis){maxdis=dis;fingpt=data;}
    }
    cvAvgSdv(dismat,&mean,&std);
    if(filted_elimhull->total==1&&maxdis>fingerTh*0.5) return filted_elimhull;
    if(filted_elimhull->total==2&&maxdis>fingerTh*0.5&&std.val[0]<10)
    {
        CvPoint startpt=**CV_GET_SEQ_ELEM(CvPoint*,filted_elimhull,0);
        CvPoint endpt=**CV_GET_SEQ_ELEM(CvPoint*,filted_elimhull,1);;
        double bfang=atan(double(startpt.y-handcenter.y)/(startpt.x-handcenter.x))*180/PI;
        if(bfang<0)bfang+=180;
        double afang=atan(double(endpt.y-handcenter.y)/(endpt.x-handcenter.x))*180/PI;
        if(afang<0)afang+=180;
        if(fabs(bfang-afang)>60)
        {cvClearSeq(filted_elimhull);cvSeqPush(filted_elimhull,fingpt);return filted_elimhull;}
        else	return filted_elimhull;
    }
예제 #8
0
bool CybCamCalibration::generateDataToCalibration(IplImage *rgb_image) {

    IplImage *view_gray;
    int found = 0, count = 0;

    img_size = cvGetSize(rgb_image);

    found = cvFindChessboardCorners( rgb_image, board_size,
                                     image_points_buf, &count, CV_CALIB_CB_ADAPTIVE_THRESH );

    // improve the found corners' coordinate accuracy
    view_gray = cvCreateImage( cvGetSize(rgb_image), 8, 1 );
    cvCvtColor( rgb_image, view_gray, CV_BGR2GRAY );

    cvFindCornerSubPix( view_gray, image_points_buf, count, cvSize(11,11),
                        cvSize(-1,-1), cvTermCriteria( CV_TERMCRIT_EPS+CV_TERMCRIT_ITER, 30, 0.1 ));

    cvReleaseImage( &view_gray );

    if( found )
    {
        cvSeqPush( image_points_seq, image_points_buf );

        sprintf( imagename, "image%03d.png", image_points_seq->total - 1 );
        cvSaveImage( imagename, rgb_image );
    }

    cvDrawChessboardCorners( rgb_image, board_size, image_points_buf, count, found );

    return ((unsigned)image_points_seq->total >= (unsigned)image_count);
}
예제 #9
0
void  EdgeDetection::getEdge(CvSeq *seq)
{
	
	int nWidth	= pImage->width;
	int nHeight = pImage->height;
	int widthStep=pImage->widthStep;

	//计算直方图
	int histab[256];
	for(int i=0;i<256;i++)
		histab[i]=0;

	for(int j=1;j<nHeight-1;j++)
	{
		for(int i=1;i<nWidth-1;i++)
		{
			double temp=(pImage->imageData+nWidth*j)[i];
			if(temp<0)
				temp+=256;

			histab[(int)temp]++;
		}
	}
	for(int i=0;i<256;i++)
		probability[i]=(float)(histab[i]/((nHeight-2)*(nWidth-2)*1.0));
	double edgethreadhold=moment_shresh_sel1(probability);//计算边缘二值化的分割阈值
	for(int j=0;j<nHeight;j++)//将sobel检测出来的边缘进行二值化,滤去值过小的边缘点
	{
		for(int i=0;i<nWidth;i++)
		{
			if(i<1||j<1||j==nHeight-1||i==nWidth-1)
			{
				(pImage->imageData+nWidth*j)[i]=0;
			}
			else
			{
				double temp=(pImage->imageData+nWidth*j)[i];
				if(temp<0)
					temp+=256;

				if(temp>edgethreadhold)
				{
					(pImage->imageData+nWidth*j)[i]=255;
					
					CvPoint2D32f sample;
					sample.x=j;
					sample.y=i;
					cvSeqPush(seq,&sample);

				}
				else
					(pImage->imageData+nWidth*j)[i]=0;
			}
		}
	}

	cvNamedWindow( "Segment2_边缘图", 1 );//创建窗口
	cvShowImage( "Segment2_边缘图", pImage );//显示图像
	
}
예제 #10
0
void bContourFinder::findConvexHulls(){
  CvMemStorage *stor = cvCreateMemStorage(); 
  CvSeq * ptseq = cvCreateSeq( CV_SEQ_KIND_CURVE|CV_32SC2,
                                    sizeof(CvContour),
                                    sizeof(CvPoint),
                                    stor );
  CvSeq * hull; 
  CvPoint pt; 
  this->convexBlobs.clear();
  for(int i = 0 ; i < this->blobs.size(); i++){
    this->convexBlobs.push_back(ofxCvBlob());
    this->convexBlobs[i] = this->blobs[i];
    this->convexBlobs[i].pts.clear(); 
    // get blob i
    for(int j = 0; j < this->blobs[i].pts.size(); j++){
      // fill in blob points 
      pt.x = this->blobs[i].pts[j].x; 
      pt.y = this->blobs[i].pts[j].y; 
      cvSeqPush( ptseq, &pt); 
    }
  
    hull = cvConvexHull2(ptseq, 0, CV_CLOCKWISE, 0); 
   
    // get the points for the blob:
    CvPoint           pt  = **CV_GET_SEQ_ELEM( CvPoint*, hull, hull->total - 1 );
    for( int j=0; j < hull->total; j++ ) {
      pt = **CV_GET_SEQ_ELEM( CvPoint*, hull, j );
      convexBlobs[i].pts.push_back( ofPoint((float)pt.x, (float)pt.y) );
    }
    convexBlobs[i].nPts = convexBlobs[i].pts.size();
  }
  cvClearMemStorage( stor ); 
}
예제 #11
0
int
main (int argc, char **argv)
{
    CvMemStorage *storage = cvCreateMemStorage( 0 );
    CvSeq *points = cvCreateSeq( CV_SEQ_ELTYPE_POINT, sizeof( CvSeq ), sizeof( CvPoint ), storage );
    cvSeqPush( points, &cvPoint( 100, 50 ) );
    cvSeqPush( points, &cvPoint( 50, 100 ) );
    cvSeqPush( points, &cvPoint( 50, 150 ) );
    cvSeqPush( points, &cvPoint( 150, 50 ) );

    CvRect rect = cvBoundingRect( points );
    cvPrintRect( rect );

    cvReleaseMemStorage( &storage );
    return 0;
}
예제 #12
0
CV_IMPL CvSeq*
cvSegmentMotion( const CvArr* mhimg, CvArr* segmaskimg, CvMemStorage* storage,
                 double timestamp, double segThresh )
{
    cv::Mat mhi = cv::cvarrToMat(mhimg);
    const cv::Mat segmask = cv::cvarrToMat(segmaskimg);
    std::vector<cv::Rect> brs;
    cv::segmentMotion(mhi, segmask, brs, timestamp, segThresh);
    CvSeq* seq = cvCreateSeq(0, sizeof(CvSeq), sizeof(CvConnectedComp), storage);

    CvConnectedComp comp;
    memset(&comp, 0, sizeof(comp));
    for( size_t i = 0; i < brs.size(); i++ )
    {
        cv::Rect roi = brs[i];
        float compLabel = (float)(i+1);
        int x, y, area = 0;

        cv::Mat part = segmask(roi);
        for( y = 0; y < roi.height; y++ )
        {
            const float* partptr = part.ptr<float>(y);
            for( x = 0; x < roi.width; x++ )
                area += partptr[x] == compLabel;
        }

        comp.value = cv::Scalar(compLabel);
        comp.rect = roi;
        comp.area = area;
        cvSeqPush(seq, &comp);
    }

    return seq;
}
예제 #13
0
파일: sift.c 프로젝트: joa-quim/mirone
/*
Detects features at extrema in DoG scale space.  Bad features are discarded
based on contrast and ratio of principal curvatures.

@param dog_pyr DoG scale space pyramid
@param octvs octaves of scale space represented by dog_pyr
@param intvls intervals per octave
@param contr_thr low threshold on feature contrast
@param curv_thr high threshold on feature ratio of principal curvatures
@param storage memory storage in which to store detected features

@return Returns an array of detected features whose scales, orientations,
	and descriptors are yet to be determined.
*/
CvSeq* scale_space_extrema( IplImage*** dog_pyr, int octvs, int intvls,
			   double contr_thr, int curv_thr, CvMemStorage* storage ) {
	CvSeq* features;
	double prelim_contr_thr = 0.5 * contr_thr / intvls;
	struct feature* feat;
	struct detection_data* ddata;
	int o, i, r, c;

	features = cvCreateSeq( 0, sizeof(CvSeq), sizeof(struct feature), storage );
	for( o = 0; o < octvs; o++ )
		for( i = 1; i <= intvls; i++ )
			for(r = SIFT_IMG_BORDER; r < dog_pyr[o][0]->height-SIFT_IMG_BORDER; r++)
				for(c = SIFT_IMG_BORDER; c < dog_pyr[o][0]->width-SIFT_IMG_BORDER; c++)
					/* perform preliminary check on contrast */
					if( ABS( pixval32f( dog_pyr[o][i], r, c ) ) > prelim_contr_thr )
						if( is_extremum( dog_pyr, o, i, r, c ) ) {
							feat = interp_extremum(dog_pyr, o, i, r, c, intvls, contr_thr);
							if( feat ) {
								ddata = feat_detection_data( feat );
								if( ! is_too_edge_like( dog_pyr[ddata->octv][ddata->intvl],
									ddata->r, ddata->c, curv_thr ) ) {
									cvSeqPush( features, feat );
								}
								else
									free( ddata );
								free( feat );
							}
						}

	return features;
}
// 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;
}
예제 #15
0
void plot_scan_image(IplImage* image, Two_dimensional_vector* scan_image)
{
    CvSeq *points;
    CvPoint pt;
    CvMemStorage *storage = cvCreateMemStorage (0);

    points = cvCreateSeq (CV_SEQ_ELTYPE_POINT, sizeof (CvSeq), sizeof (CvPoint), storage);

    for (int i = 0; i < (int)scan_image->x.size(); i++) {
        if(0 > scan_image->x[i] || scan_image->x[i] > 639) {
            continue;
        }
        if(0 > scan_image->y[i] || scan_image->y[i] > 479) {
            continue;
        }
        pt.x = scan_image->x[i];
        pt.y = scan_image->y[i];

        cvSeqPush (points, &pt);
        cvCircle(image, pt, 2, CV_RGB (0, 255, 0), CV_FILLED, 8, 0);
    }

    cvClearSeq(points);
    cvReleaseMemStorage(&storage);
}
예제 #16
0
/*
Adds features to an array for every orientation in a histogram greater than a specified threshold.
@param features new features are added to the end of this array
@param hist orientation histogram
@param n number of bins in hist
@param mag_thr new features are added for entries in hist greater than this
@param feat new features are clones of this with different orientations
*/
static void add_good_ori_features( CvSeq* features, double* hist, int n,
								   double mag_thr, struct feature* feat )
{
	struct feature* new_feat;
	double bin, PI2 = CV_PI * 2.0;
	int l, r, i;

    //遍历直方图
	for( i = 0; i < n; i++ )
	{
        l = ( i == 0 )? n - 1 : i-1;//前一个(左边的)bin的下标
        r = ( i + 1 ) % n;//后一个(右边的)bin的下标

        //若当前的bin是局部极值(比前一个和后一个bin都大),并且值大于给定的幅值阈值,则新生成一个特征点并添加到特征点序列末尾
		if( hist[i] > hist[l]  &&  hist[i] > hist[r]  &&  hist[i] >= mag_thr )
		{
            //根据左、中、右三个bin的值对当前bin进行直方图插值
			bin = i + interp_hist_peak( hist[l], hist[i], hist[r] );
            bin = ( bin < 0 )? n + bin : ( bin >= n )? bin - n : bin;//将插值结果规范到[0,n]内
            new_feat = clone_feature( feat );//克隆当前特征点为新特征点
            new_feat->ori = ( ( PI2 * bin ) / n ) - CV_PI;//新特征点的方向
            cvSeqPush( features, new_feat );//插入到特征点序列末尾
			free( new_feat );
		}
	}
}
예제 #17
0
파일: blobtrack.cpp 프로젝트: 93sam/opencv
void CvBlobTrackSeq::AddBlobTrack(int TrackID, int StartFrame)
{
    CvBlobTrack N;
    N.TrackID = TrackID;
    N.StartFrame = StartFrame;
    N.pBlobSeq = new CvBlobSeq;
    cvSeqPush(m_pSeq,&N);
}
예제 #18
0
/*
 * This function generates an illumination montage containing a square in worm space
 * at a location specified.
 *
 * This function is most useful when it takes input from a slider bar.
 *
 * In that manner the user can specify a rectangular (in worm space) region of illumination
 * at run time.
 *
 */
int GenerateSimpleIllumMontage(CvSeq* montage, CvPoint origin, CvSize radius, CvSize gridSize){
	/** If the square has no length or width, then  there is nothing to do **/
	if (radius.width == 0 || radius.height ==  0) return 0;

	/** If the y value extends off the worm, we need to crop it.. (otherwise it wraps... weird!)**/



	cvClearSeq(montage);

	WormPolygon* polygon=CreateWormPolygon(montage->storage,gridSize);


	CvPoint curr;

	/** Lower Left **/
	curr=cvPoint(origin.x-radius.width, CropNumber(1,gridSize.height-1,origin.y-radius.height));
	cvSeqPush(polygon->Points,&curr);

	/** Lower Right **/
	curr=cvPoint(origin.x+radius.width, CropNumber(1,gridSize.height-1,origin.y-radius.height));
	cvSeqPush(polygon->Points,&curr);

	/** Upper Right **/
	curr=cvPoint(origin.x+radius.width, CropNumber(1,gridSize.height-1,origin.y+radius.height));
	cvSeqPush(polygon->Points,&curr);


	/** Upper Left **/
	curr=cvPoint(origin.x-radius.width, CropNumber(1,gridSize.height-1,origin.y+radius.height));
	cvSeqPush(polygon->Points,&curr);


	/** Push this sparse polygon onto the temp montage **/
	CvSeq* tempMontage= CreateIlluminationMontage(montage->storage);
	cvSeqPush(tempMontage,&polygon);

	/** Convert the sparse polygon into a contour-polygon **/
	CvtPolyMontage2ContourMontage(tempMontage,montage);
	cvClearSeq(tempMontage);

	return 1;


}
/*!
    \fn CvBinGabAdaFeatureSelect::loadweaks(const char* filename)
 */
void CvBinGabAdaFeatureSelect::loadweaks(const char* filename)
{
  //clear();
  {
    delete new_pool;
    weaks.clear();
  }
  CvMemStorage* fstorage = cvCreateMemStorage( 0 );
  CvFileStorage *fs;
  fs = cvOpenFileStorage( filename, fstorage, CV_STORAGE_READ );
  CvFileNode *root = cvGetRootFileNode( fs, 0);
  char *weakname = new char[20];
  int i = 0;
  
  CvMemStorage* storage = cvCreateMemStorage(0);
  CvSeq* seq = cvCreateSeq(0, sizeof(CvSeq), sizeof(CvGaborTree), storage );
  while(1)
  {
    sprintf( weakname, "weak_%d", i);
    CvFileNode *weaknode = cvGetFileNodeByName( fs, root, weakname);
    if (!weaknode) break;
    CvGaborTree tree;
    weaknode2tree(weaknode, fs, &tree);
    cvSeqPush( seq, &tree );
    i++;	
  }
  
    /* from squence to vector weaks*/
  new_pool = new CvGaborFeaturePool;
  for (int i = 0; i <seq->total; i++)
  {
    CvGaborTree *atree = (CvGaborTree*)cvGetSeqElem(seq, i);
    CvWeakLearner *weak = new CvWeakLearner;
    weak->setType( weaklearner_type );
    weak->setthreshold(atree->threshold);
    weak->setparity(atree->parity);
    weaks.push_back(*weak);
    CvGaborFeature *feature = new CvGaborFeature(atree->x, atree->y, atree->Mu, atree->Nu);
    new_pool->add(feature);
    alphas.push_back(atree->alpha);
    delete weak;
    delete feature;
  } 
  
  cvReleaseMemStorage( &storage );
  cvReleaseFileStorage(&fs);
  cvReleaseMemStorage( &fstorage );
  delete [] weakname;
  
    /* set member variables */
  current = new_pool->getSize();
  falsepositive = 0.0;
  nexpfeatures = new_pool->getSize();
  nselecfeatures = new_pool->getSize();
  printf(" %d weak classifiers have been loaded!\n", nselecfeatures);
}
예제 #20
0
CvLCMNode* _cvTreatExeptionalCase(CvLCM* pLCM,
                                  CvLCMData* pLCMInputData)
{
    CvVoronoiEdge2D* pEdge = pLCMInputData->pedge;
    CvVoronoiSite2D* pSite = pLCMInputData->psite;
    CvVoronoiNode2D* pNode = CV_VORONOIEDGE2D_BEGINNODE(pEdge,pSite);
    CvLCMNode* pLCMNode = _cvCreateLCMNode(pLCM);
    cvSeqPush((CvSeq*)pLCMNode->contour,&pNode->pt);
    return pLCMNode;
}//end of _cvConstructLCMEdge
예제 #21
0
void getconvexhull()
{
  hull = cvConvexHull2( contours, 0, CV_CLOCKWISE, 0 );
  pt0 = **CV_GET_SEQ_ELEM( CvPoint*, hull, hull->total - 1 );
        
  for(int i = 0; i < hull->total; i++ )
  {
    pt = **CV_GET_SEQ_ELEM( CvPoint*, hull, i );
    //printf("%d,%d\n",pt.x,pt.y);
    cvLine( frame, pt0, pt, CV_RGB( 128, 128, 128 ),2,8,0);
    pt0 = pt;
  }

  defect = cvConvexityDefects(contours,hull,defectstorage); //��M�ʳ� 

  for(int i=0;i<defect->total;i++)
  {
    CvConvexityDefect* d=(CvConvexityDefect*)cvGetSeqElem(defect,i);

    // if(d->depth < 50)
    // {
    //  p.x = d->start->x;
    //  p.y = d->start->y;
    //  cvCircle(frame,p,5,CV_RGB(255,255,255),-1,CV_AA,0);
    //  p.x = d->end->x;
    //  p.y = d->end->y;
    //  cvCircle(frame,p,5,CV_RGB(255,255,255),-1,CV_AA,0);
    //  }
    if(d->depth > 10)  
    {
      p.x = d->depth_point->x;
      p.y = d->depth_point->y;
      cvCircle(frame,p,5,CV_RGB(255,255,0),-1,CV_AA,0);
      cvSeqPush(palm,&p);
    }

  }

  //if(palm->total>1)
  //{
  //  cvMinEnclosingCircle(palm,&mincirclecenter,&radius);
  //  cvRound(radius);
  //  mincirclecenter2.x = cvRound(mincirclecenter.x);
  //  mincirclecenter2.y = cvRound(mincirclecenter.y);
  //  cvCircle(frame,mincirclecenter2,cvRound(radius),CV_RGB(255,128,255),4,8,0);
  //  cvCircle(frame,mincirclecenter2,10,CV_RGB(255,128,255),4,8,0);
  //  palmcenter =  cvMinAreaRect2(palm,0);
  //  center.x = cvRound(palmcenter.center.x);
  //  center.y = cvRound(palmcenter.center.y);
  //  cvEllipseBox(frame,palmcenter,CV_RGB(128,128,255),2,CV_AA,0);
  //  cvCircle(frame,center,10,CV_RGB(128,128,255),-1,8,0);
  //}

}
예제 #22
0
파일: gbt.cpp 프로젝트: Rocky030/opencv
void CvGBTrees::read( CvFileStorage* fs, CvFileNode* node )
{

    CV_FUNCNAME( "CvGBTrees::read" );

    __BEGIN__;

    CvSeqReader reader;
    CvFileNode* trees_fnode;
    CvMemStorage* storage;
    int i, ntrees;
    cv::String s;

    clear();
    read_params( fs, node );

    if( !data )
        EXIT;

    base_value = (float)cvReadRealByName( fs, node, "base_value", 0.0 );
    class_count = cvReadIntByName( fs, node, "class_count", 1 );

    weak = new pCvSeq[class_count];


    for (int j=0; j<class_count; ++j)
    {
        s = cv::format("trees_%d", j);

        trees_fnode = cvGetFileNodeByName( fs, node, s.c_str() );
        if( !trees_fnode || !CV_NODE_IS_SEQ(trees_fnode->tag) )
            CV_ERROR( CV_StsParseError, "<trees_x> tag is missing" );

        cvStartReadSeq( trees_fnode->data.seq, &reader );
        ntrees = trees_fnode->data.seq->total;

        if( ntrees != params.weak_count )
            CV_ERROR( CV_StsUnmatchedSizes,
            "The number of trees stored does not match <ntrees> tag value" );

        CV_CALL( storage = cvCreateMemStorage() );
        weak[j] = cvCreateSeq( 0, sizeof(CvSeq), sizeof(CvDTree*), storage );

        for( i = 0; i < ntrees; i++ )
        {
            CvDTree* tree = new CvDTree();
            CV_CALL(tree->read( fs, (CvFileNode*)reader.ptr, data ));
            CV_NEXT_SEQ_ELEM( reader.seq->elem_size, reader );
            cvSeqPush( weak[j], &tree );
        }
    }

    __END__;
}
예제 #23
0
void CPolygonalRegion::AddPoint(double x, double y) {

  auto Pos = cvPoint((int)x,(int)y);
  cvSeqPush(_pPointsOuter, &Pos);
//  cvSeqPush(_pPointsOuter, &cvPoint2D32f(x,y)); //なおすならcvGetSeqElemも.

  _dXMin = std::min(x, _dXMin);
  _dXMax = std::max(x, _dXMax);
  _dYMin = std::min(y, _dYMin);
  _dYMax = std::max(y, _dYMax);

}
예제 #24
0
int main(int argc, char** argv)
{
    const char* filename = argv[1];
    int x;
    int y;
    int size;
    sscanf(argv[2], "%d", &x);
    sscanf(argv[3], "%d", &y);
    sscanf(argv[4], "%d", &size);
    
    int SURF_EXTENDED = 0;
    int SURF_HESSIAN_THRESHOLD = 500;
    int SURF_NOCTAVES = 2;
    int SURF_NOCTAVELAYERS = 2; 
    int MATCH_THRESHOLD = 20;  

    CvSURFParams params = cvSURFParams(SURF_HESSIAN_THRESHOLD, SURF_EXTENDED);
    params.nOctaves=SURF_NOCTAVES;
    params.nOctaveLayers=SURF_NOCTAVELAYERS;
    CvMemStorage* storage = cvCreateMemStorage(0);
    IplImage* image = cvLoadImage(filename, CV_LOAD_IMAGE_GRAYSCALE );
    CvSeq *objectKeypoints = 0, *objectDescriptors = 0;

    // set up the keypoint
    int useProvidedKeyPoints = 1;
    CvMemStorage* kp_storage = cvCreateMemStorage(0);
    CvSeq* surf_kp = cvCreateSeq(0, sizeof(CvSeq), sizeof(CvSURFPoint), kp_storage);
    int laplacian = 1; 
    int direction = 0; 
    int hessian = SURF_HESSIAN_THRESHOLD+1; 
    CvSURFPoint point = cvSURFPoint(cvPoint2D32f(x, y), laplacian, size, direction, hessian);
    cvSeqPush(surf_kp, &point);

    // extract descriptor
    cvExtractSURF(image, 0, &surf_kp, &objectDescriptors, storage, params, useProvidedKeyPoints);

    // print to stdout
    /*
    // if keypoint info also wanted
    CvSeqReader kp_reader;
    cvStartReadSeq(surf_kp, &kp_reader);
    const CvSURFPoint* kp = (const CvSURFPoint*)kp_reader.ptr;
    printf("%.2f %.2f %.2f %d %d",kp->pt.x,kp->pt.y,kp->hessian,kp->laplacian,kp->size);
    */
    
    const float* des = (const float*)cvGetSeqElem(objectDescriptors, 0);
    for (int i=0;i<64;i++){
        printf("%.5f ",des[i]);
    } 
    cvReleaseImage( &image );
}
예제 #25
0
//Wrapper de C a CPP
void HandDetection::findConvexityDefects(vector<Point>& contour, vector<int>& hull, vector<CvConvexityDefect>& convexDefects){
    if(hull.size() > 0 && contour.size() > 0){
        //Conversion de objetos CPP a C
        CvSeq* contourPoints;
        CvSeq* defects;
        CvMemStorage* storage;
        CvMemStorage* strDefects;
        CvMemStorage* contourStr;
        CvConvexityDefect *defectArray = 0;

        strDefects = cvCreateMemStorage();
        defects = cvCreateSeq( CV_SEQ_KIND_GENERIC|CV_32SC2, sizeof(CvSeq),sizeof(CvPoint), strDefects );

        //Empezamos con los contornos: los pasamos a un array de objetos Seq
        contourStr = cvCreateMemStorage();
        contourPoints = cvCreateSeq(CV_SEQ_KIND_GENERIC|CV_32SC2, sizeof(CvSeq), sizeof(CvPoint), contourStr);
        for(int i=0; i<(int)contour.size(); i++) {
            CvPoint cp = {contour[i].x,  contour[i].y};
            cvSeqPush(contourPoints, &cp);
        }

        //Ahora con los puntos del poligono convexo
        int count = (int)hull.size();
        //int hullK[count];
        int* hullK = (int*)malloc(count*sizeof(int));
        for(int i=0; i<count; i++){hullK[i] = hull.at(i);}
        CvMat hullMat = cvMat(1, count, CV_32SC1, hullK);

        //Inicializamos la salida
        storage = cvCreateMemStorage(0);
        defects = cvConvexityDefects(contourPoints, &hullMat, storage);
        defectArray = (CvConvexityDefect*)malloc(sizeof(CvConvexityDefect)*defects->total);
        cvCvtSeqToArray(defects, defectArray, CV_WHOLE_SEQ);
        //Conversion de C a CPP
        //float cutoff = lower - (lower - upper) * 0.3f;
        for(int i = 0; i<defects->total; i++){
            convexDefects.push_back(defectArray[i]);
        }
        //Liberar vectores con cvReleaseMemStorage
        //No olvides hacer un free
        //free(hullK);
        cvReleaseMemStorage(&contourStr);
        cvReleaseMemStorage(&strDefects);
        cvReleaseMemStorage(&storage);

        //Devolvemos el vector de puntos con los defectos de convexidad
        //return defects;
    }
    //return defectArray;
}
예제 #26
0
CPolygonalRegion::CPolygonalRegion(const CPolygonalRegion &r) {

  _storage1 = cvCreateMemStorage (0);
  _pPointsOuter = cvCreateSeq (CV_SEQ_ELTYPE_POINT, sizeof (CvSeq), sizeof (CvPoint), _storage1);
  _dXMin = r._dXMin;
  _dXMax = r._dXMax;
  _dYMin = r._dYMin;
  _dYMax = r._dYMax;

  for (int i=0; i<r._pPointsOuter->total; ++i) {
    CvPoint *pt = (CvPoint*)cvGetSeqElem(r._pPointsOuter, i);
    cvSeqPush(_pPointsOuter, pt);
  }
}
예제 #27
0
CPolygonalRegion & CPolygonalRegion::operator = (const CPolygonalRegion &r) {

  Reset();
  _dXMin = r._dXMin;
  _dXMax = r._dXMax;
  _dYMin = r._dYMin;
  _dYMax = r._dYMax;

  for (int i=0; i<r._pPointsOuter->total; ++i) {
    CvPoint *pt = (CvPoint*)cvGetSeqElem(r._pPointsOuter, i);
    cvSeqPush(_pPointsOuter, pt);
  }
  return *this;
}
예제 #28
0
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)
예제 #29
0
CvSeq *reghand::elimNeighborHulls(CvSeq *hullseq)
{
    int disthreshold=handradis/3;
    CvSeq *filtedhullseq=cvCloneSeq(hullseq);
    if(hullseq->total<=1) return filtedhullseq;
    cvClearSeq(filtedhullseq);
    CvPoint **curdata;CvPoint currpt,nextpt;
    for(int i=0;i<hullseq->total-1;i++)
    {
        curdata=CV_GET_SEQ_ELEM(CvPoint*,hullseq,i);
        currpt=**curdata;
        nextpt=**CV_GET_SEQ_ELEM(CvPoint*,hullseq,i+1);
        double distance=sqrt(pow(double(currpt.x-nextpt.x),2)+pow(double(currpt.y-nextpt.y),2));
        if(distance>disthreshold) cvSeqPush(filtedhullseq,curdata);

    }
    //	if(hullseq->total==2)return filtedhullseq;
    curdata=CV_GET_SEQ_ELEM(CvPoint*,hullseq,hullseq->total-1);
    currpt=**curdata;
    nextpt=**CV_GET_SEQ_ELEM(CvPoint*,hullseq,0);
    double distance=sqrt(pow(double(currpt.x-nextpt.x),2)+pow(double(currpt.y-nextpt.y),2));
    if(distance>disthreshold) cvSeqPush(filtedhullseq,curdata);
    return filtedhullseq;
}
void ConvexityClassifier::findConvexityDefects(vector<Point>& contour, vector<int>& hull, vector<Point>& convexDefects){
    if(hull.size() > 0 && contour.size() > 0){
    CvSeq* contourPoints;
    CvSeq* defects;
    CvMemStorage* storage;
    CvMemStorage* strDefects;
    CvMemStorage* contourStr;
    CvConvexityDefect *defectArray = 0;

    strDefects = cvCreateMemStorage();
    defects = cvCreateSeq( CV_SEQ_KIND_GENERIC|CV_32SC2, sizeof(CvSeq),sizeof(CvPoint), strDefects );

    //We transform our vector<Point> into a CvSeq* object of CvPoint.
    contourStr = cvCreateMemStorage();
    contourPoints = cvCreateSeq(CV_SEQ_KIND_GENERIC|CV_32SC2, sizeof(CvSeq), sizeof(CvPoint), contourStr);
    for(int i=0; i<(int)contour.size(); i++) {
        CvPoint cp = {contour[i].x,  contour[i].y};
        cvSeqPush(contourPoints, &cp);
    }

    //Now, we do the same thing with the hull index
    int count = (int)hull.size();
    //int hullK[count];
    int* hullK = (int*)malloc(count*sizeof(int));
    for(int i=0; i<count; i++){hullK[i] = hull.at(i);}
    CvMat hullMat = cvMat(1, count, CV_32SC1, hullK);

    //We calculate convexity defects
    storage = cvCreateMemStorage(0);
    defects = cvConvexityDefects(contourPoints, &hullMat, storage);
    defectArray = (CvConvexityDefect*)malloc(sizeof(CvConvexityDefect)*defects->total);
    cvCvtSeqToArray(defects, defectArray, CV_WHOLE_SEQ);
    //printf("DefectArray %i %i\n",defectArray->end->x, defectArray->end->y);

    //We store defects points in the convexDefects parameter.
    for(int i = 0; i<defects->total; i++){
        CvPoint ptf;
        ptf.x = defectArray[i].depth_point->x;
        ptf.y = defectArray[i].depth_point->y;
        convexDefects.push_back(ptf);
    }

    //We release memory
    cvReleaseMemStorage(&contourStr);
    cvReleaseMemStorage(&strDefects);
    cvReleaseMemStorage(&storage);
    }
}