Image line detection and following help

edited June 2014 in Questions about Code

Hello,

I am in the process of creating some basic software for a CNC Mill i have just built. Now i want to be able to convert a basic 2D black line drawing into GCode. Now i know there is software out there that already does this for CNCs, but my CNC is runs completly through Arduino and Processing using a custom made GCode interpreter.

I have this so far, and as far as i can see this should work. It should trace a line drawing in green. But i cant figure out why it gets stuck. Any ideas? Here is a link to the image i am currently trying with: IMAGE

color threshold = color(50,50,50);

int img_X = 0, img_Y = 0;
int line_X, line_Y, stop = 0;

PImage img;

void setup(){
  img = loadImage("test.jpg");
  size(img.width,img.height); 
  image(img,0,0);
}

void draw(){
  //userInterface();

  for(int i = 0; i < 50; i++){
    img.loadPixels();
    if(detectColor(img_X, img_Y, threshold) == 0 && stop == 0){
      img.pixels[img_Y*width+img_X] = color(255,0,0);

      img_X++;
      if(img_X > width){
        img_X = 0;
        img_Y++;
      }
      if(img_Y > height){
        img_X = 0;
        img_Y = 0;
      }
    }else{
      if(stop == 0){
        line_X = img_X;
        line_Y = img_Y;
        stop = 1;
      }
      img.pixels[line_Y*width+line_X] = color(0,255,0);
      if(detectColor(line_X+1, line_Y, threshold) == 1){
        line_X++;
      }else if(detectColor(line_X, line_Y+1, threshold) == 1){
        line_Y++;
      }else if(detectColor(line_X-1, line_Y, threshold) == 1){
        line_X--;
      }else if(detectColor(line_X, line_Y-1, threshold) == 1){
        line_Y--;
      }else{
        stop = 0;
      }
    }
    img.updatePixels();
  }
  image(img,0,0);
}

int detectColor(int img_X, int img_Y, color threshold){
  if(img.pixels[img_Y*width+img_X] <= threshold && img.pixels[img_Y*width+img_X] >= color(0,0,0)){
    return 1;
  }else{
    return 0;
  }
}

Answers

  • "it gets stuck"
    Not sure what it means, but I guess what is missing to your code is a call to background() at the start of draw(). A very common error...

  • When i say it gets stuck, i mean that the green tracing line, traces the top line of cube, then the right hand side of the cube, and then just stops. It should continue to follow connected black lines. I added background(0) but didn;t change anything. Im doing all the analysis and changing in the loaded image pixel data, so whether background is set or not makes no difference.

Sign In or Register to comment.