加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – xutility文件?

发布时间:2020-12-16 10:50:38 所属栏目:百科 来源:网络整理
导读:我正试图在opencv中使用C代码 检测和计数,但我无法建立源. 我正在尝试编译我的项目 xutility文件中的一行有很多问题. 错误消息显示它在xutility文件中给出错误. 请帮帮我解决这个问题? 码 // Include header files#include "stdafx.h"#include "cv.h"#inclu
我正试图在opencv中使用C代码
检测和计数,但我无法建立源.
我正在尝试编译我的项目
xutility文件中的一行有很多问题.

错误消息显示它在xutility文件中给出错误.

请帮帮我解决这个问题?

// Include header files
#include "stdafx.h"
#include "cv.h"
#include "highgui.h"

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <math.h>
#include <float.h>
#include <limits.h>
#include <time.h>
#include <ctype.h>
#include <iostream>
#include <fstream>
#include <vector>

using namespace std;


#ifdef _EiC
#define WIN32
#endif

int countfaces=0;
int numFaces = 0;
int k=0 ;
int list=0;
char filelist[512][512];

int timeCount = 0;

static CvMemStorage* storage = 0;
static CvHaarClassifierCascade* cascade = 0;

void detect_and_draw( IplImage* image );

void WriteInDB();
int found_face(IplImage* img,CvPoint pt1,CvPoint pt2);
int load_DB(char * filename);


const char* cascade_name = "C:Program FilesOpenCVOpenCV2.1datahaarcascadeshaarcascade_frontalface_alt_tree.xml"; 


// BEGIN NEW CODE
#define WRITEVIDEO
char* outputVideo = "c:face_counting1_tracked.avi";
//int faceCount = 0;
int posBuffer = 100;
int persistDuration = 10; //faces can drop out for 10 frames
int timestamp = 0;
float sameFaceDistThreshold = 30; //pixel distance
CvPoint facePositions[100];
int     facePositionsTimestamp[100];

float distance( CvPoint a,CvPoint b ) {
    float dist = sqrt(float ( (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) ) );
    return dist;
}

void expirePositions()
{
    for (int i = 0; i < posBuffer; i++)
    {
        if (facePositionsTimestamp[i] <= (timestamp - persistDuration))    //if a tracked pos is older than three frames
        {
            facePositions[i] = cvPoint(999,999);
        }
    }
}

void updateCounter(CvPoint center) 
{
    bool newFace = true;
    for(int i = 0; i < posBuffer; i++) 
    {
        if (distance(center,facePositions[i]) < sameFaceDistThreshold)
        {
            facePositions[i] = center;
            facePositionsTimestamp[i] = timestamp;
            newFace = false;
            break;
        }
    }
    if(newFace)
    {
        //push out oldest tracker
        for(int i = 1; i < posBuffer; i++) 
        {
            facePositions[i] = facePositions[i - 1];
        }
        //put new tracked position on top of stack
        facePositions[0] = center;
        facePositionsTimestamp[0] = timestamp;
        countfaces++;
    }
}

void drawCounter(IplImage* image) {
    // Create Font
    char buffer[5];
    CvFont font;
    cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX,.5,1);
    cvPutText(image,"Faces:",cvPoint(20,20),&font,CV_RGB(0,255,0));
    cvPutText(image,itoa(countfaces,buffer,10),cvPoint(80,0));
}
#ifdef WRITEVIDEO
CvVideoWriter* videoWriter = cvCreateVideoWriter(outputVideo,-1,30,cvSize(240,180));
#endif
//END NEW CODE

int main( int argc,char** argv )
{

    CvCapture* capture = 0;
    IplImage *frame,*frame_copy = 0;
    int optlen = strlen("--cascade=");
    const char* input_name;

    if( argc > 1 && strncmp( argv[1],"--cascade=",optlen ) == 0 )
    {
        cascade_name = argv[1] + optlen;
        input_name = argc > 2 ? argv[2] : 0;
    }
    else
    {
        cascade_name =  "C:Program FilesOpenCVOpenCV2.1datahaarcascadeshaarcascade_frontalface_alt_tree.xml"; 

        input_name = argc > 1 ? argv[1] : 0;
    }

    cascade = (CvHaarClassifierCascade*)cvLoad( cascade_name,0 );

    if( !cascade )
    {
        fprintf( stderr,"ERROR: Could not load classifier cascaden" );
        fprintf( stderr,"Usage: facedetect --cascade="<cascade_path>" [filename|camera_index]n" );
        return -1;
    }
    storage = cvCreateMemStorage(0);

    //if( !input_name || (isdigit(input_name[0]) && input_name[1] == '') )
    //    capture = cvCaptureFromCAM( !input_name ? 0 : input_name[0] - '0' );
    //else

    capture = cvCaptureFromAVI( "c:face_counting1.avi" ); 

    cvNamedWindow( "result",1 );

    if( capture )
    {
        for(;;)
        {
            if( !cvGrabFrame( capture ))
                break;
            frame = cvRetrieveFrame( capture );
            if( !frame )
                break;
            if( !frame_copy )
                frame_copy = cvCreateImage( cvSize(frame->width,frame->height),IPL_DEPTH_8U,frame->nChannels );
            if( frame->origin == IPL_ORIGIN_TL )
                cvCopy( frame,frame_copy,0 );
            else
                cvFlip( frame,0 );

            detect_and_draw( frame_copy );

            if( cvWaitKey( 30 ) >= 0 )
                break;
        }

        cvReleaseImage( &frame_copy );
        cvReleaseCapture( &capture );
    }
    else
    {
        if( !input_name || (isdigit(input_name[0]) && input_name[1] == ''))
        cvNamedWindow( "result",1 );
        const char* filename = input_name ? input_name : (char*)"lena.jpg";
        IplImage* image = cvLoadImage( filename,1 );

        if( image )
        {
            detect_and_draw( image );
            cvWaitKey(0);
            cvReleaseImage( &image );
        }
        else
        {
            /* assume it is a text file containing the
               list of the image filenames to be processed - one per line */
            FILE* f = fopen( filename,"rt" );
            if( f )
            {
                char buf[1000+1];
                while( fgets( buf,1000,f ) )
                {
                    int len = (int)strlen(buf);
                    while( len > 0 && isspace(buf[len-1]) )
                        len--;
                    buf[len] = '';
                    image = cvLoadImage( buf,1 );
                    if( image )
                    {
                        detect_and_draw( image );
                        cvWaitKey(0);
                        cvReleaseImage( &image );
                    }
                }
                fclose(f);
            }
        }

    }

    cvDestroyWindow("result");
    #ifdef WRITEVIDEO
    cvReleaseVideoWriter(&videoWriter); 
    #endif
    return 0;
}

void detect_and_draw( IplImage* img )
{
    static CvScalar colors[] = 
    {
        {{0,255}},{{0,128,0}},{{255,255}}
    };

         double scale = 1.3;
        IplImage* gray = cvCreateImage( cvSize(img->width,img->height),8,1 );
         IplImage* small_img = cvCreateImage( cvSize( cvRound (img->width/scale),cvRound (img->height/scale)),1 );
        CvPoint pt1,pt2;
        int i;

        cvCvtColor( img,gray,CV_BGR2GRAY );
        cvResize( gray,small_img,CV_INTER_LINEAR );
         cvEqualizeHist( small_img,small_img );
        cvClearMemStorage( storage );

        if( cascade )
            {
            double t = (double)cvGetTickCount();
            CvSeq* faces = cvHaarDetectObjects( small_img,cascade,storage,1.1,2,0/*CV_HAAR_DO_CANNY_PRUNING*/,cvSize(30,30) );
            t = (double)cvGetTickCount() - t;
            printf( "detection time = %gmsn",t/((double)cvGetTickFrequency()*1000.) );

            if (faces)
            {
            //To save the detected faces into separate images,here's a quick and dirty code:
            char filename[6];

            for( i = 0; i < (faces ? faces->total : 0); i++ )
             {
                /* CvRect* r = (CvRect*)cvGetSeqElem( faces,i );
                CvPoint center;
                int radius;
                center.x = cvRound((r->x + r->width*0.5)*scale);
                center.y = cvRound((r->y + r->height*0.5)*scale); 
                radius = cvRound((r->width + r->height)*0.25*scale);
                cvCircle( img,center,radius,colors[i%8],3,0 );*/
                // Create a new rectangle for drawing the face
                CvRect* r = (CvRect*)cvGetSeqElem( faces,i );

                // Find the dimensions of the face,and scale it if necessary
                pt1.x = r->x*scale;
                pt2.x = (r->x+r->width)*scale;
                pt1.y = r->y*scale;
                pt2.y = (r->y+r->height)*scale;

                // Draw the rectangle in the input image
                cvRectangle( img,pt1,pt2,CV_RGB(255,0),0 );
                CvPoint center;
                int radius;
                center.x = cvRound((r->x + r->width*0.5)*scale);
                center.y = cvRound((r->y + r->height*0.5)*scale); 
                radius = cvRound((r->width + r->height)*0.25*scale);
                cvCircle( img,0 );

                //update counter
                updateCounter(center);

                int y=found_face(img,pt2);
                if(y==0)
                countfaces++;
            }//end for
            printf("Number of detected faces: %dt",countfaces);

    }//end if 
    //delete old track positions from facePositions array
    expirePositions();
    timestamp++;

    //draw counter
    drawCounter(img);
    #ifdef WRITEVIDEO
    cvWriteFrame(videoWriter,img);
    #endif
    cvShowImage( "result",img );
     cvDestroyWindow("Result");
    cvReleaseImage( &gray );
    cvReleaseImage( &small_img );
}//end if
} //end void


int found_face(IplImage* img,CvPoint pt2)
{
        /*if (faces)
            {*/
        CvSeq* faces = cvHaarDetectObjects( img,CV_HAAR_DO_CANNY_PRUNING,cvSize(40,40) );
        int i=0;
        char filename[512];
         for( i = 0; i < (faces ? faces->total : 0); i++ )
          {//int scale = 1,i=0;
            //i=iface;
            //char filename[512];

            /* extract the rectanlges only */
             //  CvRect face_rect = *(CvRect*)cvGetSeqElem( faces,i);
            CvRect face_rect = *(CvRect*)cvGetSeqElem( faces,i);


            //IplImage* gray_img = cvCreateImage( cvGetSize(img),1 );
            IplImage* clone = cvCreateImage (cvSize(img->width,img->nChannels );
            IplImage* gray = cvCreateImage (cvSize(img->width,1 );

            cvCopy (img,clone,0);
            cvNamedWindow ("ROI",CV_WINDOW_AUTOSIZE);
            cvCvtColor( clone,CV_RGB2GRAY );
            face_rect.x = pt1.x;
            face_rect.y = pt1.y;
            face_rect.width = abs(pt1.x - pt2.x);
            face_rect.height = abs(pt1.y - pt2.y);


             cvSetImageROI ( gray,face_rect);
            ////    * rectangle  = cvGetImageROI ( clone );
            face_rect = cvGetImageROI ( gray );
            cvShowImage ("ROI",gray);
            k++;
            char *name=0;
             name=(char*) calloc(512,1);
            sprintf(name,"Image%d.pgm",k);
            cvSaveImage(name,gray);

            ////////////////
            for(int j=0;j<512;j++)
            filelist[list][j]=name[j];
            list++;
            WriteInDB();
            //int found=SIFT("result.txt",name);
            cvResetImageROI( gray );
            //return found;
            return 0;
        //  }//end if

        }//end for
}//end void

void WriteInDB()
{
ofstream myfile;
myfile.open ("result.txt");
for(int i=0;i<512;i++)
{
if(strcmp(filelist[i],"")!=0)
myfile << filelist[i]<<"n";
}
myfile.close();
}

错误消息

Error   3   error C4430: missing type specifier - int assumed. Note: C++ does not support default-int   
Error   8   error C4430: missing type specifier - int assumed. Note: C++ does not support default-int   
Error   13  error C4430: missing type specifier - int assumed. Note: C++ does not support default-int   c:program filesmicrosoft visual studio 9.0vcincludexutility    766
Error   18  error C4430: missing type specifier - int assumed. Note: C++ does not support default-int   c:program filesmicrosoft visual studio 9.0vcincludexutility    768
Error   23  error C4430: missing type specifier - int assumed. Note: C++ does not support default-int   c:program filesmicrosoft visual studio 9.0vcincludexutility    769
Error   10  error C2868: 'std::iterator_traits<_Iter>::value_type' : illegal syntax for using-declaration; expected qualified-name  c:program filesmicrosoft visual studio 9.0vcincludexutility    765
Error   25  error C2868: 'std::iterator_traits<_Iter>::reference' : illegal syntax for using-declaration; expected qualified-name   c:program filesmicrosoft visual studio 9.0vcincludexutility    769
Error   20  error C2868: 'std::iterator_traits<_Iter>::pointer' : illegal syntax for using-declaration; expected qualified-name c:program filesmicrosoft visual studio 9.0vcincludexutility    768
Error   5   error C2868: 'std::iterator_traits<_Iter>::iterator_category' : illegal syntax for using-declaration; expected qualified-name   c:program filesmicrosoft visual studio 9.0vcincludexutility    764
Error   15  error C2868: 'std::iterator_traits<_Iter>::difference_type' : illegal syntax for using-declaration; expected qualified-name c:program filesmicrosoft visual studio 9.0vcincludexutility    766
Error   9   error C2602: 'std::iterator_traits<_Iter>::value_type' is not a member of a base class of 'std::iterator_traits<_Iter>' c:program filesmicrosoft visual studio 9.0vcincludexutility    765
Error   24  error C2602: 'std::iterator_traits<_Iter>::reference' is not a member of a base class of 'std::iterator_traits<_Iter>'  c:program filesmicrosoft visual studio 9.0vcincludexutility    769
Error   19  error C2602: 'std::iterator_traits<_Iter>::pointer' is not a member of a base class of 'std::iterator_traits<_Iter>'    c:program filesmicrosoft visual studio 9.0vcincludexutility    768
Error   4   error C2602: 'std::iterator_traits<_Iter>::iterator_category' is not a member of a base class of 'std::iterator_traits<_Iter>'  c:program filesmicrosoft visual studio 9.0vcincludexutility    764
Error   14  error C2602: 'std::iterator_traits<_Iter>::difference_type' is not a member of a base class of 'std::iterator_traits<_Iter>'    c:program filesmicrosoft visual studio 9.0vcincludexutility    766
Error   7   error C2146: syntax error : missing ';' before identifier 'value_type'  c:program filesmicrosoft visual studio 9.0vcincludexutility    765
Error   22  error C2146: syntax error : missing ';' before identifier 'reference'   c:program filesmicrosoft visual studio 9.0vcincludexutility    769
Error   17  error C2146: syntax error : missing ';' before identifier 'pointer' c:program filesmicrosoft visual studio 9.0vcincludexutility    768
Error   2   error C2146: syntax error : missing ';' before identifier 'iterator_category'   c:program filesmicrosoft visual studio 9.0vcincludexutility    764
Error   12  error C2146: syntax error : missing ';' before identifier 'difference_type' c:program filesmicrosoft visual studio 9.0vcincludexutility    766
Error   6   error C2039: 'value_type' : is not a member of 'CvPoint'    c:program filesmicrosoft visual studio 9.0vcincludexutility    765
Error   21  error C2039: 'reference' : is not a member of 'CvPoint' c:program filesmicrosoft visual studio 9.0vcincludexutility    769
Error   16  error C2039: 'pointer' : is not a member of 'CvPoint'   c:program filesmicrosoft visual studio 9.0vcincludexutility    768
Error   1   error C2039: 'iterator_category' : is not a member of 'CvPoint' c:program filesmicrosoft visual studio 9.0vcincludexutility    764
Error   11  error C2039: 'difference_type' : is not a member of 'CvPoint'   c:program filesmicrosoft visual studio 9.0vcincludexutility    766

解决方法

我提到过,你的代码中有一个名为“distance”的函数.我和我一直收到同样的错误.将此函数重命名为“Distance”后,我的代码编译成功.我的猜测是在xutility文件中定义了另一个名为“distance”的函数. 因此解决方案就是重命名该功能.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读