# Getting error message on Raspberry Pi V2 Camera Colour Detection code

**URL:** <https://discourse.processing.org/t/getting-error-message-on-raspberry-pi-v2-camera-colour-detection-code/16064>\
**Category:** Processing for Pi\
**Created:** [December 3, 2019, 2:37am UTC](https://discourse.processing.org/t/getting-error-message-on-raspberry-pi-v2-camera-colour-detection-code/16064 "2019-12-03T02:37:28Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Treedom](https://avatars.discourse-cdn.com/v4/letter/t/5f9b8f/32.png) [@Treedom](https://discourse.processing.org/u/Treedom)\
**Post date:** [December 3, 2019, 2:37am UTC](https://discourse.processing.org/t/getting-error-message-on-raspberry-pi-v2-camera-colour-detection-code/16064/1 "2019-12-03T02:37:28Z")

</div>

Hi, I’m using a Raspberry pi with a V2 camera and am running this code and getting the error message “ArrayIndexOutOfBoundsException: 64001”. I have ran this code on a mac computer and I do not get this error. It seems to only be happening when I run it on the Pi. Any suggestions/help

```auto

import gohai.glvideo.*;
GLCapture video;

GLCapture cap;
PImage bg_img;

color bgColor = color(0, 0, 0);

int rMargin = 1;
int rWidth = 320;
int rHeight = 200;

color input = color(0, 0, 0);
color background = color(255, 255, 255);
color current;
int bgTolerance = 5;

PImage myImage;

void setup() {
  size(320, 200, P2D);

  // start the webcam
  String[] devices = GLCapture.list();
  println("Devices:");
  printArray(devices);
  if (0 < devices.length) {
    String[] configs = GLCapture.configs(devices[0]);
    println("Configs:");
    printArray(configs);
  }
  video = new GLCapture(this);
  video.start();
  myImage = loadImage("ALL final.jpg");
}

void draw() {
  if (video.available()) {
    video.read();

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

    noFill();
    rect(rMargin, rMargin, rWidth, rHeight);

    int yCenter = (rWidth/2) + rMargin;
    int xCenter = (rWidth/2) + rMargin;
    // rectMode(CENTER);

    int rectCenterIndex = (width* yCenter) + xCenter;

    int r = 0, g = 0, b = 0;

    //CALCULATE AVG COLOR:
    int i;
    for (int x = 0; x <= width; x++) {
      for (int y = 0; y <= height; y++) {
        if (x >= rMargin && x <= rMargin + rWidth && y >= rMargin && y <= rMargin + rHeight) {

          i = (width*y) + x;
          color c = video.pixels[i];
          r += c>>16&0xFF;
          g += c>>8&0xFF;
          b += c&0xFF;
        }
      }
    }
    //divide by just the area sampled (x >= 50 && x <= 150 && y >= 50 && y <= 150 is a 100x100 px area) 
    int totalSampledPixels = rWidth * rHeight;

    r /= totalSampledPixels;
    g /= totalSampledPixels;
    b /= totalSampledPixels;

    if ((r<128) || (r>133)) {
      image(myImage, 0, 0);
    }

    if ((g<118) || (g>123)) {
      image(myImage, 0, 0);
    }

    if ((b<111) || (b>115)) {
      image(myImage, 0, 0);
    }

    println(r + " " + g + " " + b);
  }
}

```

---

<div class="post-metadata">

**Author:** ![kll](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.processing.org/kll/32/964_2.png) [@kll](https://discourse.processing.org/u/kll)\
**Post date:** [December 3, 2019, 4:02am UTC](https://discourse.processing.org/t/getting-error-message-on-raspberry-pi-v2-camera-colour-detection-code/16064/2 "2019-12-03T04:02:57Z")

</div>

> [@Treedom](#):
>
> x \<= width

i would use only

```auto
for (int x = 0; x < width; x++) {
      for (int y = 0; y < height; y++) {

```

so for me the question is why it works on MAC
