Using the code below, I am trying to create an app in C++ with OpenCV + Raspicam. This app should stream video in real time from the RasPi camera model connected to my Pi to an Xwindow.
I get the following error on compile:
videofeed.cpp: In function ‘int main(int, char**)’:
videofeed.cpp:36:37: error: ‘cv::imread’ is not a member of ‘raspicam::RaspiCam_Cv’
How do I remedy this?
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/videoio.hpp"
#include <iostream>
#include </home/pi/raspicam-0.1.3/src/raspicam_cv.h>
using namespace std;
int main ( int argc,char **argv ) {
raspicam::RaspiCam_Cv Camera;
cv::Mat image;
int nCount=100;
//set camera params
Camera.set( CV_CAP_PROP_FORMAT, CV_8UC1 );
//Open camera
cout<<"Opening Camera..."<<endl;
if (!Camera.open())// if not success, exit program
{
cout << "Cannot open the video cam" << endl;
return -1;
}
double dWidth = Camera.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames o$
double dHeight = Camera.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frame$
cout << "Frame size : " << dWidth << " x " << dHeight << endl;
cv::namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"
while (1)
{
cv::Mat frame;
bool bSuccess = Camera.cv::imread(frame); // get a new frame from camera
if (!bSuccess) //if not success, break loop
{
cout << "Cannot read a frame from video stream" << endl;
break;
}
cv::imshow("MyVideo", frame); //show the frame in "MyVideo" window
if (cv::waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' ke$
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;
}
///////////////////////////////////////////////////////////////////////////////////
bool bSuccess = Camera.cv::imread(frame); is an error,there is no imreadfunciton in raspicam::RaspiCam_Cv.If you want grub a frame,you can use function grub and than retrieve .
For example:
Mat img;
camera.grab();
camera.retrieve(img);
You can see the declaration:
/**
* Grabs the next frame from video file or capturing device.
*/
bool grab();
/**
*Decodes and returns the grabbed video frame.
*/
void retrieve ( cv::Mat& image );
Also ,you can get example in:https://github.com/cedricve/raspicam
Related
I want to record selected frames as multiple videos from webcam. I tried the following code to start recording a video on a key press and stop recording that video with a different key press. I want to record multiple such videos. But the recorded video files are empty. I can run its equivalent Python code successfully, but I want the same in C++. Can you please help me to correct my mistake?
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
int main(){
// Create a VideoCapture object and use camera to capture the video
VideoCapture cap(0);
// Check if camera opened successfully
if(!cap.isOpened()){
cout << "Error opening video stream" << endl;
return -1;
}
// Default resolutions of the frame are obtained.
int frame_width = cap.get(cv::CAP_PROP_FRAME_WIDTH);
int frame_height = cap.get(cv::CAP_PROP_FRAME_HEIGHT);
bool recording = false;
int videono = 1;
VideoWriter video("dummy.avi", cv::VideoWriter::fourcc('M','J','P','G'), 10, Size(frame_width,frame_height));
video.release();
while(1)
{
Mat frame;
// Capture frame-by-frame
cap >> frame;
// If the frame is empty, break immediately
if (frame.empty())
break;
// Display the resulting frame
imshow( "Frame", frame );
// Press ESC on keyboard to exit
char c = (char)waitKey(1);
if( c == 27 )
break;
// Press s on keyboard to start recording
if( c == 115 and !recording)
{
char path[100];
sprintf(path, "%d.avi", videono);
std::cout << "recording started for " << path << "\n";
videono += 1;
VideoWriter video(path, cv::VideoWriter::fourcc('M','J','P','G'), 10, Size(frame_width,frame_height));
recording = true;
}
if( recording )
video.write(frame);
// Press x on keyboard to stop recording
if( c == 120)
{
std::cout << "recording finished.\n";
recording = false;
video.release();
}
}
// release the video capture and write object
cap.release();
// Closes all the frames
destroyAllWindows();
return 0;
}
Instead of re-creating a new VideoWriter every time and deleting it immediately like this
{
VideoWriter video(path, cv::VideoWriter::fourcc('M','J','P','G'), 10, Size(frame_width,frame_height));
} // <-- deleted here, because it's going out of scope
You should just use the 'open' function on the existing VideoWriter.
So something like this:
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
using namespace std;
using namespace cv;
int main(){
// Create a VideoCapture object and use camera to capture the video
VideoCapture cap(0);
// Check if camera opened successfully
if(!cap.isOpened()){
cout << "Error opening video stream" << endl;
return -1;
}
// Default resolutions of the frame are obtained.
int frame_width = cap.get(cv::CAP_PROP_FRAME_WIDTH);
int frame_height = cap.get(cv::CAP_PROP_FRAME_HEIGHT);
bool recording = false;
int videono = 1;
VideoWriter video;
while(1)
{
Mat frame;
// Capture frame-by-frame
cap >> frame;
// If the frame is empty, break immediately
if (frame.empty())
break;
// Display the resulting frame
imshow( "Frame", frame );
char c = (char)waitKey(1);
if( c == 27 ) // Press ESC on keyboard to exit
break;
if( c == 115 and !recording) // Press s on keyboard to start recording
{
char path[100];
sprintf(path, "%d.avi", videono);
std::cout << "recording started for " << path << "\n";
videono += 1;
video.open(path, cv::VideoWriter::fourcc('M','J','P','G'), 10, Size(frame_width,frame_height));
recording = true;
}
if( recording )
video.write(frame);
if( c == 'x') // Press x on keyboard to stop recording
{
std::cout << "recording finished.\n";
recording = false;
video.release();
}
}
cap.release();// release the video capture and write object
destroyAllWindows(); // Closes all the frames
return 0;
}
I am using OpenCV 2.4.8 and Visual Studio 2013 to run the following simple VideoCapture program. The program is intended to capture the video from the webcam and display it.
However, the program works fine only for the FIRST TIME (after signing in windows), and doesn't work second time.
The problem I get on debugging is :
After executing this line - "bool bSuccess = cap.read(frame);" frame variable is still NULL.
#include "stdafx.h"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
char key;
int main(int argc, char* argv[])
{
VideoCapture cap(0); // open the video camera no. 0
if(!cap.isOpened()) // if not success, exit program
{
cout << "Cannot open the video file" << endl;
return -1;
}
double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video
cout << "Frame size : " << dWidth << " x " << dHeight << endl;
namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"
Mat frame;
while(1)
{
bool bSuccess = cap.read(frame); // read a new frame from video
if (!bSuccess) //if not success, break loop
{
cout << "Cannot read a frame from video file" << endl;
break;
}
imshow("MyVideo", frame); //show the frame in "MyVideo" window
if(waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;
}
This happen because the camera is not correctly closed after first instance of program. You should try to close the console by esc button and not by clicking X.
Could you try and read more than a single frame before breaking the loop? This may be similar to this problem, where a corrupted first frame / slow camera set up was the only problem.
Unable to read frames from VideoCapture from secondary webcam with OpenCV
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char* argv[])
{
VideoCapture cap(0); // open the video camera no. 0
if (!cap.isOpened()) // if not success, exit program
{
cout << "ERROR: Cannot open the video file" << endl;
return -1;
}
namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"
double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video
cout << "Frame Size = " << dWidth << "x" << dHeight << endl;
Size frameSize(static_cast<int>(dWidth), static_cast<int>(dHeight));
VideoWriter oVideoWriter ("D:/MyVideo.avi", CV_FOURCC('P','I','M','1'), 20, frameSize, true); //initialize the VideoWriter object
if ( !oVideoWriter.isOpened() ) //if not initialize the VideoWriter successfully, exit the program
{
cout << "ERROR: Failed to write the video" << endl;
return -1;
}
while (1)
{
Mat frame;
bool bSuccess = cap.read(frame); // read a new frame from video
if (!bSuccess) //if not success, break loop
{
cout << "ERROR: Cannot read a frame from video file" << endl;
break;
}
oVideoWriter.write(frame); //writer the frame into the file
imshow("MyVideo", frame); //show the frame in "MyVideo" window
if (waitKey(10) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;
}
I am developing this project using Visual Studio 2012 express and opencv_2.4.9 and windows 7 x64.
But, this code is not working.
Just write out this message "ERROR: Failed to write the video".
Use try, catch to see if you can get a more detailed error message.
One thing I remember when writing videos is that you need to verify you have the codecs installed and in the correct directory or added as a PATH environment variable.
opencv_ffmpeg.dll was the issue for me, and making sure it was in the correct directory and being renamed to opencv_ffmpeg249.dll
I have a comma separated values file with x & y coordinates. I need to place a dot/circle over opencv video from camera and track with x y coordinates from comma separated values file. I have code to display camera:
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char* argv[])
{
VideoCapture cap(0); // open the video camera no. 0
if (!cap.isOpened()) // if not success, exit program
{
cout << "Cannot open the video cam" << endl;
return -1;
}
double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH); //get the width of frames of the video
double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT); //get the height of frames of the video
cout << "Frame size : " << dWidth << " x " << dHeight << endl;
namedWindow("CameraDisplay",CV_WINDOW_AUTOSIZE); //create a window called "CameraDisplay"
while (1)
{
Mat frame;
bool bSuccess = cap.read(frame); // read a new frame from video
if (!bSuccess) //if not success, break loop
{
cout << "Cannot read a frame from video stream" << endl;
break;
}
imshow("CameraDisplay", frame); //show the frame in "CameraDisplay" window
if (waitKey(30) == 27) //wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;
}
The frame captured by your webcam is nothing but a single image. So, you can put a circle at your webcam video in the same manner as you do with your single image.
The following is the openCV method to place a circle at your image:
void circle(Mat& img, Point center, int radius, const Scalar& color, int thickness=1, int lineType=8, int shift=0)
For example:
circle(frame, Point2i(x-coordinate, y-coordinate), 5, Scalar(0,125,230), 4, 3);
I am trying to run a opencv program. I have configured opencv accordingly but I am getting the Visual Studio 2012 error "The application was unable to start correctly (0xc0000007b)."
Following is the code I am trying to run.
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int argc, char* argv[])
{
VideoCapture cap(0); // open the video camera no. 0
if (!cap.isOpened()) // if not success, exit program
{
cout << "Cannot open the video file" << endl;
return -1;
}
//get the width of frames of the video
double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH);
//get the height of frames of the video
double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT);
cout << "Frame size : " << dWidth << " x " << dHeight << endl;
namedWindow("MyVideo",CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"
while (1)
{
Mat frame;
bool bSuccess = cap.read(frame); // read a new frame from video
if (!bSuccess) //if not success, break loop
{
cout << "Cannot read a frame from video file" << endl;
break;
}
imshow("MyVideo", frame); //show the frame in "MyVideo" window
//wait for 'esc' key press for 30ms. If 'esc' key is pressed, break loop
if (waitKey(30) == 27)
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;
}
You are probably not including all of your libraries properly. Use Dependency Walker to check whether you are missing anything.