2012-09-02 5 views
1

사각형을 감지하기위한 opencv 코드가 있습니다. 그리고 지금 나는 square square를 찾은 후 코드가 다른 명령을 실행하기를 원합니다. 여기 사각형 감지 후 명령을 실행하는 C++ 알고리즘

코드입니다 :

#include "cv.h" 
#include "cxcore.h" 
#include "highgui.h" 
#include "math.h" 
#include <iostream> 
#include <stdio.h> 
#include <string.h> 
#include <sstream> 

using namespace std; 

double angle(CvPoint* pt1, CvPoint* pt2, CvPoint* pt0) 
{ 
    double dx1 = pt1->x - pt0->x; 
    double dy1 = pt1->y - pt0->y; 
    double dx2 = pt2->x - pt0->x; 
    double dy2 = pt2->y - pt0->y; 
    return (dx1*dx2 + dy1*dy2)/sqrt((dx1*dx1 + dy1*dy1)*(dx2*dx2 + dy2*dy2) + 1e-10); 
} // angle 

IplImage* findSquares4( IplImage* img, CvMemStorage* storage) 
{ 
    double s = 0, t = 0; 
    int sdetect =0, sqdetect = 0,sqt = 0; 
    CvSeq* contours; 
    CvSeq* result; 
    CvSeq* squares = cvCreateSeq( 0, sizeof(CvSeq), sizeof(CvPoint), storage); 

    IplImage* cny = cvCreateImage(cvGetSize(img), 8, 1); 

    cvCanny(img, cny, 5, 100, 3); 

    cvNamedWindow("canny",CV_WINDOW_AUTOSIZE); 
    cvShowImage("canny",cny); 

    cvFindContours( cny, storage, &contours, sizeof(CvContour), CV_RETR_LIST, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0)); 

    while( contours) 
    { 
     result = cvApproxPoly( contours, 
           sizeof(CvContour), 
           storage, 
           CV_POLY_APPROX_DP, 
           cvContourPerimeter(contours)*0.02, 0); 

     if( result->total == 4 && 
      fabs(cvContourArea(result, CV_WHOLE_SEQ)) > 1000 && 
      cvCheckContourConvexity(result)) 
     { 
      s = 0; 
      for( int i = 2; i < 5; i++) 
      { 
       { 
        t = fabs(angle(
           (CvPoint*)cvGetSeqElem( result, i), 
           (CvPoint*)cvGetSeqElem( result, i-2), 
           (CvPoint*)cvGetSeqElem( result, i-1))); 
        s = s > t ? s : t; 
        cout << "s = " << s<< endl; 
        cout << "t = " << t<< endl; 
       } 
      } // for 
      if( s < 0.3) 
       for( int i = 0; i < 4; i++) 
        cvSeqPush( squares, 
          (CvPoint*)cvGetSeqElem( result, i)); 
     } // if 

    contours = contours->h_next; 

    } // while 
    if ((squares->total/4) = 1) 
    { 
     sdetect = 1; 
    }//if 
    else 
    { 
     sdetect = 2; 
     sleep(0.5); 
    } // else 

    if (sqdetect != sdetect) 
    { 
     sqdetect=sdetect; 
     switch(sqdetect) 
     { 
      case 0 : system(NULL) ; break; 
      case 2 : cout<< "no "<< endl; break; 
      case 1 : system("./ambil1.sh"); break; 
     } // switch 
    } // if 
    sdetect = 0; 

    cout<<"Persegi : "<< squares->total/4 <<endl; 

    cvReleaseMemStorage(&storage); 
    cvClearSeq(squares); 

} // findSquares4 

void drawSquares(IplImage *img, CvSeq* squares) 
{ 
    CvFont font; 
    cvInitFont(&font, CV_FONT_HERSHEY_SIMPLEX, 0.4f, 0.4f, 0,1, 8); 
    int i,j,sdetect,sqdetect = 0; 
    CvSeqReader reader; 
    cvStartReadSeq(squares, &reader, 0); 

    for(i = 0; i < squares->total; i += 4) 
    { 
     j++; 
     CvPoint pt[4], *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); 

     cvPutText(img, "SQUARE", pt[i], &font, CV_RGB(20,255,0)); 

     cvPolyLine(img, &rect, &count, 1, 1, CV_RGB(200,0,0), 4, CV_AA, 0); 
    } // for 

cvClearSeq(squares); 
} // drawSquares 

하지만 내가 가진 것은 프로그램이 랙이 될 것입니다. 그렇다면 system (./ambil1.sh)을 실행하기위한 조건을 배치하는 올바른 알고리즘은 무엇입니까?

답변

1

종속 명령이 끝날 때까지 프로그램이 대기합니다. 이 시간 동안에는 응답하지 않습니다.

필요한 경우 system이면 충분합니다.

화재 및 잊어 버리려는 경우 fork/exec 쌍을 사용하거나 셸 & 연산자를 사용하여 백그라운드에서 물건을 실행할 수 있습니다.

화재가 필요하면 프로그램의 동작을 변경 한 다음 종속 명령이 끝나면 다시 변경해야하므로 거의 fork/exec/SIGCHLD/waitpid 조합을 사용해야합니다.

+0

화재로 무엇을 의미합니까? 잊어 버리시겠습니까? – Faqih

+0

하위 프로그램을 시작하고 둘 사이의 상호 작용없이 주 프로그램과 병렬로 실행하십시오. –

+0

내 코드를 예제로 제공해 주시겠습니까? – Faqih