How to detect only single color such as Red, Blue or Green from an image using Java or Processing?

StackOverflow https://stackoverflow.com/questions/18555787

  •  27-06-2022
  •  | 
  •  

Question

I am working on a robotics project in which I have to some image processing in order to recognize blue colored objects, red colored obstacles, and green colored destination. I am using java for Image Processing.

Right now, I have been able to locate Red, Green, and Blue colored objects using Blobscanner library. But the difficulty is that, my algorithm only works fine when the background is pure Black. Because I am using RGB color model, and in RGB, black is represented as 0,0,0 and white as 255,255,255 and gray color also has some red component in it, so it also gets detected by the algorithm. I don't know the algorithm which exactly pinpoints the red color ignoring others.

Please help me detect only red color (and its other shades) in any image.

Était-ce utile?

La solution

Well, while @geotheroy were posting I also gave this a try, it works and also is cool to see :) so I'm posting it anyway... Same base idea thought...

drag vertically to set the threshold, any key to view original.

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();
}

Autres conseils

Does this help give you some ideas:

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();
  }
}
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top