【问题标题】:How can we set different `colors` for different objects detected using YOLO我们如何为使用 YOLO 检测到的不同对象设置不同的“颜色”
【发布时间】:2019-09-10 06:10:27
【问题描述】:

我们如何为使用 YOLO 检测到的不同对象设置不同的colors。现在检测到的所有内容都显示在绿色矩形中。

function draw() {
    image(video, 0, 0, width, height); // Displaying image on a canvas
    for (let i = 0; i < objects.length; i++)  //Iterating through all objects
    {
        noStroke();
        fill(0, 255, 0); //Color of text
        text(objects[i].label, objects[i].x * width, objects[i].y * height - 5); 
        //Displaying the label
        noFill();
        strokeWeight(4); 
        stroke(0, 255, 0); //Defining stroke for rectangular outline
        rect(objects[i].x * width, objects[i].y * height, objects[i].w * width, 
        objects[i].h * height);
    }
}

【问题讨论】:

    标签: javascript processing p5.js yolo


    【解决方案1】:

    stroke() 设置全局状态,它设置用于在形状周围绘制线条和边框的颜色。这种状态甚至会在帧之外保持。
    这意味着在调用 stroke() 之后绘制的所有对象都使用设置的颜色绘制。
    如果你想改变颜色,你必须再次调用stroke()

    如果您想使用不同的颜色,您可以定义颜色列表并使用modulo operator (%) 获取颜色列表的索引。例如使用红色、绿色和蓝色:

    colorList = [[255, 0, 0], [0, 255, 0], [0, 0, 255]];
    
    function draw() {
    
        // [...]
    
    
        for (let i = 0; i < objects.length; i++) {
    
            // [...]
    
            let colorI = i % colorList.length;
            stroke(...colorList[colorI]);
    
            // [...]
        }
    }
    

    【讨论】:

    • 好的,我也更新了我的问题中的代码。但我找不到如何为不同的对象应用差异颜色
    • @soccerway 我已经扩展了答案
    • 我会尽快回复你的
    • 太棒了,现在我可以更改对象的颜色了。
    猜你喜欢
    • 2020-02-04
    • 2020-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-01
    相关资源
    最近更新 更多