2015-04-22 46 views
-3

我是一個開放的cv工作,我面臨從視頻捕獲圖像的問題。 我的基本需求是,當視頻在打開的cv中運行時,只要單擊鼠標程序的右鍵應從視頻捕捉圖像,然後圖像應保存在任何位置。之後,在保存的圖像上,我必須執行進一步的圖像處理。 請任何人幫助我。如何從視頻捕獲圖像時,鼠標右鍵單擊打開cv c + +

+0

OpenCV支持鼠標回調。對於視頻,它是逐幀讀取的,因此,創建一個存儲當前幀的全局變量,然後您就可以開始了。 – CroCo

回答

0

這很簡單。你需要鼠標回調,寫一個圖像,並運行一個剪輯,因此這是我的代碼爲您的問題。

#include <iostream> 
#include <string> 
#include <sstream> 
#include <opencv2/core/core.hpp> 
#include <opencv2/highgui/highgui.hpp> 



cv::Mat currentFrame; 

static void onMouse(int event, int x, int y, int, void*) 
{ 
    static int count(0); 
    if (event == cv::EVENT_RBUTTONDOWN) { 

     std::stringstream ss; 
     ss << count; 
     std::string countStr = ss.str(); 
     std::string imageName = "image_" + countStr; 
     std::string FullPath = "/home/xxxx/" + imageName + ".jpg"; 
     cv::imwrite(FullPath, currentFrame); 
     std::cout << " image has been saved " << std::endl; 
     ++count; 
    } 
} 

int main() 
{ 

    std::string clipFullPath = "/home/xxxx/drop.avi"; 
    cv::VideoCapture clip(clipFullPath);  

    if (!clip.isOpened()){ 
     std::cout << "Error: Could not open the video: " << std::endl; 
     return -1; 
    } 


    cv::namedWindow("Display", CV_WINDOW_AUTOSIZE); 
    cv::setMouseCallback("Display", onMouse, 0); 

    for(;;){ 

     clip >> currentFrame; 

     if (currentFrame.empty()) 
       break;   

     cv::imshow("Display", currentFrame); 
     cv::waitKey(30); 

    } 
    return 0; 
} 

需要-std=c++11std::to_string()。如果你想通過GUI窗口保存圖像,我認爲你需要另外一個庫,比如支持這個功能的Qt。

$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ $$$$$$$$$$$$$$$$$$$$$$$$$

編輯:OP需要從網絡攝像頭而不是視頻捕捉幀。

這是您的問題在評論中的代碼。

#include <iostream> 
#include <cstdlib> 
#include <string> 
#include <sstream> 
#include "opencv2/core/core.hpp" 
#include "opencv2/highgui/highgui.hpp" 


cv::Mat frame; 

static void onMouse(int event, int x, int y, int, void*) 
{ 
    static int count(0); 
    if (event == cv::EVENT_RBUTTONDOWN) { 

     std::stringstream ss; 
     ss << count; 
     std::string countStr = ss.str(); 
     std::string imageName = "image_" + countStr; 
     std::string FullPath = imageName + ".jpg"; // save to the current directory 
     cv::imwrite(FullPath, frame); 
     std::cout << " image has been saved " << std::endl; 
     ++count; 
    } 
} 


int main() 
{ 
    // access the default webcam 
    cv::VideoCapture cap(0); 

    // Double check the webcam before start reading. 
    if (!cap.isOpened()){ 
     std::cerr << "Cannot open the webcam " << std::endl; 
     exit (EXIT_FAILURE); 
    } 

    cv::namedWindow("webcam",CV_WINDOW_AUTOSIZE); 
    cv::setMouseCallback("webcam", onMouse, 0); 

    while (true){ 

     // acquire frame 
     cap >> frame; 

     // Safety checking 
     if (!frame.data){ 
     std::cerr << "Cannot acquire frame from the webcam " << std::endl; 
      break; 
     } 

     cv::imshow("webcam", frame); 

     if (cv::waitKey(30) == 27){ 
     std::cout << "esc key is pressed" << std::endl; 
     break; 
     } 
    } 

    return 0; 
} 

圖像保存在可執行文件的目錄中。在終端命令(即我正在使用Mac和OpenCV 2.4.11)

g++ main.cpp -o main -I/opt/local/include -L/opt/local/lib -lopencv_highgui.2.4.11 -lopencv_core.2.4.11 
+0

此代碼發生錯誤。 to_string,錯誤多個重載函數實例「to string」匹配參數列表:@CroCo –

+0

std :: C++ 11如何獲得std :: to_string @CroCo –

+0

@ShaikIbrahim,您正在使用哪種操作系統?在Linux中,'g ++ -std = C++ 11' – CroCo

相關問題