2014-11-03 3 views
0

여러 개의 Haar Classifier를 사용하여 하나의 창에서 여러 객체를 감지하는 OpenCV 프로그램이 있습니다. 첫 번째 물체는 감지되고 타원은 그려야하지만 두 개의 두 번째 물체가 감지되면 모든 물체가 그려지지 않습니다 (물체가 감지되면 콘솔로 출력됩니다).하나의 창에서 여러 객체를 감지하는 OpenCV - 그려지지 않음

String cascade_name = "frontalface.xml"; 
    String nestcascade_name = "body.xml"; 
    String nested_cascade_name_two = "HandCascade.xml"; 

그때 사용하여 분류를로드하고 있습니다 :

std::vector<Rect> firstObject; 
    std::vector<Rect> secondObject; 
    std::vector<Rect> thirdObject; 
: 다음 세 가지 개체에 대한 세 가지 벡터를 만들

cascade_one.load(cascade_name) 
cascade_two.load(nested_cascade_name) 
cascade_three.load(nested_cascade_name_two) 

내가 세과 같이 분류를 지정하고

다음 코드를 사용하여 화면에 객체를 찾아 그리십시오.

cascade_one.detectMultiScale(frame_gray, firstObject, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30)); 
    for(size_t i = 0; i < firstObject.size(); i++) { 
    Point center(firstObject[i].x + firstObject[i].width*0.5, firstObject[i].y + firstObject[i].height*0.5); 
    ellipse(frame, center, Size(firstObject[i].width*0.5, firstObject[i].height*0.5), 0, 0, 360, Scalar(0, 255, 0), 4, 8, 0); //GREEN 
    std::cout << " " << cascade_name << " " << timeFound() << endl; 
    } 

각 개체에 대한 관련 이름을 가진 cascade_onefirstObjectcascade_name 변경. 첫 번째 객체가 완벽하게 작동하는 이유는 무엇입니까? 그러나 두 번째 및 세 번째 객체는 모두 화면에 그려지지 않았지만 여러 개의 탐지 결과를 출력하고 있습니까?

편집 :

전체 감지하고 그리는 코드 :이 잘 작동하는 동안, 당신은했습니다

Point center(firstObject[i].x + firstObject[i].width*0.5, firstObject[i].y + firstObject[i].height*0.5); 

:

void detectAndDisplay(Mat frame) { 
    std::vector<Rect> firstObject; 
    std::vector<Rect> secondObject; 
    std::vector<Rect> thirdObject; 
    Mat frame_gray; 
    cvtColor(frame, frame_gray, CV_BGR2GRAY); 
    equalizeHist(frame_gray, frame_gray); 
    //-- Detect object 
    cascade_one.detectMultiScale(frame_gray, firstObject, 1.1, 2, 0|CV_HAAR_SCALE_IMAGE, Size(30, 30)); 
    for(size_t i = 0; i < firstObject.size(); i++) { 
    Point center(firstObject[i].x + firstObject[i].width*0.5, firstObject[i].y + firstObject[i].height*0.5); 
    ellipse(frame, center, Size(firstObject[i].width*0.5, firstObject[i].height*0.5), 0, 0, 360, Scalar(0, 255, 0), 4, 8, 0); //GREEN 
    std::cout << " " << cascade_name << " " << timeFound() << endl; 
    } 
    //-- detect second object 
    cascade_two.detectMultiScale(frame_gray, secondObject, 1.1, 2, 0 |CV_HAAR_SCALE_IMAGE, Size(30, 30)); 
    for(size_t k = 0; k < secondObject.size(); k++) { 

     Point center(secondObject[k].x + secondObject[k].x + secondObject[k].width*0.5, secondObject[k].y + secondObject[k].y + secondObject[k].height*0.5); 
     int radius = cvRound((secondObject[k].width + secondObject[k].height)*0.25); 
     circle(frame, center, radius, Scalar(255, 0, 0), 4, 8, 0); //BLUE 
     std::cout << " " << nested_cascade_name << " " << timeFound() << endl; 
    } 
    //-- detect third object 
    cascade_three.detectMultiScale(frame_gray, thirdObject, 1.1, 2, 0 |CV_HAAR_SCALE_IMAGE, Size(30, 30)); 

    for(size_t j = 0; j < thirdObject.size(); j++) { 
     Point center(thirdObject[j].x + thirdObject[j].x + thirdObject[j].width*0.5, thirdObject[j].y + thirdObject[j].y + thirdObject[j].height*0.5); 
     int radius = cvRound((thirdObject[j].width + thirdObject[j].height)*0.25); 
     circle(frame, center, radius, Scalar(0, 0, 255), 4, 8, 0); //RED 
     std::cout << " " << nested_cascade_name_two << " " << timeFound() << endl; 
    } 
    imshow(window_name, frame); 
    } 
+0

얼마나 많은 요소가 secondObject 및 thirdObject 벡터에 있습니까? 그들을 그리기위한 코드도 게시 할 수 있습니까? 작동하지 않는다면 여전히 코드에 버그가있을 수 있습니다. 3 가지 검출 단계와 드로잉 단계 모두를 포함하는 전체 코드. – Micka

+0

필자의 질문에 전체 탐지 및 코드 그리기를 포함 시켰습니다. – Colin747

+0

두 번째/세 번째 객체가 감지되었거나 그려지지 않았습니까? 아니면 전혀 감지되지 않았습니까? – Micka

답변

0

문제는 탐지 된 개체의 위치를 ​​읽기/해석하는 동안 발생 두 번째와 세 번째 물체 해석에서 작은 실수를 범했습니다 :

Point center(secondObject[k].x + secondObject[k].x + secondObject[k].width*0.5, secondObject[k].y + secondObject[k].y + secondObject[k].height*0.5); 

secondObject [k] .x + secondObject [k] .x은 감지 된 개체를 이미지에서 "이동"시킬 수 있으며 해당 개체를 해당 위치에 표시하려면 큰 의미가 없습니다. 감지 됨. 당신이 (세 번째 검출기)

Point center(secondObject[k].x + secondObject[k].width*0.5, secondObject[k].y + secondObject[k].y + secondObject[k].height*0.5); 

및하여 라인 교체 할 경우

:

Point center(thirdObject[j].x + thirdObject[j].width*0.5, thirdObject[j].y + thirdObject[j].y + thirdObject[j].height*0.5); 

아무런 문제 될 것이 없다.

관련 문제