# Threading on image processing

**URL:** https://discourse.processing.org/t/threading-on-image-processing/8439
**Category:** Libraries
**Created:** [February 16, 2019, 6:32am UTC](https://discourse.processing.org/t/threading-on-image-processing/8439 "2019-02-16T06:32:14Z")
**Posts on this page:** 4
**Page:** 1

<div class="post-metadata">

### Author: ![doni](https://avatars.discourse-cdn.com/v4/letter/d/a698b9/32.png) [@doni](https://discourse.processing.org/u/doni)
#### Post date: [February 16, 2019, 6:32am UTC](https://discourse.processing.org/t/threading-on-image-processing/8439/1 "2019-02-16T06:32:14Z")

</div>

hi i try to add another thread to improve fps or might be rendering process but my code do not succed

```auto
import processing.video.*;

Capture video;

void setup() {
  size(640, 480);
  // Uses the default video input, see the reference if this causes an error
  video = new Capture(this, width, height);ot
  video.start();  
  noStroke();
  smooth();
}

void draw() {
  if (video.available()) {
    video.read();
    image(video, 0, 0, width, height); // Draw the webcam video onto the screen
    int brightestX = 0; // X-coordinate of the brightest video pixel
    int brightestY = 0; // Y-coordinate of the brightest video pixel
    float brightestValue = 0; // Brightness of the brightest video pixel
    // Search for the brightest pixel: For each row of pixels in the video image and
    // for each pixel in the yth row, compute each pixel's index in the video
    video.loadPixels();
    int index = 0;
    for (int y = 0; y < video.height; y++) {
      for (int x = 0; x < video.width; x++) {
        // Get the color stored in the pixel
        int pixelValue = video.pixels[index];
        // Determine the brightness of the pixel
        float pixelBrightness = brightness(pixelValue);
        // If that value is brighter than any previous, then store the
        // brightness of that pixel, as well as its (x,y) location
        if (pixelBrightness > brightestValue) {
          brightestValue = pixelBrightness;
          brightestY = y;
          brightestX = x;
        }
        index++;
      }
    }
thread("elipse");
  }
}

void elipse (){
    // Draw a large, yellow circle at the brightest pixel
    fill(255, 204, 0, 128);
    ellipse(brightestX, brightestY, 200, 200);
}

```

---

<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 16, 2019, 10:04am UTC](https://discourse.processing.org/t/threading-on-image-processing/8439/2 "2019-02-16T10:04:59Z")

</div>

You cannot use thread to draw things. And you shouldn’t set off a thread every frame. Even if this worked it would decrease framerate not increase it.

---

<div class="post-metadata">

### Author: ![jeremydouglass](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/jeremydouglass/32/20_2.png) [@jeremydouglass](https://discourse.processing.org/u/jeremydouglass)
#### Post date: [February 20, 2019, 6:45pm UTC](https://discourse.processing.org/t/threading-on-image-processing/8439/3 "2019-02-20T18:45:30Z")

</div>

> [@neilcsmith](#):
>
> You cannot use thread to draw things.

@doni – for more on _why_, read the reference page for `thread()`:

> You cannot draw to the screen from a function called by **thread()**. Because it runs independently, the code will not be synchronized to the animation thread, causing strange or at least inconsistent results.  
> [thread() / Reference / Processing.org](https://processing.org/reference/thread_.html)

---

<div class="post-metadata">

### Author: ![doni](https://avatars.discourse-cdn.com/v4/letter/d/a698b9/32.png) [@doni](https://discourse.processing.org/u/doni)
#### Post date: [March 14, 2019, 12:45pm UTC](https://discourse.processing.org/t/threading-on-image-processing/8439/4 "2019-03-14T12:45:20Z")

</div>

@neilcsmith @jeremydouglass. what part of my code you suggest me to run on a thread. thank you
