title: How to get the average color of a specific area in a webcam feed (Processing/JavaScript)?
body: <p>I'm using Processing to get a webcam feed from my laptop. In the top left corner, I have drawn a rectangle over the displayed feed. I'm trying to get the average color of the webcam, but only in the region contained by that rectangle. </p>
  
  <p>I keep getting color (0, 0, 0), black, as the result. </p>
  
  <p>Thank you all!</p>
  
  <p>PS sorry if my code seems messy..I'm new at Processing and so I don't know if this might be hard to read or contain bad practices. Thank you.</p>
  
  
  
  <pre class="lang-java prettyprint-override"><code>import processing.video.*;
  
  Capture webcam;
  Capture cap;
  PImage bg_img;
  
  color bgColor = color(0, 0, 0);
  
  int rMargin = 50;
  int rWidth = 100;
  
  color input = color(0, 0, 0);
  color background = color(255, 255, 255);
  color current;
  int bgTolerance = 5;
  
  void setup() {
    size(1280,720);
  
    // start the webcam
    String[] inputs = Capture.list();
    if (inputs.length == 0) {
      println("Couldn't detect any webcams connected!");
      exit();
    }
    webcam = new Capture(this, inputs[0]);
  
    webcam.start();
  
  }
  
  
  void draw() {
    if (webcam.available()) {
  
      // read from the webcam
      webcam.read();
  
      image(webcam, 0,0);
      webcam.loadPixels();
  
      noFill();
      strokeWeight(2);
      stroke(255,255, 255);
      rect(rMargin, rMargin, rWidth, rWidth);
  
      int yCenter = (rWidth/2) + rMargin;
      int xCenter = (rWidth/2) + rMargin;
      // rectMode(CENTER);
  
      int rectCenterIndex = (width* yCenter) + xCenter;
  
      int r = 0, g = 0, b = 0;
  
      //for whole image:
      //for (int i=0; i&lt;bg_img.pixels.length; i++) {
      //    color c = bg_img.pixels[i];
      //    r += c&gt;&gt;16&amp;0xFF;
      //    g += c&gt;&gt;8&amp;0xFF;
      //    b += c&amp;0xFF;
      //}
      //r /= bg_img.pixels.length;
      //g /= bg_img.pixels.length;
      //b /= bg_img.pixels.length;
  
      //CALCULATE AVG COLOR:
      int i;
  for(int x = 50; x &lt;= 150; x++){
     for(int y = 50; y &lt;= 150; y++){
             i = (width*y) + x;
             color c = webcam.pixels[i];
             r += c&gt;&gt;16&amp;0xFF;
             g += c&gt;&gt;8&amp;0xFF;
             b += c&amp;0xFF;
         }
      }
      r /= webcam.pixels.length;
      g /= webcam.pixels.length;
      b /= webcam.pixels.length;
  
      println(r + " " + g + " " + b);
    }
  }
  </code></pre>