# Image/movie frame/capture frame pixel\[\] scan

**URL:** https://discourse.processing.org/t/image-movie-frame-capture-frame-pixel-scan/13103
**Category:** Libraries
**Created:** [July 31, 2019, 5:35pm UTC](https://discourse.processing.org/t/image-movie-frame-capture-frame-pixel-scan/13103 "2019-07-31T17:35:17Z")
**Posts on this page:** 2
**Page:** 1

<div class="post-metadata">

### Author: ![markcosmic](https://avatars.discourse-cdn.com/v4/letter/m/ecc23a/32.png) [@markcosmic](https://discourse.processing.org/u/markcosmic)
#### Post date: [July 31, 2019, 5:35pm UTC](https://discourse.processing.org/t/image-movie-frame-capture-frame-pixel-scan/13103/1 "2019-07-31T17:35:17Z")

</div>

This pertains to image/movie frame/capture frame pixel[] scan.  
In the Coding Train examples Dan uses a nested FOR loop to check RGB values of each pixel. Within each loop the x/y coordinates are converted to pixel[] coordinates and then tested.  
Could you folks have a look at this algorithm. I’m using a single FOR loop. It test pixel[] values and if it test TRUE only then does it calculate the x/y coordinates. I think it’s more streamline/efficient than the nested FOR loop which converts the x/y coordinates to pixel[] coordinates each loop. What do you think? I modified the “sketch\_11\_5\_AveragePixelColorTracking” sketch with the code below. I am new to Processing 3, but not new to coding.  
Thank you all for the knowledge you’ve given me.

```auto
// Begin loop to walk through every pixel
  for (int pix = 0; pix < video.pixels.length -1; pix++ ) { //1 for loop only
      // What is current color
      color currentColor = video.pixels[pix];
      float r1 = red(currentColor);
      float g1 = green(currentColor);
      float b1 = blue(currentColor);
      float r2 = red(trackColor);
      float g2 = green(trackColor);
      float b2 = blue(trackColor);

      float d = distSq(r1, g1, b1, r2, g2, b2);
      // x and y are only calculated when needed
      if (d < threshold*threshold) {
        int y = floor(pix/video.width);
        int x = pix - (video.width * y);
        stroke(255);
        strokeWeight(1);
        point(x, y);
        avgX += x;
        avgY += y;
        count++;
      }

  }

```

---

<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: [August 5, 2019, 5:50am UTC](https://discourse.processing.org/t/image-movie-frame-capture-frame-pixel-scan/13103/2 "2019-08-05T05:50:49Z")

</div>

> [@markcosmic](#):
>
> It test pixel values and if it test TRUE only then does it calculate the x/y coordinates

Great – that is a good strategy for efficiency, as is looking over the pixels array rather than using get – this is discussed here:

> “Getting the color of a single pixel with **get(x, y)** is easy, but not as fast as grabbing the data directly from **pixels[]**” [get() / Reference / Processing.org](https://processing.org/reference/get_.html)

You don’t usually need to use point() (I don’t think?) or set() – unless you point has a non-normal strokeWeight – you can just use `pixels[y*width+x]`. [set() / Reference / Processing.org](https://processing.org/reference/set_.html)

If you want to further optimize for speed, don’t use red() / green() / blue() – instead, use bit-shifting.

> “The **red()** function is easy to use and understand, but it is slower than a technique called bit shifting.” [red() / Reference / Processing.org](https://processing.org/reference/red_.html)

Coding Train code tends to be written for conceptual clarity – for teaching and learning – rather than for performance.
