Colors 如何获取网络摄像头提要(Processing/JavaScript)中特定区域的平均颜色?

Colors 如何获取网络摄像头提要(Processing/JavaScript)中特定区域的平均颜色?,colors,processing,average,webcam,Colors,Processing,Average,Webcam,我正在使用Processing从我的笔记本电脑获取网络摄像头。在左上角,我在显示的提要上绘制了一个矩形。我试图得到网络摄像头的平均颜色,但仅限于矩形所包含的区域 我不断得到颜色(0,0,0),黑色,作为结果 谢谢大家! PS抱歉,如果我的代码看起来很凌乱..我在处理方面是新手,所以我不知道这是否很难阅读或包含不良做法。多谢各位 导入处理。视频。*; 捕捉网络摄像头; 捕捉帽; PImage bg_img; 颜色bgColor=color(0,0,0); int rMargin=50; int r

我正在使用Processing从我的笔记本电脑获取网络摄像头。在左上角,我在显示的提要上绘制了一个矩形。我试图得到网络摄像头的平均颜色,但仅限于矩形所包含的区域

我不断得到颜色(0,0,0),黑色,作为结果

谢谢大家!

PS抱歉,如果我的代码看起来很凌乱..我在处理方面是新手,所以我不知道这是否很难阅读或包含不良做法。多谢各位

导入处理。视频。*;
捕捉网络摄像头;
捕捉帽;
PImage bg_img;
颜色bgColor=color(0,0,0);
int rMargin=50;
int rWidth=100;
颜色输入=颜色(0,0,0);
颜色背景=颜色(255、255、255);
色电流;
int-bg公差=5;
无效设置(){
规模(1280720);
//启动网络摄像头
String[]inputs=Capture.list();
如果(inputs.length==0){
println(“无法检测到任何连接的网络摄像头!”);
退出();
}
网络摄像头=新捕获(此,输入[0]);
webcam.start();
}
作废提款(){
if(webcam.available()){
//从网络摄像头读取
webcam.read();
图像(网络摄像头,0,0);
webcam.loadPixels();
noFill();
冲程重量(2);
冲程(25525525255);
rect(rMargin,rMargin,rWidth,rWidth);
int-yCenter=(rWidth/2)+rMargin;
int xCenter=(rWidth/2)+rMargin;
//矩形模式(中心);
int rectCenterIndex=(宽度*yCenter)+xCenter;
int r=0,g=0,b=0;
//对于整个图像:
//对于(int i=0;i>16&0xFF;
//g+=c>>8&0xFF;
//b+=c&0xFF;
//}
//r/=bg_img.pixels.length;
//g/=bg_img.pixels.length;
//b/=bg_img.pixels.length;
//计算平均颜色:
int i;
对于(int x=50;x 16&0xFF;
g+=c>>8&0xFF;
b+=c&0xFF;
}
}
r/=webcam.pixels.length;
g/=webcam.pixels.length;
b/=webcam.pixels.length;
println(r+“”+g+“”+b);
}
}

您非常接近,但忽略了一个重要方面:采样的像素数

请注意,在注释掉的完整图像示例代码中,您将除以完整的像素数(pixels.length)

然而,在您的改编版本中,您只需要计算完整图像的一部分的平均颜色,这意味着像素数量较少

您只对100x100像素的区域进行采样,这意味着您需要除以10000,而不是
webcam.pixels.length
(1920x1000)。这就是为什么您得到0,因为它是整数除法。 这就是我在代码中的意思:

 int totalSampledPixels = rWidth * rWidth;
 r /= totalSampledPixels;
 g /= totalSampledPixels;
 b /= totalSampledPixels;
完全调整的草图:

import processing.video.*;

Capture webcam;
Capture cap;
PImage bg_img;

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

int rMargin = 50;
int rWidth = 100;
int rHeight = 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, 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;

    //for whole image:
    //for (int i=0; i<bg_img.pixels.length; i++) {
    //    color c = bg_img.pixels[i];
    //    r += c>>16&0xFF;
    //    g += c>>8&0xFF;
    //    b += c&0xFF;
    //}
    //r /= bg_img.pixels.length;
    //g /= bg_img.pixels.length;
    //b /= bg_img.pixels.length;

    //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 = webcam.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;

    fill(r,g,b);
    rect(rMargin + rWidth, rMargin, rWidth, rHeight);

    println(r + " " + g + " " + b);
  }
}
导入处理。视频。*;
捕捉网络摄像头;
捕捉帽;
PImage bg_img;
颜色bgColor=color(0,0,0);
int rMargin=50;
int rWidth=100;
int-rHeight=100;
颜色输入=颜色(0,0,0);
颜色背景=颜色(255、255、255);
色电流;
int-bg公差=5;
无效设置(){
规模(1280720);
//启动网络摄像头
String[]inputs=Capture.list();
如果(inputs.length==0){
println(“无法检测到任何连接的网络摄像头!”);
退出();
}
网络摄像头=新捕获(此,输入[0]);
webcam.start();
}
作废提款(){
if(webcam.available()){
//从网络摄像头读取
webcam.read();
图像(网络摄像头,0,0);
webcam.loadPixels();
noFill();
冲程重量(2);
冲程(25525525255);
rect(rMargin、rMargin、rWidth、rHeight);
int-yCenter=(rWidth/2)+rMargin;
int xCenter=(rWidth/2)+rMargin;
//矩形模式(中心);
int rectCenterIndex=(宽度*yCenter)+xCenter;
int r=0,g=0,b=0;
//对于整个图像:
//对于(int i=0;i>16&0xFF;
//g+=c>>8&0xFF;
//b+=c&0xFF;
//}
//r/=bg_img.pixels.length;
//g/=bg_img.pixels.length;
//b/=bg_img.pixels.length;
//计算平均颜色:
int i;
对于(intx=0;x16&0xFF;
g+=c>>8&0xFF;
b+=c&0xFF;
}
}
}

//仅除以采样区域(x>=50&&x=50&&y请发布一个使用硬编码图像而不是相机馈送的图像,这样我们可以运行您的代码,并将其功能与您的预期进行比较。请尝试准确地找出哪一行的行为与您的预期不同。
println()
function是解决此类问题的最佳朋友。哦!我觉得自己太傻了,你完全正确!这解决了它!同时-感谢您提供了顶级的颜色类型…如果我所做的事情看起来不对劲,我会在转换颜色时记住这一点。