Reversing the threshold filter

I’ll throw in only the important parts, but for an image processing application I’m making, I want to analyze the image with the rgb components (typical picture), and then change it to black and white using the threshold filter, and run a few things with black and white. It all happens when I click the mouse, but I can’t seem to get the color photo back. Ideally it would happen in mouseReleased, and I’d run the majority of the code in mousePressed. Let me know with any thoughts, thanks.

PImage crossSection;

void setup() {
  size(1008, 756);
  crossSection = loadImage("crossSection.jpg");
}

void draw() {
  backgound(0);
  image(crossSection, 0, 0);
  crossSection.resize(1008, 756);
  loadPixels();
  crossSection.loadPixels();
  updatePixels();
}

void mousePressed() {
  //code that needs an RGB photo
  crossSection.filter(THRESHOLD, .85);
  updatePixels();
  //code that needs the black and white version
}

void mouseReleased() {
  //not sure what to put here in order to change the image back to RGB
}
1 Like

untested,
but i think that the idea is to run the

filter();

https://processing.org/reference/filter_.html function
after the

image();

a temporary loading from mousePressed() might be overwritten by next draw.
so better use the mouse to change the threshold value only ( used in draw later )

OR
move the image(); up into the setup(); could work too.


start from:
/File/Examples/ Basics/Image/LoadDisplayImage/

for operation can try my MouseWheelPlusPlus

/**
 * Load and Display 
 */

PImage img;
float fset = 0.50;
boolean fenable = false;

void setup() {
  size(640, 360);
  img = loadImage("moonwalk.jpg");  // Load the image into the program
  println("click on canvas, press \n click toggle enable filter\n key [f] and turn mousewheel");
}

void draw() {
  image(img, 0, 0);
  if ( fenable ) filter(THRESHOLD,fset);
}
void mouseWheel(MouseEvent event) {
  float e = event.getCount(); //println(e);
  if ( keyPressed && key == 'f' ) { 
    fset += e/10.0 ;
    println(" key f: fset "+fset);
  }
}

void mousePressed() {
  fenable = ! fenable; 
}
4 Likes

It worked, thank you very much!

1 Like