im trying to draw a circle in a video from my webcam i using this function
cv::circle(cap,points(1,0),3,cv::Scalar(255,255,255),-1);
i found it in a document but i don't know why its't work i edit my code many time but its still give my error that's my full code i using opencv3
#include <opencv2/opencv.hpp>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <vector>
#include <iostream>
#include <sstream>
#include <opencv2/video/background_segm.hpp>
#include <opencv2/video/background_segm.hpp>
using namespace cv;
using namespace std;
int main()
{
VideoCapture cap(0); // open the video file for reading
if ( !cap.isOpened() ) // if not success, exit program
{
cout << "Cannot open the video file" << endl;
return -1;
}
//cap.set(CV_CAP_PROP_POS_MSEC, 300); //start the video at 300ms
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;
bool bSuccess = cap.read(frame); // read a new frame from video
if (!bSuccess) //if not success, break loop
{
cout << "Cannot read the frame from video file" << endl;
break;
}
imshow("MyVideo", frame); //show the frame in "MyVideo" window
cv::circle(cap,points(1,0),3,cv::Scalar(255,255,255),-1);
if(waitKey(30) == 27) //wait for 'esc' key press for 30 ms. If 'esc' key is pressed, break loop
{
cout << "esc key is pressed by user" << endl;
break;
}
}
return 0;
}
circle accepts a Mat object, not a VideoCapture object. So you need to draw the circle on frame.
Also you need to show the image after you actually draw the circle.
So replace the imshow / circle part of your code with:
...
cv::circle(frame, points(1,0), 3, cv::Scalar(255,255,255), -1);
imshow("MyVideo", frame);
...
Related
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 ?
I have a question about saving a video with openCV in C++ (I'm using Linux Ubuntu).
I was trying to save a stream from open camera for some time. I finally succeeded, but now I have really not many ideas why the saved stream doesn't have the same duration as I was streaming from my camera. When I am streaming for 10 seconds it has only for example 2-3 seconds and looks like it is accelerated.
Does anybody have some clue what could be the problem? Something wrong in my code or maybe computing performance, maybe the system doesn't save every frame?
Thanks for your help.
My code:
#include <stdio.h>
#include <iostream> // for standard I/O
#include <string> // for strings
#include <opencv2/core.hpp> // Basic OpenCV structures (cv::Mat)
#include <opencv2/videoio.hpp> // Video write
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/opencv.hpp"
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
Mat capture;
VideoCapture cap(0);
if(!cap.isOpened())
{
cout<<"Cannot connect to camera"<<endl;
return -1;
}
namedWindow("Display",CV_WINDOW_AUTOSIZE);
double dWidth = cap.get(CV_CAP_PROP_FRAME_WIDTH);
double dHeight = cap.get(CV_CAP_PROP_FRAME_HEIGHT);
Size frameSize(static_cast<int>(dWidth), static_cast<int>(dHeight));
VideoWriter oVideoWriter ("/home/Stream_video/cpp/Cam/out.avi", CV_FOURCC('P','I','M','1'), 20, frameSize,true);
if ( !oVideoWriter.isOpened() ) {
cout << "ERROR: Failed to write the video" << endl;
return -1;
}
while(true){
Mat frame;
bool bSuccess = cap.read(frame); // read a new frame from video
if (!bSuccess) {
cout << "ERROR: Cannot read a frame from video file" << endl;
break; //if not success, break loop
}
oVideoWriter.write(frame); //writer the frame into the file
imshow("Display", frame);
if (waitKey(10) == 27) {
cout << "esc key is pressed by user" << endl;
break;
}
}
}
I'm working with KTH dataset which contains videos for basic human action. I've tried to subtract the background using opencv-2.4.9 BackgroundSubtractorMOG2, but still getting the shadows in the result.
Here is the code (written in C++) that I'm using:
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/video/background_segm.hpp>
//C
#include <stdio.h>
//C++
#include <iostream>
#include <sstream>
using namespace cv;
using namespace std;
//global variables
Mat frame; //current frame
Mat fgMaskMOG2; //fg mask fg mask generated by MOG2 method
Ptr<BackgroundSubtractor> pMOG2; //MOG2 Background subtractor
int keyboard;
//function declarations
void processVideo(char* videoFilename);
int main(int argc, char* argv[])
{
pMOG2 = new BackgroundSubtractorMOG2(); //MOG2 approach
pMOG2->setInt("nmixtures",3);
pMOG2->setDouble("fTau",0.5);
processVideo("person14_boxing_d1_uncomp.avi");// read the avi file
//destroy GUI windows
destroyAllWindows();
return EXIT_SUCCESS;
}
void processVideo(char* videoFilename) {
//create the capture object
VideoCapture capture(videoFilename);
if(!capture.isOpened()){
//error in opening the video input
cerr << "Unable to open video file: " << videoFilename << endl;
exit(EXIT_FAILURE);
}
//read input data. ESC or 'q' for quitting
while( (char)keyboard != 'q' && (char)keyboard != 27 ){
//read the current frame
if(!capture.read(frame)) {
cerr << "Unable to read next frame." << endl;
cerr << "Exiting..." << endl;
//exit(EXIT_FAILURE);
return;
}
pMOG2->operator()(frame, fgMaskMOG2, 0.1
imshow("FG Mask MOG 2", fgMaskMOG2);
//get the input from the keyboard
keyboard = waitKey( 30 );
}
//delete capture object
capture.release();
}
So please help me to change the code so that I can get a better result.
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
I tried the following code for capturing a video from my webcam:
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace cv;
using namespace std;
int main()
{
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("MyVideo", CV_WINDOW_AUTOSIZE); //create a window called "MyVideo"
namedWindow("Changed", CV_WINDOW_AUTOSIZE);
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;
}
Mat imgH = frame + Scalar(75, 75, 75);
imshow("MyVideo", frame); //show the frame in "MyVideo" window
imshow("Changed", imgH);
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;
}
Now here's my problem:
After debugging that program for the first time everything works as expected. But when debugging for a second time (after changing some lines in the code) it cannot read from the camera.
Does anyone have a hint for me how to solve that problem?
Thanks!
The code you posted seems to be working absolutely fine in my case, and the output is as intended.
However please make sure that your webcam is switched on before you run the program, this is important.
Since i have a YouCam client in my computer for the webcam, therefore it shows that i need to start youcam.
Since i dont have enough reputation to post an image, so please see the following link in order to view the output i got when webcam not already switched on.
http://i.imgur.com/h4bTZ7z.png
Hope this helps!!