【问题标题】:How to detect only single color such as Red, Blue or Green from an image using Java or Processing?如何使用 Java 或 Processing 从图像中仅检测红色、蓝色或绿色等单一颜色?
【发布时间】:2013-09-04 12:48:42
【问题描述】:

我正在从事一个机器人项目,在该项目中我必须进行一些图像处理才能识别蓝色物体、红色障碍物和绿色目的地。我正在使用 java 进行图像处理。

现在,我已经能够使用 Blobscanner 库定位红色、绿色和蓝色的对象。但困难在于,我的算法只有在背景是纯黑色时才能正常工作。因为我使用的是 RGB 颜色模型,并且在 RGB 中,黑色表示为 0,0,0,白色表示为 255,255,255,灰色也有一些红色分量,所以它也被算法检测到。我不知道完全确定红色忽略其他颜色的算法。

请帮助我在任何图像中仅检测红色(及其其他色调)。

【问题讨论】:

  • 请告诉我算法
  • 也许您需要检查组件之间的关系,如果 R 至少比 G 和 B 大 THAT AMOUNT,那么这不是白色或黄色或洋红色...

标签: image processing robotics


【解决方案1】:

好吧,虽然@geotheroy 正在发帖,但我也试了一下,它很有效,而且看起来也很酷:) 所以我还是要发布它......同样的基本想法......

垂直拖动设置阈值,任意键查看原图。

PImage original, result;
float t = 0.9;


void setup() {
  //image linked from this question in processing forum
  //http://forum.processing.org/topic/help-random-distribution-of-non-overlapping-circles#25080000001787197

  original = loadImage("http://24.media.tumblr.com/tumblr_lzi0y7OpsC1r87izio1_1280.png");
  if (original != null) {
    size(original.width, original.height);
    result = createImage(original.width, original.height, RGB); 
    result = original.get(0, 0, original.width, original.height);
  }
  else
  {
    println("unable to load the image. Are you connected?");
    exit();
  }
}

void draw() {


  if (keyPressed) {
    image (original, 0, 0);
  }
  else {
    image(result, 0, 0);
  }
}

void mouseDragged() {
  t = map(mouseY, 0, height, 0, 1);
  findReds(original, t);
}


void findReds(PImage orig, float thresh) {
  result = orig.get(0, 0, orig.width, orig.height);
  result.loadPixels();
  for (int i = 0; i < result.pixels.length; i++) { 
    color c = result.pixels[i];
    float r = c >> 16 & 0xFF;
    float g = c >> 8 & 0xFF;
    float b = c & 0xFF;

    float limitR = r - r*thresh;
    if ( g < limitR && b < limitR) {
      result.pixels[i] = color(255);
    }
  }
  result.updatePixels();
}

【讨论】:

    【解决方案2】:

    这是否有助于给你一些想法:

    PImage moog;
    
    void setup() {
      String url = "http://bananamondaes.files.wordpress.com/2013/02/the-moog.jpg";
      moog = loadImage(url, "jpg");
      size(moog.width, moog.height);
      noStroke();
      strokeWeight(10);
      textSize(18);
      textAlign(CENTER);
    }
    
    void draw() {
      image(moog, 0, 0);
    
      color c = moog.pixels[mouseY*width + mouseX];
      fill(c);
      ellipse(450,285,30,17);
      ellipse(430,250,50,30);
      ellipse(400,200,70,40);
      ellipse(360,120,320,60);
      fill(0);
      text("Red: "+int(red(c))+", Green: "+int(green(c))+", Blue: "+int(blue(c)),360,125);
      text("Check out Moog's ears..", 300, 50);
    
      if(red(c)>200 & green(c)<100 & blue(c)<100) {
        noFill();
        stroke(c);
        rect(5,5,width-10,height-10);
        noStroke();
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2011-10-28
      • 2011-09-22
      • 2017-08-19
      • 2013-12-19
      • 2014-04-29
      • 2012-04-25
      • 1970-01-01
      • 2017-07-09
      • 1970-01-01
      相关资源
      最近更新 更多