[Solved]Windows+OpenCV+Webcam
in
Contributed Library Questions
•
1 year ago
Hi guys,
I was on a workshop today (Ole Kristensen's "Making your computer see"),
and i was trying to run the OpenCV examples on Processing 2.0a5 and it didn't work.
We noticed that there's a problem related to webcam capture on Windows using OpenCV+Processing.
The Mac users didn't have any problem.
I've googled it and noticed that a lot of Windows users had the same problem.
So here's the thing: I got it working with a workaround.
It's not a highly elaborated fix, but it's enough to work with the webcam, so I want to share this with you.
It's REALLY just a workaround, i've never programmed in Processing before so i'm pretty sure that there may be more clever solutions. I'm running a Windows 7 64-bit laptop with Processing 2.0a5 and OpenCV 1.0.
Well, first of all, if you haven't done it yet, you have to configure your opencv+processing environment according to
the official page . Download the code samples as well.
Try to run the blobs example. If it gives you a Null pointer exception due to the webcam, you got the same problem as me.
So, instead of using OpenCV video capture, you can use Processing to capture the video and redirect its output to OpenCV. I'll share what i have done to solve for the blobs example, and this approach can be used to the other OpenCV samples (i've tested it).
All you have to do is
- import processing.video.*;
- Capture video;
before the setup function,
on the setup function, instead of using
- opencv.capture()
you do
- video = new Capture(this, w, h);
- video.start();
- // Make the pixels[] array available for direct manipulation
- loadPixels();
- opencv.allocate(video.width,video.height); // create the bufer
- opencv.copy(video);
This opencv.copy function is responsible for copying your webcam image to the openCV buffer.
Now the draw function:
- void draw() {
- if (video.available()) {
- video.read(); // Read a new video frame
- video.loadPixels(); // Make the pixels of video available
- // Difference between the current frame and the stored background
- // opencv.allocate(video.width,video.height); //uncomment this to make it colored, but you'll have to adapt the remember function
- opencv.copy(video);
- background(0);
- opencv.read();
and the rest of the function is the same as the original blob example. Remember to close the new brace, introduced by that
if.
If you need to use opencv.remember, replace it with
- opencv.remember(OpenCV.BUFFER);
So you're telling OpenCV to remember not an image from the source camera, but an image from its buffer.
And that's it, we're good to go!
My code, which is running completely fine on my computer, can be downloaded here:
http://w3.impa.br/~tknop/tmp/blobs_win7.zip
Feel free to use this code as you want.
Someone could expect a performance loss, since we're copying this buffer every time, but still, this workaround provided a good frame rate here.
Hope it helps someone. For those who come up with better solutions or questions, feel free to comment!
Cheers!
Tassio Knop
3