2015-04-22 75 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

相关问题