2

次のスケッチをProcessingに書きました。これは、画像をスキャンし、各黒ピクセルの座標を xList と yList の 2 つの ArrayList に格納します。現在は座標を出力していますが、これらの ArrayList を Arduino に送信し、Arduino に独自の 2 つの配列 (または 1 つの 2D 配列) に格納させる必要があります。私が直面している問題は、ArrayLists に事前定義された数の要素がないため、xList からのデータが終了し、yList からのデータが開始されたときに Arduino に通知できないことです。

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

1 に答える 1

0

X リスト全体を送信してから Y リスト全体を送信するのではなく、両方の配列が同じサイズであることがわかっているため、x を 1 つ、y を 1 つ、次の x を送信し、次の y... を送信できます。

Arduino 側では、受け取った最初の数値を x 配列に配置し、次に 2 番目を Y 配列に配置し、3 番目を X に、4 番目を Y に配置します。

Arduino で注意すべきことの 1 つは、C++ であるため、配列の境界チェックがなく、新しいデータが追加されたときに配列が大きくならないことです。すべての座標に適合する十分なストレージで配列が初期化されていることを確認する必要があります。

于 2012-09-25T17:48:57.943 に答える