2013-04-24 5 views
5

Ich bin neu in OpenCV und ich möchte eine bestimmte Region in dem Video/Bild für die Erkennung auswählen. In meinem Fall möchte ich Autos erkennen, die nur auf der Straße, nicht auf dem Parkplatz sind.Auswahl einer Region OpenCV

Antwort

8

Nun, die Auswahl von Autos erfordert die Verwendung von Trainingsdaten. Aber einen ROI zu wählen (Region of Interest) ist ziemlich einfach:

Betrachten img = cv2.imread(image)

In diesem Fall irgendwo in Ihrem Code, können Sie eine Region auf diese Weise angeben:

sub_image = img[y:y+h, x:x+w] 

Das wird Erhalten Sie die ROI, sobald Sie die Werte angeben, natürlich nicht mit 'x' oder 'y', wobei h die Höhe und w die Breite ist. Denken Sie daran, dass Bilder nur 2D-Matrizen sind.

Verwenden Sie CascadeClassifier(), um das/die Auto (s) aus den Bildern auszuwählen. Dokumentation gefunden here. OpenCV enthält Trainingsdaten, mit denen Sie Klassifikationen in Form von XML-Dateien erstellen können.

7

Wenn Sie eine Region of Interest (ROI) manuell auswählen möchten, um etwas daran zu bearbeiten, können Sie mit einem Mausklick-Ereignis die Start- und Stopppunkte Ihrer ROI auswählen.

Sobald Sie Start- und Endpunkt haben, können Sie damit das Bild aus der ausgewählten Region abrufen.

Das kann auf Bild oder Video Frame erfolgen.

bool roi_captured = false; 
Point pt1, pt2; 
Mat cap_img; 
//Callback for mousclick event, the x-y coordinate of mouse button-up and button-down 
//are stored in two points pt1, pt2. 
void mouse_click(int event, int x, int y, int flags, void *param) 
{ 

    switch(event) 
    { 
     case CV_EVENT_LBUTTONDOWN: 
     { 
      std::cout<<"Mouse Pressed"<<std::endl; 

      if(!roi_capture) 
      { 
       pt1.x = x; 
       pt1.y = y; 
      } 
      else 
      { 
       std::cout<<"ROI Already Acquired"<<std::endl; 
      } 
     break; 
     } 
     case CV_EVENT_LBUTTONUP: 
     { 
      if(!got_roi) 
     { 
      Mat cl; 
      std::cout<<"Mouse LBUTTON Released"<<std::endl; 

      pt2.x = x; 
      pt2.y = y; 
      cl = cap_img.clone(); 
      Mat roi(cl, Rect(pt1, pt2)); 
      Mat prev_imgT = roi.clone(); 
      std::cout<<"PT1"<<pt1.x<<", "<<pt1.y<<std::endl; 
      std::cout<<"PT2"<<pt2.x<<","<<pt2.y<<std::endl; 

      imshow("Clone",cl); 

      got_roi = true; 
     } 
     else 
     { 
      std::cout<<"ROI Already Acquired"<<std::endl; 
     } 
     break; 
     } 

    } 

} 
//In main open video and wait for roi event to complete by the use. 
// You capture roi in pt1 and pt2 you can use the same coordinates for processing // //subsequent frame 
int main(int argc, char *argv[]) 
{ 
    int frame_num = 0; 
    int non_decode_frame =0; 
    int count = 1, idx =0; 
    int frame_pos =0; 

    std::cout<<"Video File "<<argv[1]<<std::endl; 

    cv::VideoCapture input_video(argv[1]); 

    namedWindow("My_Win",1); 

    cvSetMouseCallback("My_Win", mouse_click, 0); 

    sleep(1); 

    while(input_video.grab()) 
    { 
     cap_img.release(); 

     if(input_video.retrieve(cap_img)) 
     { 
      imshow("My_Win", cap_img); 
      if(!got_roi) 
      { 
          //Wait here till user select the desire ROI 
       waitKey(0); 
      } 
      else 
      { 
       std::cout<<"Got ROI disp prev and curr image"<<std::endl; 
       std::cout<<"PT1"<<pt1.x<<" "<<pt1.y<<std::endl; 
       std::cout<<"PT2"<<pt2.x<<" "<<pt2.y<<std::endl; 

       Mat curr_img_t1; 
       Mat roi2(cap_img,Rect(pt1, pt2)); 
       Mat curr_imgT = roi2.clone(); 
       cvtColor(curr_imgT, curr_img_t1, CV_RGB2GRAY); 

        imshow("curr_img", curr_img); 

      // Do remaining processing here on capture roi for every frame 
           waitKey(1); 
         } 
        } 
} 
}