Image Processing program running just fine- added in variables and freezes

I was tidying up my first ever program (that worked), wanting to make it adjust to different circumstances like any good code is able to do, I recognized numbers that were being used more than once in the same context and threw those into ints and floats and thought it was nice and clean, then went to run it and it ran, but when I went to execute the bulk of the code (mouseClicked), it freezes, and doesn’t give me my measurements.
If you want to execute the code, I’ll include it all, but simply for analyzing scroll down half-way to “void mouseClicked() {” this is where I was cleaning up. I added the top two variables cameraRatio & blackPixel, and all of a sudden it freezes. I feel like there is some common knowledge on when to use variables and when to just plug in the repetitive numbers that I missed.
Any input is greatly appreciated.

PImage crossSection;
int l = 0;
int w = 0;

//size 1/4 of iPhone image
void setup() {
  size(1008, 756);
  crossSection = loadImage("6.5inches.jpeg");
}

void draw() {
  display();
  //adjustable threshold for different backgrounds
  measuringLines();
  updatePixels();
}

void display() {
  image(crossSection, 0, 0);
  crossSection.resize(1008, 756);
  crossSection.filter(THRESHOLD, 0.1);
}

void mouseClicked() {
  
  float cameraRatio = 3.5/196;     //ratio of iPhone's pixels to inches
  int blackPixel = -16777214;    //256^3 == black pixel
  int topLine = mouseY - (height/4 + l/2);
  int bottomLine = mouseY  + (height/4 + l/2);
  int leftLine = mouseX - (width/4 + w/2);
  int rightLine = mouseX + (width/4 + w/2);
  int e = 0;
  int f = 0;
  int g = 0;
  int h = 0;

  while (e<1) {
    color leftSide = get(leftLine, mouseY - 2);    //assigning color of pixel to "leftSide"
    if (leftSide == blackPixel) {    
      e++;    //exit loop
    } else {
      leftLine++;    //keep checking for black pixels
    }
  }
  while (f<1) {
    color rightSide = get(rightLine, mouseY - 2);
    if (rightSide == blackPixel) {    
      println("Length: "+((rightLine-leftLine)*cameraRatio) +" inches");   
      f++;    
    } else {
      rightLine--;    
    }
  }
  while (g<1) {
    color top = get(mouseX - 2, topLine);
    if (top == blackPixel) {
      g++;
    } else {
      topLine++;
    }
  }
  while (h<1) {
    color bottom = get(mouseX - 2, bottomLine);
    if (bottom == blackPixel) {
      println("Height: "+((bottomLine-topLine)*cameraRatio) +" inches");
      h++;
    } else {
      bottomLine--;
    }
  }
}

void measuringLines() {
  stroke(0);
  rectMode(CENTER);
  rect(mouseX, mouseY, 1, (height/2) + l);
  rect(mouseX, mouseY, (width/2) + w, 1);
}

void keyPressed() {
  if (key == CODED) {
    if (keyCode == UP) {
      l = l + 5;
    } else if (keyCode == DOWN) {
      l = l - 5;
    } else if (keyCode == LEFT) {
      w = w - 5;
    } else if (keyCode == RIGHT) {
      w = w + 5;
    }
  }
}

my PImage

If you are looking to run it, to save you some time, it’s supposed to measure from the crosshairs in, and if it once it hits a black pixel it stops and calculates the distance.

Small note, I don’t know what people put in their size arguments, P3D, P2D, I’ll look it up, but Dan (Coding Train) didn’t cover that extensively in the first half of tutorials that I’ve made it through.

1 Like

possibly your

mouseClicked()

starts a endless while loop ( one of the 4 while )

  • i not say do never use while
  • i not say do never start loops on click ( inside mouseClicked )
  • but i do say use PRINTLN ( in all 4 while times 2 if else )
    to know what is going on.
2 Likes

Thank you very much, it was the loop. Took me forever to fix it, even though I knew exactly where it was. Is there a difference to having a global variable with mouseX in it rather than a local variable? It seemed like the mouseX position was never updated when it was global or something along those lines. Thank you very much for the help.

Our bestie Dan has an interesting video on while and for Loops, which also includes the dangers kll spoke about (the endless loop).

1 Like