I’m looking for a simple way to use videoinput from OpenCv, because I read a lot of material from the net but still I’m very confused.
I’ve read that from release 2.3 it is part of OpenCv.
What does it mean?
I included “highgui.hpp” and declared “videoInput VI” (with HAVE_DSHOW
HAVE_VIDEOINPUT defined), but got error 'videoInput' not identified.
Then I included a "videoinput.h" file I found in the web and I got
“error LNK2001: simbolo esterno "public: __thiscall videoInput::~videoInput(void)" (??1videoInput##QAE#XZ) non risolto”
Have I to download libraries as well?
I thought it should be simpler given that it is “part of OpenCv”.
If you want to read and process the video frames I recommend you the VideoCapture class.
Here is an example with an edge detector:
#include "opencv2/opencv.hpp"
using namespace cv;
int main(int, char**)
{
VideoCapture cap("videofilename"); // open the video file
if(!cap.isOpened()) // check if we succeeded
return -1;
Mat edges;
namedWindow("edges",1);
for(;;)
{
Mat frame;
cap >> frame; // get a new frame from the video
cvtColor(frame, edges, CV_BGR2GRAY);
GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
Canny(edges, edges, 0, 30, 3);
imshow("edges", edges);
if(waitKey(30) >= 0) break;
}
return 0;
}
Related
I have a problem I can not open camera 0 of my pc
here is the code that I use:
#include "opencv2/opencv.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main(int, char**)
{
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) { // check if we succeeded
cout << "cannot open camera "<< endl;
return -1;
}
Mat edges;
namedWindow("edges",1);
for(;;){
Mat frame;
cap >> frame; // get a new frame from camera
cvtColor(frame, edges, COLOR_BGR2GRAY);
GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
Canny(edges, edges, 0, 30, 3);
imshow("edges", edges);
if(waitKey(30) >= 0) break;
}
return 0;
}
it is displaying
can not open camera
because isOpened returns false
Please make sure that the camera is correctly detected. You can do so by executing:
$ls /dev/video*
I have also found that other programs give you a more detailed output e.g. ffmpeg with V4L
Execute e.g.
ffmpeg -f v4l2 -i /dev/video0 -vf scale=640:480 -r 20 -t 00:00:10 output.mp4
and it will tell you if the source is busy or why the camera can't be opened, while OpenCV just returns false.
I am doing a project of Automatic fabric defect detection. In this i developed the algorithm using the [FFT][1] (Fast Fourier Transform) and its working fine in my Ubuntu 14.04 opencv c++. But now i want to develop this to real time there i have to capture image every 2s and have to process that image with my developed algorithm. I need ideas on how to capture images using webcam in opencv c++ and to process withat same image which is being captured. Please do help me if anyone knows of this. Thank you in advance.
You can follow the guidance which has given by OpenCV - They have provided enough examples such as following sample code. Following code is provided by OpenCV Dev team as sample.
#include "opencv2/opencv.hpp"
using namespace cv;
int main(int, char**)
{
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
return -1;
Mat edges;
namedWindow("edges",1);
for(;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
cvtColor(frame, edges, CV_BGR2GRAY);
GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
Canny(edges, edges, 0, 30, 3);
imshow("edges", edges);
if(waitKey(30) >= 0) break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
I need to access the pixel data from a video camera attached to my Windows PC in real time. Once accessed, I will modify it and output it as part of the video stream. In other words, I need to find the easiest way to modify a video stream in real time. I know about OpenCV and Matlab functionality, but I am wondering if anyone has found a simpler way to do this.
If you want to do this with C++, OpenCV, as long as it works with your camera, is one of the simplest ways there is. The code below is from the OpenCV documentation VideoCapture. The only trick is instantiating the VideoCapture instance. How much simpler can it be?
#include "opencv2/opencv.hpp"
using namespace cv;
int main(int, char**)
{
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
return -1;
Mat edges;
namedWindow("edges",1);
for(;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
cvtColor(frame, edges, COLOR_BGR2GRAY);
GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
Canny(edges, edges, 0, 30, 3);
imshow("edges", edges);
if(waitKey(30) >= 0) break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
There is even a python version at Capture Video from Camera that looks very similar to the C++ version above.
This is my code which I copy/paste from here:
#include "opencv2/opencv.hpp"
using namespace cv;
int main(int, char**)
{
VideoCapture cap(0); // open the default camera
if(!cap.isOpened()) // check if we succeeded
return -1;
Mat edges;
namedWindow("edges",1);
for(;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
cvtColor(frame, edges, CV_BGR2GRAY);
GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
Canny(edges, edges, 0, 30, 3);
imshow("edges", edges);
if(waitKey(30) >= 0) break;
}
// the camera will be deinitialized automatically in VideoCapture destructor
return 0;
}
But I get this error:
OpenCV error: Assertion failed (scn==3 || scn==4)
in unknown function, file ..\..\..\..\opencv\modules\imgproc\src\color.cpp, line 3737
I am using Windows 7 x64, Visual Studio 2008, OpenCV 2.4.7
What can be the problem?
EDIT:
It is sometimes it works, sometimes it does not.
EDIT 2:
I edited VideoCapture cap(0); to cv::VideoCapture cap(0); then, I rebuild my solution and run it. It worked for the first time, I tried to run for the second time, it gave me the same error.
EDIT 3:
I have even edited for(;;):
for(;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
imshow("edges", frame);
if(waitKey(30) >= 0) break;
}
This time I receive another error:
OpenCV error: Assertion failed (size.width>0 && size.height>0)
in unknown function, file ..\..\..\..\opencv\modules\highgui\src\window.cpp, line 261
I guess the problem is with imshow.
I get similar problem. I solve it by putting everything after cap >> frame into an if-statement:
for(;;)
{
Mat frame;
cap >> frame; // get a new frame from camera
if (!frame.empty()) {
cvtColor(frame, edges, CV_BGR2GRAY);
GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
Canny(edges, edges, 0, 30, 3);
imshow("edges", edges);
}
if(waitKey(30) >= 0) break;
}
I tested your code in my environment (Win XP 32 bit OS, VS2008, OpenCV2.4.7). It works normally every time. And you can also do it like this:
IplImage* frame,*edges;
CvCapture* pcapture = cvCreateCameraCapture(0);
cvNamedWindow("edges",CV_WINDOW_AUTOSIZE);
while (1)
{
frame = cvQueryFrame(pcapture);
if (!frame) break;
edges = cvCreateImage(cvGetSize(frame),8,1);
cvCvtColor(frame, edges, CV_BGR2GRAY);
cvSmooth(edges,edges,CV_GAUSSIAN,7,7,1.5,1.5);
cvCanny(edges,edges,0,30,3);
cvShowImage("edges",edges);
cvReleaseImage(&edges);
if (cvWaitKey(30)>=0) break;
}
cvReleaseCapture(&pcapture);
cvDestroyWindow("edges");
You can have a try whether its also have some problems in your environment or not.
Hope a little help to you!
I need a program to capture pictures from multiple webcams and save them automatically in Windows Vista. I got the basic code from this link. The code runs in Window XP, but when I tried using it on Vista it says "failed." Different errors pop up every time it is executed. Would it help if I used the SDK platform? Does anyone have any suggestions?
I can't test this on multiple webcams since I only have one, but I'm sure OpenCV2.0 should be able to handle it. Here's some sample code (I use Vista) with one webcam to get you started.
#include <cv.h>
#include <highgui.h>
using namespace cv;
int main()
{
// Start capturing on camera 0
VideoCapture cap(0);
if(!cap.isOpened()) return -1;
// This matrix will store the edges of the captured frame
Mat edges;
namedWindow("edges",1);
for(;;)
{
// Acquire the frame from cap into frame
Mat frame;
cap >> frame;
// Now, find the edges by converting to grayscale, blurring and then Canny edge detection
cvtColor(frame, edges, CV_BGR2GRAY);
GaussianBlur(edges, edges, Size(7,7), 1.5, 1.5);
Canny(edges, edges, 0, 30, 3);
// Display the edges and the frame
imshow("edges", edges);
imshow("frame", frame);
// Terminate by pressing a key
if(waitKey(30) >= 0) break;
}
return 0;
}
Note:
The matrix edges is allocated during
the first frame processing and unless
the resolution will suddenly change,
the same buffer will be reused for
every next frame’s edge map.
As you can see, the code is quite clean and readable! I lifted this from the OpenCV 2.0 documentation (opencv.pdf).
The code not only displays the image from the webcam (under frame) but also does real-time edge detection! Here's a screenshot when I pointed the webcam at my monitor :)
screenshot http://img245.imageshack.us/img245/5014/scrq.png
If you want code to just display the frames from one camera:
#include <cv.h>
#include <highgui.h>
using namespace cv;
int main()
{
VideoCapture cap(0);
if(!cap.isOpened()) return -1;
for(;;)
{
Mat frame;
cap >> frame;
imshow("frame", frame);
if(waitKey(30) >= 0) break;
}
return 0;
}
If the program works with UAC off or when running administrator, make sure the place you choose to save the results are in writable places like the user's my documents folder. Generally speaking root folders and the program files folder is read only for normal users.