opencv - How to capture image from video when right button of mouse is clicked in open cv c++ -
i working open cv , facing problem in capturing image video. basic need when video running in open cv ,as click right button of mouse program should capture image video image should saved @ location. after on saved image have perform further image processing. please 1 me.
this simple. need mouse callback, write image, , running clip, therefore code question.
#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: not open 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; }
you need -std=c++11
std::to_string()
. if want save image through gui window, think need library such qt supports functionality.
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
edited: op needs capturing frames webcam rather video.
this code question in comment.
#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 current directory cv::imwrite( fullpath, frame ); std::cout << " image has been saved " << std::endl; ++count; } } int main() { // access default webcam cv::videocapture cap(0); // double check webcam before start reading. if ( !cap.isopened() ){ std::cerr << "cannot open 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 webcam " << std::endl; break; } cv::imshow("webcam", frame); if ( cv::waitkey(30) == 27){ std::cout << "esc key pressed" << std::endl; break; } } return 0; }
the images saved in directory of executable file. in terminal command (i.e. i'm using 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
Comments
Post a Comment