# Send video frame into OpenCV?

**URL:** https://discourse.processing.org/t/send-video-frame-into-opencv/18075
**Category:** Libraries
**Created:** [February 24, 2020, 5:34pm UTC](https://discourse.processing.org/t/send-video-frame-into-opencv/18075 "2020-02-24T17:34:20Z")
**Posts on this page:** 10
**Page:** 1

<div class="post-metadata">

### Author: ![jeffthompson](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeffthompson/32/743_2.png) [@jeffthompson](https://discourse.processing.org/u/jeffthompson)
#### Post date: [February 24, 2020, 5:34pm UTC](https://discourse.processing.org/t/send-video-frame-into-opencv/18075/1 "2020-02-24T17:34:20Z")

</div>

I’m trying to read the frame of a video and send it into OpenCV for blob detection, but am getting a strange error.

With this simple example:

```auto
import processing.video.*;

Movie mov;
OpenCV cv;

void setup() {
  size(640, 360);

  mov = new Movie(this, sketchPath("transit.mov"));   
  mov.play();
  mov.jump(0);

  cv = new OpenCV(this, mov.width, mov.height);
}

void draw() {
  if (mov.available()) {
    mov.read();
    cv.loadImage(mov);
    cv.threshold(150);
    image(cv.getOutput(), 0, 0);
  }
}

```

I get the error (three times, then it quits):

```auto
IndexOutOfBoundsException: Index: 3, Size: 0
IndexOutOfBoundsException: Index: 3, Size: 0

```

The video is from the Video library example, so I know it plays ok in Processing. Help!

---

<div class="post-metadata">

### Author: ![20203](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/20203/32/8164_2.png) [@20203](https://discourse.processing.org/u/20203)
#### Post date: [February 25, 2020, 11:57pm UTC](https://discourse.processing.org/t/send-video-frame-into-opencv/18075/2 "2020-02-25T23:57:16Z")

</div>

Which line is the error coming from? Do you have a .mov file called “transit.mov”?

---

<div class="post-metadata">

### Author: ![jeffthompson](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeffthompson/32/743_2.png) [@jeffthompson](https://discourse.processing.org/u/jeffthompson)
#### Post date: [February 26, 2020, 1:22am UTC](https://discourse.processing.org/t/send-video-frame-into-opencv/18075/3 "2020-02-26T01:22:58Z")

</div>

Sorry – it’s on the `cv.loadImage(mov);` line. Definitely have the video and it loads/plays fine. Crashes only when I try to pass the frame into OpenCV.

---

<div class="post-metadata">

### Author: ![jeffthompson](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeffthompson/32/743_2.png) [@jeffthompson](https://discourse.processing.org/u/jeffthompson)
#### Post date: [February 26, 2020, 8:16pm UTC](https://discourse.processing.org/t/send-video-frame-into-opencv/18075/4 "2020-02-26T20:16:27Z")

</div>

Ok, I got this to work but it’s a total hack:

```auto
import gab.opencv.*;
import processing.video.*;

Movie mov;
OpenCV cv;

void setup() {
  size(640, 360);

  mov = new Movie(this, sketchPath("transit.mov"));   
  mov.play();
  mov.jump(0);
  mov.loop();

  cv = new OpenCV(this, mov.width, mov.height);
}

void draw() {
  if (mov.available()) {
    mov.read();
    
    // display the frame onscreen
    image(mov, 0,0);

    // then pass it to OpenCV using get()
    cv = new OpenCV(this, get(0,0,width,height));
    
    cv.threshold(150);
    image(cv.getOutput(), 0, 0);
  }
}

```

---

<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: [February 27, 2020, 1:05am UTC](https://discourse.processing.org/t/send-video-frame-into-opencv/18075/5 "2020-02-27T01:05:45Z")

</div>

> [@jeffthompson](#):
>
> … but it’s a total hack:

- For a better performance you can use a separate cloned PImage.
- Then use **arrayCopy()** to transfer the library’s PImage to the cloned 1:
- [arrayCopy() / Reference / Processing.org](http://Processing.org/reference/arrayCopy_.html)
- I believe it’s safe now to pass the cloned PImage to the OpenCV instance.
- Take a look at this example sketch below using Capture w/ **arrayCopy()** and a separate PImage:

```auto
// https://Discourse.Processing.org/t/send-video-frame-into-opencv/18075/5
// GoToLoop 2020-Feb-26

import processing.video.Capture;

Capture cam;
PImage frame = new PImage();

static final int CAM = 1, DELAY = 5;

void setup() {
  size(640, 480);
  initFeed();
  println("Cam's size:", cam.width, 'x', cam.height);
}

void draw() {
  set(0, 0, frame);
  getSurface().setTitle( str(round(frameRate)) );
}

void captureEvent(final Capture c) {
  c.read();

  if (frame.width == c.width) {
    c.loadPixels();
    arrayCopy(c.pixels, frame.pixels);
    frame.updatePixels();
  } else frame = c.get();
}

void initFeed() {
  final String[] cams = Capture.list();
  printArray(cams);
  println("\nChosen Cam #" + CAM + ':', cams[CAM], ENTER);

  ( cam = new Capture(this, cams[CAM]) ).start();

  println("cam.width: " + cam.width);
  while (cam.width == 0) delay(DELAY);
  println("cam.width: " + cam.width + ENTER);

  if (cam.width > 0) getSurface().setSize(cam.width, cam.height);
}

```

---

<div class="post-metadata">

### Author: ![jeffthompson](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeffthompson/32/743_2.png) [@jeffthompson](https://discourse.processing.org/u/jeffthompson)
#### Post date: [February 27, 2020, 1:28pm UTC](https://discourse.processing.org/t/send-video-frame-into-opencv/18075/6 "2020-02-27T13:28:59Z")

</div>

Thanks! I’m actually not seeing any improvement in performance here, which is surprising. This sample video (from the `Video` library) is `640 x 360` but I’m still getting 60fps on both.

Would still be interested to know if this is an OpenCV or Video lib issue.

---

<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: [February 27, 2020, 1:33pm UTC](https://discourse.processing.org/t/send-video-frame-into-opencv/18075/7 "2020-02-27T13:33:43Z")

</div>

> [@jeffthompson](#):
>
> Would still be interested to know if this is an OpenCV or Video lib issue.

- You need to consider that the video library is constantly mutating its _pixels[]_ internal array.
- Therefore passing its reference to another library can be problematic depending on what the other library does to it.
- Passing instead a vanilla PImage clone to OpenCV seems like an excellent workaround IMO.

---

<div class="post-metadata">

### Author: ![jeffthompson](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeffthompson/32/743_2.png) [@jeffthompson](https://discourse.processing.org/u/jeffthompson)
#### Post date: [February 27, 2020, 1:44pm UTC](https://discourse.processing.org/t/send-video-frame-into-opencv/18075/8 "2020-02-27T13:44:45Z")

</div>

Totally – just weird because you can easily pass a `Capture` object from the same lib with no issues.

---

<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: [February 27, 2020, 1:49pm UTC](https://discourse.processing.org/t/send-video-frame-into-opencv/18075/9 "2020-02-27T13:49:28Z")

</div>

> [@jeffthompson](#):
>
> … you can easily pass a Capture object from the same lib with no issues.

Is it true? In my posted example I’m passing the cloned PImage _frame_ as **image()**'s argument in place of the original Capture _cam_.

The idea here is that you can use the same strategy when invoking OpenCV::**loadImage()**.

---

<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: [February 27, 2020, 4:02pm UTC](https://discourse.processing.org/t/send-video-frame-into-opencv/18075/10 "2020-02-27T16:02:52Z")

</div>

> [@GoToLoop](#):
>
> You need to consider that the video library is constantly mutating its _pixels[]_ internal array.

No it isn’t.

> [@GoToLoop](#):
>
> println(“Cam’s size:”, cam.width, ‘x’, cam.height);

This on the other hand is more interesting. The width and height of the Movie are probably going to be 0 when the `cv = new OpenCV(this, mov.width, mov.height);` line executes. Try outputting them. If so, maybe try -

```auto
 if (mov.available()) {
    mov.read();
    if (cv == null) {
        cv = new OpenCV(this, mov.width, mov.height);
    }

```
