如何在p5.js中跟踪两种颜色?

时间:2019-05-08 20:24:40

标签: javascript webcam p5.js

首先,这是我有史以来的第一篇文章,对不起,我是菜鸟和不好的代码格式!

我的项目基于跟踪3D打印的刷新图标-一种橙色和一种蓝色。我希望两者都被跟踪(椭圆将显示)。我正在使用p5.js(即使将来我想使用openCV)。

我已经成功跟踪了橙色图标!根据我的理解,当我查看像素阵列以识别颜色时,我还需要遍历目标阵列(以便它可以容纳多种颜色)。

//code that tracks the orange

var video;
var target;

// XY coordinate of closest color
var closestX = 0;
var closestY = 0;

var threshold = 25;

function setup() {
    createCanvas(640, 480);
    pixelDensity(1);
    video = createCapture(VIDEO);
    video.hide();
    noStroke();

    target = new TargetColor(color(255,127,80));
}

function draw() {
    background(0);
    image(video, 0, 0);
    video.loadPixels();

    var worldRecord = 500;


    for (var x = 0; x < video.width; x += 1){
        for (var y = 0; y < video.height; y += 1) {

            var index = (x + (y * video.width)) * 4;
            var redSource = video.pixels[index + 0];
            var greenSource = video.pixels[index + 1];
            var blueSource = video.pixels[index + 2];

            var d = dist(redSource, greenSource, blueSource, target.red,
                target.green, target.blue);


            if (d < worldRecord) {
                worldRecord = d;
                closestX = x;
                closestY = y;
            }
        }
    }

    if (worldRecord < 10) {

        //draw ellipse

        fill(target.rgb);
        strokeWeight(4);
        stroke(0);
        ellipse(closestX, closestY, 16, 16);
    }  
}  
//snippet of code that should allow multiple color tracking

//TWO - COLOR TRACKING

var video;
var target = [] // Empty array for targets so we can have more than 1
"object-colors"

var particles = [] //empty array of particles

// XY coordinate of closest color
var closestX = 0;
var closestY = 0;

var threshold = 25;

function setup() {
    createCanvas(640, 480);
    pixelDensity(1);
    video = createCapture(VIDEO);
    video.hide();
    noStroke();

    //pushing an object of type Target Color on the target array

    target.push(TargetColor(color(255,127,80)));
}

function draw() {
    background(0);
    image(video, 0, 0);
    video.loadPixels();   

    var worldRecord = 500;

    //For loop - Access each individual pixel in video by iterating 
    through array


    //for loop that allows us to loop over the contents of the array - targets


    for (var i = 0; i < target.length; i++){
        for (var x = 0; x < video.width; x += 1){
            for (var y = 0; y < video.height; y += 1) {

            var index = (x + (y * video.width)) * 4;
            var redSource = video.pixels[index + 0];
            var greenSource = video.pixels[index + 1];
            var blueSource = video.pixels[index + 2];

            var d = dist(redSource, greenSource, blueSource, target[i].red,
                target[i].green, target[i].blue);


            if (d < worldRecord) {
                worldRecord = d;
                closestX = x;
                closestY = y;
                }
            }
        }
    }

    if (worldRecord < 10) {

        //draw ellipse

        fill(target.rgb);
        strokeWeight(4);
        stroke(0);
        ellipse(closestX, closestY, 16, 16);
    } 
}


function mousePressed() {
    // Save color where the mouse is clicked in target variable
    target = new TargetColor(video.get(mouseX, mouseY));
}

function TargetColor(_color){
    this.rgb = _color;
    this.red = red(_color);
    this.green = green(_color);
    this.blue = blue(_color);
}

我在嵌套循环的几个不同位置放置了目标的for循环,但我仍然无法弄清楚。我得到的结果是黑屏,显示“未捕获的类型错误:无法读取未定义的属性”红色”。

但是,它是否尚未定义?我想念什么?

我将非常感谢您提出的任何提示和方法!

0 个答案:

没有答案