2010-03-25 2 views
0

저는 C++을 많이 사용하지 않습니다 (Java와 VB.NET과 같은 더 쉬운 것들을 고수하려고합니다). 그러나 요즘에는 선택의 여지가 없습니다. 내가 다운로드 한 C++ 소스를 위해 VS에서 프로젝트 유형을 선택할 때 어떤 프로젝트 유형을 선택해야합니까? 필자는 Win32 콘솔 응용 프로그램을 계속 사용하고 있었지만 오류가 발생하여 컴파일 할 때도 제대로 작동하지 않는 코드 (아래)를 다운로드했습니다. CLR Console Application과 빈 프로젝트를 사용하려고 시도했지만 많은 변수가 변경되었지만이 코드를 사용할 수는 없습니다. 이 코드는 처음에 "int main()"을 가지고 있지 않은 것으로 나타났습니다. 그 코드와 관련이 있습니까? 이전 코드 전에 ... 넣으면VS 프로젝트 유형 선택 (C++)

/* Demo of modified Lucas-Kanade optical flow algorithm. 
    See the printf below */ 

#ifdef _CH_ 
#pragma package <opencv> 
#endif 

#ifndef _EiC 
#include "cv.h" 
#include "highgui.h" 
#include <stdio.h> 
#include <ctype.h> 
#endif 

#include <windows.h> 

#define FULL_IMAGE_AS_OUTPUT_FILE 


#define cvMirror cvFlip 

//IplImage *image = 0, *grey = 0, *prev_grey = 0, *pyramid = 0, *prev_pyramid = 0, *swap_temp; 
IplImage **buf = 0; 
IplImage *image1 = 0; 
IplImage *imageCopy=0; 

IplImage *image = 0; 
int win_size = 10; 
const int MAX_COUNT = 500; 
CvPoint2D32f* points[2] = {0,0}, *swap_points; 
char* status = 0; 
//int count = 0; 
//int need_to_init = 0; 
//int night_mode = 0; 
int flags = 0; 
//int add_remove_pt = 0; 
bool bLButtonDown = false; 
//bool bstopLoop = false; 
CvPoint pt, pt1,pt2; 
//IplImage* img1; 
FILE* FileDest; 

char* strImageDir = "E:\\Projects\\TSCreator\\Images"; 
char* strItemName = "b"; 
int imageCount=0; 
int bFirstFace = 1;  // flag for first face 
int mode = 1; // Mode 1 - Haar Traing Sample Creation, 2 - HMM sample creation, Mode = 3 - Both Harr and HMM. 
//int startImgeNo = 1; 
bool isEqualRation = false; //Weidth to height ratio is equal 

//Selected Image data 
IplImage *selectedImage = 0; 
int selectedX = 0, selectedY = 0, currentImageNo = 0, selectedWidth = 0, selectedHeight= 0; 
CvRect selectedROI; 


void saveFroHarrTraining(IplImage *src, int x, int y, int width, int height, int imageCount); 
void saveForHMMTraining(IplImage *src, CvRect roi,int imageCount); 

// Code for draw ROI Cropping Image 
void on_mouse(int event, int x, int y, int flags, void* param) 
{ 

char f[200]; 
CvRect reg; 

     if(!image) 
      return; 

    if(event == CV_EVENT_LBUTTONDOWN) 
    { 
     bLButtonDown = true; 

     pt1.x = x; 
     pt1.y = y; 

    } 
    else if (event == CV_EVENT_MOUSEMOVE) //Draw the selected area rectangle 
    { 
     pt2.x = x; 
     pt2.y = y; 

     if(bLButtonDown) 
     { 
      if(!image1) 
     { 
      /* allocate all the buffers */ 
      image1 = cvCreateImage(cvGetSize(image), 8, 3); 
      image1->origin = image->origin; 
      points[0] = (CvPoint2D32f*)cvAlloc(MAX_COUNT*sizeof(points[0][0])); 
      points[1] = (CvPoint2D32f*)cvAlloc(MAX_COUNT*sizeof(points[0][0])); 
      status = (char*)cvAlloc(MAX_COUNT); 
      flags = 0; 
     } 

     cvCopy(image, image1, 0);  

     //Equal Weight-Height Ratio 
      if(isEqualRation) 
      { 
       pt2.y = pt1.y + (pt2.x-pt1.x); 
      } 

      //Max Height and Width is the image width and height 
      if(pt2.x>image->width) 
      { 
       pt2.x = image->width; 
      } 
      if(pt2.y>image->height) 
      { 
       pt2.y = image->height; 
      } 

      CvPoint InnerPt1 = pt1; 
      CvPoint InnerPt2 = pt2; 

      if (InnerPt1.x > InnerPt2.x) 
      { 
       int tempX = InnerPt1.x; 
       InnerPt1.x = InnerPt2.x; 
       InnerPt2.x = tempX; 
      } 

      if (pt2.y < InnerPt1.y) 
      { 
       int tempY = InnerPt1.y; 
       InnerPt1.y = InnerPt2.y; 
       InnerPt2.y = tempY; 

      } 

      InnerPt1.y = image->height - InnerPt1.y; 
      InnerPt2.y = image->height - InnerPt2.y;  




      CvFont font; 
      double hScale=1.0; 
      double vScale=1.0; 
      int lineWidth=1; 
      cvInitFont(&font,CV_FONT_HERSHEY_SIMPLEX|CV_FONT_ITALIC, hScale,vScale,0,lineWidth); 

      char size [200]; 
      reg.x = pt1.x; 
      reg.y = image->height - pt2.y; 
      reg.height = abs (pt2.y - pt1.y); 
      reg.width = InnerPt2.x -InnerPt1.x; 
      //print width and heght of the selected reagion 
      sprintf(size, "(%dx%d)",reg.width, reg.height); 
      cvPutText (image1,size,cvPoint(10,10), &font, cvScalar(255,255,0)); 
      cvRectangle(image1, InnerPt1, InnerPt2, CV_RGB(255,0,0), 1); 

      //Mark Selected Reagion 
      selectedImage = image; 
      selectedX = pt1.x; 
      selectedY = pt1.y; 
      selectedWidth = reg.width; 
      selectedHeight = reg.height; 
      selectedROI = reg; 

      //Show the modified image 
      cvShowImage("HMM-Harr Positive Image Creator",image1); 
     } 

    } 
    else if (event == CV_EVENT_LBUTTONUP) 
    { 
     bLButtonDown = false; 

//  pt2.x = x; 
//  pt2.y = y; 
// 
//  if (pt1.x > pt2.x) 
//  { 
//   int tempX = pt1.x; 
//   pt1.x = pt2.x; 
//   pt2.x = tempX; 
//  } 
// 
//  if (pt2.y < pt1.y) 
//  { 
//   int tempY = pt1.y; 
//   pt1.y = pt2.y; 
//   pt2.y = tempY; 
// 
//  } 
// 
//reg.x = pt1.x; 
//reg.y = image->height - pt2.y; 
// 
//reg.height = abs (pt2.y - pt1.y); 
////reg.width = reg.height/3; 
//reg.width = pt2.x -pt1.x; 
////reg.height = (2 * reg.width)/3; 



#ifdef FULL_IMAGE_AS_OUTPUT_FILE 
     CvRect FullImageRect; 
     FullImageRect.x = 0; 
     FullImageRect.y = 0; 
     FullImageRect.width = image->width; 
     FullImageRect.height = image->height; 

     IplImage *regionFullImage =0; 
     regionFullImage = cvCreateImage(cvSize (FullImageRect.width, FullImageRect.height), 
            image->depth, image->nChannels); 

     image->roi = NULL; 

     //cvSetImageROI (image, FullImageRect); 
     //cvCopy (image, regionFullImage, 0); 

#else 
     IplImage *region =0; 
     region = cvCreateImage(cvSize (reg.width, reg.height),    
           image1->depth, image1->nChannels); 

     image->roi = NULL; 
     cvSetImageROI (image1, reg); 

     cvCopy (image1, region, 0); 
#endif 
     //cvNamedWindow("Result", CV_WINDOW_AUTOSIZE); 

     //selectedImage = image; 
     //selectedX = pt1.x; 
     //selectedY = pt1.y; 
     //selectedWidth = reg.width; 
     //selectedHeight = reg.height; 
     ////currentImageNo = startImgeNo; 
     //selectedROI = reg; 

     /*if(mode == 1) 
     { 
      saveFroHarrTraining(image,pt1.x,pt1.y,reg.width,reg.height,startImgeNo); 
     } 
     else if(mode == 2) 
     { 
      saveForHMMTraining(image,reg,startImgeNo); 
     } 
     else if(mode ==3) 
     { 
      saveFroHarrTraining(image,pt1.x,pt1.y,reg.width,reg.height,startImgeNo); 
      saveForHMMTraining(image,reg,startImgeNo); 
     } 
     else 
     { 
      printf("Invalid mode."); 
     } 


     startImgeNo++;*/ 

    } 
} 

/* 
Save popsitive samples for Harr Training. 
Also add an entry to the PositiveSample.txt with the location of the item of interest. 
*/ 
void saveFroHarrTraining(IplImage *src, int x, int y, int width, int height, int imageCount) 
{ 
    char f[255] ; 
     sprintf(f,"%s\\%s\\harr_%s%d%d.jpg",strImageDir,strItemName,strItemName,imageCount/10, imageCount%10); 

     cvNamedWindow("Harr", CV_WINDOW_AUTOSIZE); 
     cvShowImage("Harr", src); 
     cvSaveImage(f, src); 


     printf("output%d%d \t ", imageCount/10, imageCount%10); 
     printf("width %d \t", width); 
     printf("height %d \t", height); 
     printf("x1 %d \t", x); 
     printf("y1 %d \t\n", y); 

     char f1[255]; 
     sprintf(f1,"%s\\PositiveSample.txt",strImageDir); 
     FileDest = fopen(f1, "a"); 
     fprintf(FileDest, "%s\\harr_%s%d.jpg 1 %d %d %d %d \n",strItemName,strItemName, imageCount, x, y, width, height); 
     fclose(FileDest); 
} 

/* 
Create Sample Images for HMM recognition algorythm trai ning. 
*/ 
void saveForHMMTraining(IplImage *src, CvRect roi,int imageCount) 
{ 
    char f[255] ; 
    printf("x=%d, y=%d, w= %d, h= %d\n",roi.x,roi.y,roi.width,roi.height); 
    //Create the file name 
    sprintf(f,"%s\\%s\\hmm_%s%d.pgm",strImageDir,strItemName,strItemName, imageCount); 

    //Create storage for grayscale image 
    IplImage* gray = cvCreateImage(cvSize(roi.width,roi.height), 8, 1); 
    //Create storage for croped reagon 
    IplImage* regionFullImage = cvCreateImage(cvSize(roi.width,roi.height),8,3); 
    //Croped marked region 
    cvSetImageROI(src,roi); 
    cvCopy(src,regionFullImage); 
    cvResetImageROI(src); 
    //Flip croped image - otherwise it will saved upside down 
    cvConvertImage(regionFullImage, regionFullImage, CV_CVTIMG_FLIP); 
    //Convert croped image to gray scale 
    cvCvtColor(regionFullImage,gray, CV_BGR2GRAY); 
    //Show final grayscale image 
    cvNamedWindow("HMM", CV_WINDOW_AUTOSIZE); 
    cvShowImage("HMM", gray); 
    //Save final grayscale image 
    cvSaveImage(f, gray); 
} 

int maina(int argc, char** argv) 
{ 
    CvCapture* capture = 0; 
    //if(argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0]))) 
    // capture = cvCaptureFromCAM(argc == 2 ? argv[1][0] - '0' : 0); 
    //else if(argc == 2) 
    // capture = cvCaptureFromAVI(argv[1]); 

    char* video; 

    if(argc ==7) 
    { 
     mode = atoi(argv[1]); 
     strImageDir = argv[2]; 
     strItemName = argv[3]; 
     video = argv[4]; 
     currentImageNo = atoi(argv[5]); 
     int a = atoi(argv[6]); 
     if(a==1) 
     { 
      isEqualRation = true; 
     } 
     else 
     { 
      isEqualRation = false; 
     } 

    } 
    else 
    { 
     printf("\nUsage: TSCreator.exe <Mode> <Sample Image Save Path> <Sample Image Save Directory> <Video File Location> <Start Image No> <Is Equal Ratio>\n"); 
     printf("Mode = 1 - Haar Traing Sample Creation. \nMode = 2 - HMM sample creation.\nMode = 3 - Both Harr and HMM\n"); 
     printf("Is Equal Ratio = 0 or 1. 1 - Equal weidth and height, 0 - custom."); 
     printf("Note: You have to create the image save directory in correct path first.\n"); 
     printf("Eg: TSCreator.exe 1 E:\Projects\TSCreator\Images A 11.avi 1 1\n\n"); 
     return 0; 
    } 
     capture = cvCaptureFromAVI(video); 

    if(!capture) 
    { 
     fprintf(stderr,"Could not initialize capturing...\n"); 
     return -1; 
    } 

    cvNamedWindow("HMM-Harr Positive Image Creator", CV_WINDOW_AUTOSIZE); 
    cvSetMouseCallback("HMM-Harr Positive Image Creator", on_mouse, 0); 
    //cvShowImage("Test", image1); 

    for(;;) 
    { 
     IplImage* frame = 0; 
     int i, k, c; 

     frame = cvQueryFrame(capture); 
     if(!frame) 
      break; 

     if(!image) 
     { 
      /* allocate all the buffers */ 
      image = cvCreateImage(cvGetSize(frame), 8, 3); 
      image->origin = frame->origin; 
      //grey = cvCreateImage(cvGetSize(frame), 8, 1); 
      //prev_grey = cvCreateImage(cvGetSize(frame), 8, 1); 
      //pyramid = cvCreateImage(cvGetSize(frame), 8, 1); 
      // prev_pyramid = cvCreateImage(cvGetSize(frame), 8, 1); 
      points[0] = (CvPoint2D32f*)cvAlloc(MAX_COUNT*sizeof(points[0][0])); 
      points[1] = (CvPoint2D32f*)cvAlloc(MAX_COUNT*sizeof(points[0][0])); 
      status = (char*)cvAlloc(MAX_COUNT); 
      flags = 0; 
     } 

     cvCopy(frame, image, 0); 
     // cvCvtColor(image, grey, CV_BGR2GRAY); 


     cvShowImage("HMM-Harr Positive Image Creator", image); 
     cvSetMouseCallback("HMM-Harr Positive Image Creator", on_mouse, 0); 
     c = cvWaitKey(0); 
     if((char)c == 's') 
     { 
      //Save selected reagion as training data 
      if(selectedImage) 
      { 
       printf("Selected Reagion Saved\n"); 
       if(mode == 1) 
       { 
        saveFroHarrTraining(selectedImage,selectedX,selectedY,selectedWidth,selectedHeight,currentImageNo); 
       } 
       else if(mode == 2) 
       { 
        saveForHMMTraining(selectedImage,selectedROI,currentImageNo); 
       } 
       else if(mode ==3) 
       { 
        saveFroHarrTraining(selectedImage,selectedX,selectedY,selectedWidth,selectedHeight,currentImageNo); 
        saveForHMMTraining(selectedImage,selectedROI,currentImageNo); 
       } 
       else 
       { 
        printf("Invalid mode."); 
       } 
       currentImageNo++; 
      }  

     } 

    } 

    cvReleaseCapture(&capture); 
    //cvDestroyWindow("HMM-Harr Positive Image Creator"); 
    cvDestroyAllWindows(); 

    return 0; 
} 

#ifdef _EiC 
main(1,"lkdemo.c"); 
#endif 

...

#include "stdafx.h" 


int _tmain(int argc, _TCHAR* argv[]) 
{ 
    return 0; 
} 

(올바른 OpenCV의 lib 디렉토리 파일에 연결 : 어쨌든, 여기에 코드가, here에서 그것을 가지고) 오류없이 컴파일되지만 명령 행에서는 아무것도 수행하지 않습니다. 어떻게 작동합니까?

답변

2

새 프로젝트 -> Visual C++ -> Win32 -> Win32 콘솔 응용 프로그램을 사용해보십시오. 모든 기본값을 가져 와서 프로젝트에 새로운 .cpp 파일을 가져올 수 있습니다.이 파일에는 _tmain이 있습니다. 다음과 같이 _tmain 수정 :

int maina(int argc, char** argv); // add this 

int _tmain(int argc, _TCHAR* argv[]) 
{ 
    return maina(argc, argv); // call maina from here 
} 

을 한 다음 프로젝트 속성을 설정 -> 구성 속성 -> 일반 -> "유니 코드 문자 집합"을 "사용 멀티 바이트 문자 집합"의 기본에서 설정하는 문자입니다. (이것을 변경하기 전까지는 _TCHAR * []을 char **로 전달할 때 컴파일러 오류가 발생합니다.

그런 다음 수정되지 않은 프로젝트에 소스 파일을 추가하고 가져올 수 있는지 확인하십시오. 컴파일하고 링크하십시오. 그런 다음 5 개의 인쇄물에 "사용법 : ..."출력이 있는지 확인하기 위해 args없이 콘솔 창에서 실행하십시오.

(나는대로 그냥 당신에게 문제를주고있다 멀티 바이트 문자 집합 대 유니 코드를 할 수있다이 경우에, 그래서 내가 Win32 콘솔 응용 프로그램을 사용하여 있습니다 당신을보고 질문을 다시 읽어보십시오.)

(새 프로젝트의 기본값을 사용하면 추가 된 기존 소스 파일을 선택하고 속성 -> 구성 속성 -> C/C++ -> 미리 컴파일 된 헤더 -> 미리 컴파일 된 헤더 생성/사용으로 이동하여 미리 컴파일 된 헤더를 사용하지 않음)

+0

당신은 사람입니다. 그래도 메모는 미리 컴파일 된 헤더가 있거나없는 상태로 작동합니다. 나는 C++ 멍청한 놈 (그리고 프로그래밍 놈)이기 때문에 몇 가지를 나에게 설명 할 수 있니? 1.) 문자 집합, 미리 컴파일 된 헤더, 공용 언어 런타임 지원 등을 변경해야 할 때 어떻게 알 수 있습니까? 2.) Win32 vs CLR, 차이점은 무엇이며 어떤 항목을 기본적으로 선택해야합니까? 삼.) 내가 게시 한 소스 코드를 개발 한 사람이이를 수행해야한다고 생각하십니까? 나는 그런 코드가 SSCCE 스타일로 게시 될 것이라고 생각했을 것이다 ... 다시 한번 감사드립니다! – ubiquibacon

+0

멀티 바이트 사용은 추측에 불과합니다. char 대신 wchar_t가 표시되면 유니 코드를 사용했을 가능성이 큽니다. 관련 정보가 많이있는 Google '_MBCS _UNICODE' 컴파일러가 작동하도록하려면 미리 컴파일 된 헤더가 필요하지 않습니다. 단지 컴파일 속도를 높이기 위해서입니다. 예제 시나리오는 생각할 수 없지만 동기화가되지 않을 수 있습니다. 의심 스럽다면 전체 프로젝트를 다시 작성하여 다시 생성하십시오. C++이 .NET, AFAIK를 사용하는 경우에만 CLR이 필요합니다. 이 경우 C++/CLI 구문이 두드러 져야합니다. String^myString. –

0

C++ 응용 프로그램이 시작되면

int main(int argc, char** argv) 

기능. 그러나 창에서 이것은

int _tmain(int argc, _TCHAR* argv[]) 

의 형식을 취할 수 있습니다. 둘 다 동일하다고 가정하고, 다르게 작성된 것으로 가정 할 수 있습니다. 응용 프로그램이 시작되면 _tmain을로드 한 다음 0을 반환하므로 출력이 표시되지 않습니다. 이는 정상입니다.

int maina (...)의 이름을 int _tmain (...)으로 바꿉니다.

관련 문제