# Can Streaming video be used in OpenCV?

**URL:** https://discourse.processing.org/t/can-streaming-video-be-used-in-opencv/6318
**Category:** Libraries
**Created:** [December 5, 2018, 4:22am UTC](https://discourse.processing.org/t/can-streaming-video-be-used-in-opencv/6318 "2018-12-05T04:22:06Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![laptophead](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laptophead/32/2701_2.png) [@laptophead](https://discourse.processing.org/u/laptophead)
#### Post date: [December 5, 2018, 4:22am UTC](https://discourse.processing.org/t/can-streaming-video-be-used-in-opencv/6318/1 "2018-12-05T04:22:06Z")

</div>

I opened the example “backgroundSubstraction” and tried to replace movie file with streaming webcam video but no luck.

this line is the trouble:

```auto
opencv = new OpenCV(this, video);

```

The error says" Width(0) and height (0) cannot be \<=0"

Here is the code I tried:

```auto
import gab.opencv.*;
import processing.video.*;
Capture video;
//Movie video;
OpenCV opencv;

void setup() {
  size(840, 660);
  
   String[] cameras = Capture.list();
  if (cameras == null) {
    println("Failed to retrieve the list of available cameras, will try the default...");
   video = new Capture(this, 800, 600);
  } 
  if (cameras.length == 0) {
    println("There are no cameras available for capture.");
    exit();
  } 
  
  video = new Capture(this, "name=iSight,size=640x480,fps=15");
  video.start();
  //video = new Capture(this, 640, 480);
 // video = new Movie(this, "name=iSight,size=640x480,fps=15");
  opencv = new OpenCV(this, video);
  
  opencv.startBackgroundSubtraction(5, 3, 0.5);
  
  //video.loop();
  //video.play();
}

void captureEvent(Capture video) {
  video.read();
}

void draw() {
  
  video.loadPixels();
  image(video, 0, 0);  
  opencv.loadImage(video);
  
  opencv.updateBackground();
  
  opencv.dilate();
  opencv.erode();

  noFill();
  stroke(255, 0, 0);
  strokeWeight(3);
  for (Contour contour : opencv.findContours()) {
    contour.draw();
  }
}

void movieEvent(Movie m) {
  m.read();
}

```

---

<div class="post-metadata">

### Author: ![Jakub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jakub/32/2192_2.png) [@Jakub](https://discourse.processing.org/u/Jakub)
#### Post date: [December 5, 2018, 8:20am UTC](https://discourse.processing.org/t/can-streaming-video-be-used-in-opencv/6318/2 "2018-12-05T08:20:02Z")

</div>

Video can take some time to initialize. You should use it only when the height and width becomes non zero. You can wait until the video starts and only then create opencv. I didn’t test this code but something like this might work:

```auto
void setup() {
  ...
  // Start video here
  video = new Capture(...);
  ...
}

void captureEvent(Capture video) {
  video.read();
}

void draw() {
  if (opencv == null && video.width != 0 && video.height != 0) {
    // Video is ready
    // Create and config OpenCV here
    opencv = new OpenCV(...);
    ...
  }
  if (opencv != null) {
    // Video is ready and opencv was created
    // your draw code here
    opencv.loadImage(...);
    ...
  }
}

```

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [December 5, 2018, 1:25pm UTC](https://discourse.processing.org/t/can-streaming-video-be-used-in-opencv/6318/3 "2018-12-05T13:25:53Z")

</div>

> [@Jakub](#):
>
> You can wait until the video starts and only then create opencv.

Even better: Move the wait step to **setup()**: 💡

- [How to make the webcam capture window go away or put images over it - Processing 2.x and 3.x Forum](http://Forum.Processing.org/two/discussion/16435/how-to-make-the-webcam-capture-window-go-away-or-put-images-over-it)

```java
// ...
void setup() {
  // ...
  ( video = new Capture(Capture.list()[0]) ).start();
  while (video.height == 0) delay(2);
  opencv = new OpenCV(this, video);
  // ...
}

```

> <https://github.com/processing/processing-video/pull/30#issuecomment-126506248>
>
> Matching canvas' \*\*size()\*\* w/ \*\*Capture\*\*'s \_width\_ & \_height\_ is a good soluti…on IMO.
> Here's a simplified version from Processing's forum:
> http://forum.Processing.org/two/discussion/11528/why-are-the-actual-camera-fps-different-from-captured-fps  
> 
> \`\`\` Processing
> import processing.video.Capture;
> Capture cam;
> 
> static final int FPS\_ADJUST = 5, CAM = 1;
> 
> void setup() {
> initFeed();
> size(cam.width, cam.height, JAVA2D);
> 
> float canvasFPS = cam.frameRate + FPS\_ADJUST;
> frameRate(canvasFPS);
> 
> println("Cam's FPS:", cam.frameRate, "\\t\\tCanvas's FPS:", canvasFPS);
> print("Cam's size:", cam.width, 'x', cam.height, '\\t');
> println("Canvas's size:", width, 'x', height);
> }
> 
> void draw() {
> background(cam);
> frame.setTitle( str(round(frameRate)) );
> }
> 
> void captureEvent(Capture c) {
> c.read();
> }
> 
> void initFeed() {
> if (cam != null) return;
> 
> String\[\] cams = Capture.list();
> printArray(cams);
> println("\\nChosen Cam #" + CAM + ':', cams\[CAM\]);
> 
> ( cam = new Capture(this, cams\[CAM\]) ).start();
> while ( (cam.width & cam.height) == 0 ) delay(5);
> }
> \`\`\`

---

<div class="post-metadata">

### Author: ![Jakub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jakub/32/2192_2.png) [@Jakub](https://discourse.processing.org/u/Jakub)
#### Post date: [December 5, 2018, 3:09pm UTC](https://discourse.processing.org/t/can-streaming-video-be-used-in-opencv/6318/4 "2018-12-05T15:09:03Z")

</div>

@GoToLoop This could work too. However, you might get a crash if you use OpenGL renderer and block setup like this, so I don’t usually recommend it.

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [December 5, 2018, 4:34pm UTC](https://discourse.processing.org/t/can-streaming-video-be-used-in-opencv/6318/5 "2018-12-05T16:34:37Z")

</div>

> [@Jakub](#):
>
> However, you might get a crash if you use OpenGL renderer and block **setup()** like this, …

But OP is using JAVA2D. Nonetheless, I’ve tested all renderers using my ancient sketch “Efficient WebCam Capture” from the link below, and no crashes have happened so far: 🙃

- [How to make the webcam capture window go away or put images over it - Processing 2.x and 3.x Forum](http://Forum.Processing.org/two/discussion/16435/how-to-make-the-webcam-capture-window-go-away-or-put-images-over-it#Item_1)

---

<div class="post-metadata">

### Author: ![neilcsmith](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/neilcsmith/32/144_2.png) [@neilcsmith](https://discourse.processing.org/u/neilcsmith)
#### Post date: [December 5, 2018, 4:42pm UTC](https://discourse.processing.org/t/can-streaming-video-be-used-in-opencv/6318/6 "2018-12-05T16:42:43Z")

</div>

Totally agree with @Jakub and I made the exact same point the last time you advocated this - blocking setup() is a stupid idea for multiple reasons. Just because it works for you, with your camera on your system, doesn’t make it a good idea!

---

<div class="post-metadata">

### Author: ![laptophead](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laptophead/32/2701_2.png) [@laptophead](https://discourse.processing.org/u/laptophead)
#### Post date: [December 5, 2018, 5:55pm UTC](https://discourse.processing.org/t/can-streaming-video-be-used-in-opencv/6318/7 "2018-12-05T17:55:58Z")

</div>

Thank you folks, That worked, I got streaming video.  
However the I dont get contours and the OPENCV module does not work now.

I think it has to do with the function on the botom that referes to Movie, not streaming video.  
What to do?  
Thanks

Here is the code:

```auto
void draw() {

  video.loadPixels();
  image(video, 0, 0);  

  if (opencv == null && video.width != 0 && video.height != 0) 
  {
    opencv = new OpenCV(this, video);
  }
  if (opencv != null) 
  {
    opencv.loadImage(video);
    opencv.startBackgroundSubtraction(5, 3, 0.5);
    opencv.updateBackground();
    opencv.dilate();
    opencv.erode();
    noFill();
    stroke(255, 0, 0);
    strokeWeight(3);
    for (Contour contour : opencv.findContours()) 
    {
      contour.draw();
    }
  }
}

void movieEvent(Movie m) {
  m.read();
}

```

---

<div class="post-metadata">

### Author: ![Jakub](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jakub/32/2192_2.png) [@Jakub](https://discourse.processing.org/u/Jakub)
#### Post date: [December 6, 2018, 5:32pm UTC](https://discourse.processing.org/t/can-streaming-video-be-used-in-opencv/6318/8 "2018-12-06T17:32:30Z")

</div>

For capture you need [captureEvent()](https://processing.org/reference/libraries/video/captureEvent_.html) instead:

```auto
void captureEvent(Capture c) {
  c.read();
}

```

---

<div class="post-metadata">

### Author: ![GoToLoop](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/gotoloop/32/86_2.png) [@GoToLoop](https://discourse.processing.org/u/GoToLoop)
#### Post date: [December 7, 2018, 6:11am UTC](https://discourse.processing.org/t/can-streaming-video-be-used-in-opencv/6318/9 "2018-12-07T06:11:32Z")

</div>

@Jakub. Indeed Processing’s OpenGL renderers crash on setup() if we **delay()** it by 5 seconds! 🥶

Found that out when experimenting on the following hack sketch: 👻

> [@Problems when trying to draw to a P3D PGraphics when called from a thread?](https://discourse.processing.org/t/problems-when-trying-to-draw-to-a-p3d-pgraphics-when-called-from-a-thread/6301/4):
>
> Although it’s pretty much a hack, here’s the workaround I’ve come up w/: crazy_face /\*\* \* Threaded PGraphics II (v1.0.1) \* GoToLoop (2018/Dec/07) \* \* https://Discourse.Processing.org/t/ \* problems-when-trying-to-draw-to-a-p3d-pgraphics- \* when-called-from-a-thread/6301/4 \* \* https://Forum.Processing.org/two/discussion/25799/ \* how-to-draw-a-rect-in-a-thread#Item\_2 \*/ static final int W = 250, H = 200, FPS = 10; Layer layer; void settings() { size(W, H, JAVA2D); smooth(3); } v…

It happened when I’d removed **draw()** and forced an “eternal” loop within **setup()**! 😱

---

<div class="post-metadata">

### Author: ![laptophead](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/laptophead/32/2701_2.png) [@laptophead](https://discourse.processing.org/u/laptophead)
#### Post date: [December 8, 2018, 2:15am UTC](https://discourse.processing.org/t/can-streaming-video-be-used-in-opencv/6318/10 "2018-12-08T02:15:59Z")

</div>

Jackub  
I am using captureEvent already, the video loads fine. There is no background substraction like in the example.  
If i move an object, nothing happens, no countours around it. So it is the OPENCV part that does not kick in,

Any ideas?  
Thanks again
