如何使用Java或Processing从图像中仅检测单色(如红色,蓝色或绿色)?

时间:2013-09-01 06:14:38

标签: image processing robotics

我正在开展一个机器人项目,我需要进行一些图像处理才能识别蓝色物体,红色障碍物和绿色目的地。我正在使用java进行图像处理。

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

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

2 个答案:

答案 0 :(得分: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();
  }
}

答案 1 :(得分:2)

好吧,虽然@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();
}