Actually, Live Capture example is good for capturing images, so I am using it to capture images and save them in a folder.
#include <fstream>
#include <string>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/imgproc/imgproc.hpp>
int main()
{
std::stringstream file; // to write the file name
cv::VideoCapture cap(0); // create a capture object
int counter = 0; // Create counter
while(true) // infinite loop
{
cv::Mat frame; // Create a object
cap.read(frame); // read the frame
file << "/home/user/path_to_your_folder/image" << counter << ".jpg"; // file name
cv::imwrite(file.str(), frame);
counter++; // increment the counter
}
return 0;
}