Loading...
Logo
Processing Forum
I have the Processing sketch below, which scans through an image, storing the X and Y coordinates of each black pixel into 2 respective ArrayLists - xList, and yList. This sketch currently prints out these coordinates, but I now need to send each element of each of these arrays to the Arduino, and have the Arduino store them in it's own array (or arrays). How?

Copy code
  1. import javax.swing.*;

    PImage img;
    //Threshold determines what is considered a "black" pixel
    //based on a 0-255 grayscale.
    int threshold = 50;

    void setup() {
     
      // File opener
      try{
        UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
      } catch (Exception e) {
        e.printStackTrace();
      }
      final JFileChooser fc = new JFileChooser();
     
      int returnVal = fc.showOpenDialog(this);
     
      if (returnVal == JFileChooser.APPROVE_OPTION) {
        File file = fc.getSelectedFile();
        if (file.getName().endsWith("jpg")) {
          img = loadImage(file.getPath());
          if (img != null) {
            size(img.width,img.height);
            image(img,0,0);
          }
        } else {
          println("Please select a .jpg image file.");
        }
      }
      //I added noLoop() here, because the program would output
      //the coordinates continuously until it was stopped.
      noLoop();
    }

    void draw() {
      //Here it scans through each pixel of the image.
      ArrayList xList = new ArrayList();
      ArrayList yList = new ArrayList();
      for (int ix = 0; ix < img.width; ix++) {
        for (int iy = 0; iy < img.height; iy++) {
          //If the brightness of the pixel at (ix,iy) is less than
          //the threshold
          if (brightness(get(ix,iy)) < threshold) {
            //Add ix, and iy to their respective ArrayList.
            xList.add(ix);
            yList.add(iy);
          }
        }
      }
      //Print out each coordinate
      //For this statement, I use xList.size() as the limiting
      //condition, because xList and yList should be the same size.
      for (int i=0; i<xList.size(); i++) {
        println("X:" + xList.get(i) + " Y:" + yList.get(i));
      }
    }

Replies(1)

Note: do you know that selectInput() can do the same thing than JFileChooser, a bit simpler?

I am not a specialist of serial communication, but I think for such complex structure, you need some protocol: you need to indicate if the int you will send (or have sent) belong to x or y coordinates, and to mark the end of the data.
Some special values might do the trick.
Or, send first a value indicating the number of x coordinates to follow, then send them, then repeat for y.