我是一个开放的cv工作,我面临从视频捕获图像的问题。 我的基本需求是,当视频在打开的cv中运行时,只要单击鼠标程序的右键应从视频捕捉图像,然后图像应保存在任何位置。之后,在保存的图像上,我必须执行进一步的图像处理。 请任何人帮助我。如何从视频捕获图像时,鼠标右键单击打开cv c + +
回答
这很简单。你需要鼠标回调,写一个图像,并运行一个剪辑,因此这是我的代码为您的问题。
#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++11
为std::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
此代码发生错误。 to_string,错误多个重载函数实例“to string”匹配参数列表:@CroCo –
std :: C++ 11如何获得std :: to_string @CroCo –
@ShaikIbrahim,您正在使用哪种操作系统?在Linux中,'g ++ -std = C++ 11' – CroCo
- 1. 如何从ContextMenu打开模态(单击鼠标右键)?
- 2. 谷歌地图如何打开和关闭双击鼠标右键单击V3
- 3. 允许单击鼠标左键单击鼠标右键或按Ctrl键单击打开对话框
- 4. 如何捕获右键单击事件?
- 5. 如何从鼠标悬停图像点击开始YouTube视频点击?
- 6. 用鼠标右键单击一个图像,禁用其他图像点击
- 7. 禁用鼠标右键单击图像控件VB6
- 8. Datagrid +鼠标右键单击事件
- 9. 当我右键单击鼠标不显示在新标签菜单中打开
- 10. 右键单击Webkit视图
- 11. 用鼠标右键单击NSStatusItem
- 12. 从Kinect视频图像获取鼠标坐标
- 13. Google地图,如何捕获POI上的右键单击事件
- 14. 捕获鼠标左键点击
- 15. 捕获右键单击HTML DIV
- 16. 右键单击文本捕获
- 17. 只能从“在新标签中打开链接”右键单击
- 18. 打开cv C++:如何分割视频几个部分?
- 19. clearInterval同时单击鼠标左键和右键不起作用
- 20. 捕获Web浏览器控件的鼠标右键单击事件
- 21. 如何更改鼠标左键单击和右键单击选项?
- 22. 用鼠标右键单击然后打开工程,但简单的左键单击并不是
- 23. 在Excel VBA中单击鼠标左键捕获单元值
- 24. VS2010:鼠标右键单击“在Expression Blend中打开”:不支持的项目
- 25. 捕获鼠标光标图标C++
- 26. 如何使用getMouse捕获右键单击事件()
- 27. 如何捕获鼠标点击Gtk.Stack
- 28. C#鼠标右键单击和控制焦点
- 29. C#,mouseClickEvent不响应鼠标右键单击
- 30. 如何禁用文本框内的鼠标右键单击
OpenCV支持鼠标回调。对于视频,它是逐帧读取的,因此,创建一个存储当前帧的全局变量,然后您就可以开始了。 – CroCo