繪圖

2013-01-10 41 views
0

我試圖借鑑基於鼠標事件幀中的矩形,問題是,我不能這樣做時,我得到了一個視頻蒸,程序崩潰這裏是我的代碼:繪圖

cv::Rect theBox; 
    bool drawing_box = false; 

    void draw_box(Mat* frame, cv::Rect rectangle){ 
cvRectangle(frame, cvPoint(theBox.x, theBox.y), cvPoint heBox.x+theBox.width,theBox.y+theBox.height), 
      cvScalar(0xff,0x00,0x00)); 

} 空隙鼠標(INT事件,INT的x,INT Y,詮釋標誌,無效*參數){

Mat* image = (Mat*) parameter; 

switch(event){ 
    case CV_EVENT_MOUSEMOVE: 
     if(drawing_box){ 
      theBox.width = x-theBox.x; 
      theBox.height = y-theBox.y; 
     } 
     break; 

    case CV_EVENT_LBUTTONDOWN: 
     drawing_box = true; 
     theBox = cvRect(x, y, 0, 0); 
     break; 

    case CV_EVENT_LBUTTONUP: 
     drawing_box = false; 
     if(theBox.width < 0){ 
      theBox.x += theBox.width; 
      theBox.width *= -1; 
     } 
     if(theBox.height < 0){ 
      theBox.y += theBox.height; 
      theBox.height *= -1; 
     } 
     draw_box(image, theBox); 
     //cv::rectangle(parameter, theBox, CV_RGB(255,0,0)); 
     break; 
} 

}

int main(){ 

    cv::Mat frame; 
    int key = 0 ; 
    const char* name = "box"; 
    theBox = Rect(-1,-1,0,0); 
    cv::VideoCapture cap(1); 

    if (!cap.isOpened()){ 
     cout<< " capture isn't open " << endl; 
     return -1; 
    } 

    cv::namedWindow(name); 

    while (key != 27){ 

     cap >> frame; 
     cv::imshow(name,frame); 
     cv::setMouseCallback(name,mouse,&frame); 

     key = cv::waitKey(1); 
    } 

    cv::destroyAllWindows(); 

return 0; 

}

回答

0

當視頻流正在發生顯示器得到更新...並在同一時間試圖繪製一個矩形很難...因爲矩形的投入的基礎上描繪鼠標移動和鼠標移動獨立於流視頻幀...因此矩形可能會被覆蓋(儘管不應該崩潰)。您可以嘗試在視頻幀顯示的每次迭代中在視頻幀上繪製透明(基於鼠標移動事件的大小..)屏幕...