I am trying to open a video and write it to a location:
#include <opencv2/opencv.hpp>
#include "opencv2/core/core.hpp"
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "iostream"
using namespace cv;
using namespace std;
int main()
{
string videoName = "KorExp3.avi";
VideoCapture video(videoName);
Mat frame;
video >> frame;
VideoWriter w("D:/w.avi", CV_FOURCC('M', 'P', '4', '2'), 30, frame.size(), true);
while (true) {
video >> frame;
imshow("frame", frame);
w << frame;
}
w.release();
waitKey(0);
return 0;
}
In debug mode, while hovering the mouse on video it says:
Information not available, no symbols loaded for opencv_world340d.dll
I have copied this dll file and the video file to the same location of .exe, but still same thing happens. I also tried the absolute path to the video string videoName = "D:\\KorExp3.avi"; but didn't work.
How can I capture a video and write it to a location using openCV?
Do you generate (compile) OpenCV with debug symbols??
This is a sample (and simple) code to record a video file using OpenCV...
I am using Qt (5.5.1 and upper) on Linux, but this is doesn't matter...
It will work in every OS...
void MainWindow::on_Rec_Click()
{
QString szNome = QString("%1/%2-%3-M.mp4").arg(szPath).arg(szCamIndex).arg(obAgora.toString("yyyyMMddHHmmss"));
qDebug() << szNome;
char szCPath[2048];
strcpy(szCPath, szNome.toStdString().c_str());
qDebug() << "Path: " << szCPath;
MakePath(szCPath, inIndice+1); // If the path does not exist...
SaveEventToDB(szNome, inIndice+1, obAgora, 0); // Register event in DB
qDebug() << m_Capture[inIndice - 1] << " / " << inIndice;
cv::Size S = cv::Size((int) m_Capture[inIndice]->get(CV_CAP_PROP_FRAME_WIDTH), // Acquire input size
(int) m_Capture[inIndice]->get(CV_CAP_PROP_FRAME_HEIGHT));
qDebug() << S.width << " / " << S.height;
int ex = cv::VideoWriter::fourcc('M','P','4','2');
qDebug() << ex;
double dlFrameRate = m_Capture[inIndice]->get(CV_CAP_PROP_FPS);
qDebug() << dlFrameRate;
m_Output = new cv::VideoWriter(szNome.toStdString(), ex, dlFrameRate, S, true);
qDebug() << "Object cv::VideoWriter created.";
m_OutputFile = szNome;
m_inTimerID = startTimer(1000 / dlFrameRate);
}
void MainWindow::timerEvent(QTimerEvent *event)
{
if(m_inActualView != 0) {
cv::Mat image;
*m_Capture[m_inActualView] >> image;
if(m_Output) {
if(m_Output->isOpened()) {
*m_Output << image;
}
}
cv::flip( image,image, 0);
// Show the image
m_Ui->openCVViewer->showImage( image );
}
}
I put the directory of the video instead of moving the video to the project location andthe error was solved:
string videoName = "D:\\KorExp3.avi";
while (true) {
video >> frame;
...
Related
I want to crop a certain area of my raw video before extracting the frames. I don't want to use external cropping video software because I need the video to be raw for processing. Is it possible to crop a certain area before extraction ? I read about ROI for images, is it possible for video too ?
Here's my extraction code that's already working :
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <string>
#include <sstream>
using namespace cv;
using namespace std;
int c = 0;
string int2str(int &);
int main(int argc, char **argv) {
string s;
VideoCapture cap("test_video.mov");
if (!cap.isOpened())
{
cout << "Cannot open the video file" << endl;
return -1;
}
double fps = cap.get(CV_CAP_PROP_FPS);
cout << "Frame per seconds : " << fps << endl;
namedWindow("MyVideo", CV_WINDOW_NORMAL);
resizeWindow("MyVideo", 600, 600);
while (1)
{
Mat frame;
Mat Gray_frame;
bool bSuccess = cap.read(frame);
if (!bSuccess)
{
cout << "Cannot read the frame from video file" << endl;
break;
}
s = int2str(c);
//cout<<("%d\n",frame )<<endl;
c++;
imshow("MyVideo", frame);
imwrite("frame" + s + ".jpg", frame);
if (waitKey(30) == 27)
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;
}
string int2str(int &i) {
string s;
stringstream ss(s);
ss << i;
return ss.str();
}
Still need to crop the video, any advice ?
I'm following a tutorial to extract video frames. I've read this question, it doesn't work, also queationfrom Open CV Answer, but the solution is for capturing current frame. I have a 120fps video and want to extract all of them. Here's my code
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <iostream>
#include <string>
#include <sstream>
using namespace cv;
using namespace std;
int c = 0;
string int2str(int &);
int main(int argc, char **argv) {
string s;
VideoCapture cap("test.mp4"); // video
if (!cap.isOpened())
{
cout << "Cannot open the video file" << endl;
return -1;
}
double fps = cap.get(CV_CAP_PROP_FPS); //get the frames per seconds of the video
cout << "Frame per seconds : " << fps << endl;
namedWindow("MyVideo", CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"
while (1)
{
Mat frame;
Mat Gray_frame;
bool bSuccess = cap.read(frame); // read a new frame from video
if (!bSuccess)
{
cout << "Cannot read the frame from video file" << endl;
break;
}
s = int2str(c);
//cout<<("%d\n",frame )<<endl;
c++;
imshow("MyVideo", frame); //show the frame in "MyVideo" window
imwrite("ig" + s + ".jpg", frame);
if (waitKey(30) == 27) //esc key
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;
}
//int to string function
string int2str(int &i) {
string s;
stringstream ss(s);
ss << i;
return ss.str();
}
My problem is, I have a minute video at 120fps. I expected to be able to extract 120 frames. But the extraction went wrong, the video last for 1 minute but I got more than 200 frames stored in my folder. Did I do something wrong in my code ?
My setup is using a logitech c920, raspberry pi 3, and the latest opencv 3.3. I am showing in screen and writing in a file the camera stream. The only processing I am doing is converting video to grayscale. In the screen all show good, but the file is corrupted (see https://i.stack.imgur.com/QHrQb.png).
Noticeable, the video is well recorded in XVID, and also in MJPG if the original color image is selected instead.
UPDATE: I tested the same code in os-x and the same error happens, also with opencv3.3
Any advice welcome :)
This is the code:
#include "opencv2/opencv.hpp"
#include <iostream>
using namespace std;
using namespace cv;
int main() {
int FPS = 10;
int nframes;
double start, now;
VideoCapture vcap(0);
if(!vcap.isOpened()) {
cout << "Error opening video stream or file" << endl;
return 0;
}
vcap.set(CV_CAP_PROP_FOURCC,CV_FOURCC('M','J','P','G'));
int frame_width = vcap.get(CV_CAP_PROP_FRAME_WIDTH);
int frame_height = vcap.get(CV_CAP_PROP_FRAME_HEIGHT);
VideoWriter video("out.avi",CV_FOURCC('M','J','P','G'), FPS, Size(frame_width,frame_height), false);
namedWindow("Main",CV_WINDOW_AUTOSIZE); //create a window called
// Start time
start = (double)getTickCount();
for(;;) {
Mat frame, gray;
vcap >> frame;
cvtColor(frame, gray, cv::COLOR_BGR2GRAY);
video << gray;
imshow("Main", gray);
char c = (char)waitKey(2);
if( c == 27 ) break;
++nframes;
if (nframes==100) {
now = (double)getTickCount();
cout << "FPS: " << ++nframes/(now-start)*getTickFrequency() << endl;
start = now;
nframes = 0;
}
}
video.release();
return 0;
}
I an working with open cv , I am facing problem in capturing image from video.
My basic need is that when video is running in open cv ,as soon as I click right button of mouse program should capture the image from video then image should be saved at any location. After that on saved image I have to perform further image processing.
Please any one help me.
This is simple. You need mouse callback, write an image, and running a clip, therefore this is my code for your 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: 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;
}
you need -std=c++11 for std::to_string(). If you want to save an image through GUI window, I think you need another library such as Qt that supports this functionality.
$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$
Edited: the OP needs capturing frames from webcam rather than a video.
This is the code for your question in the 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 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;
}
The images are saved in the directory of the executable file. In the terminal command (i.e. I'm using Mac and 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
I am trying to write a program that captures video from a webcam using this code:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
Mat frame;
VideoCapture cap(1);
char key;
String outputName = "output.avi";
VideoWriter outputVideo;
Size size = Size(cap.get(CV_CAP_PROP_FRAME_WIDTH), cap.get(CV_CAP_PROP_FRAME_HEIGHT));
outputVideo.open(outputName, CV_FOURCC('D', 'I', 'V', 'X'), cap.get(CV_CAP_PROP_FPS), size, true);
if (!outputVideo.isOpened())
{
cout << "Could not open the output video for write: " << outputName << endl;
return -1;
}
cout << "size " << size.width << " x " << size.height << endl;
namedWindow("Display window", WINDOW_AUTOSIZE); // Create a window for display.
cap.set(CV_CAP_PROP_EXPOSURE, -8);
while (true){
bool success = cap.read(frame);
if (!success){
cout << "Cannot read frame from file" << endl;
return -2;
}
outputVideo.write(frame);
imshow("Display window", frame);
key = waitKey(1);
if (key == ' '){
cout << "Video ended due to key stroke" << endl;
return 1;
}
}
return 0;
}
The program doesn't seem to be able to open outputVideo since it always returns -1. I thought that I might not have the codec divx installed, but I have installed it from k-lite codec pack and divx and it still does not work.
Could anybody please tell me how to install codecs such that opencv recognizes them?
I am using OpenCV 2.4.10 on Windows 7 with Visual studio 2013.