title: How to separate an image into two with java
body: <p>I'm wondering if there is a "smart" way of splitting an image based on certain features.</p>
  
  <p>The images are 300x57, black and white (actually grayscale, but most colors are either black or white), it is comprised of two main features (let's call them blobs) separated by black space, each blob slightly varies in width and height, the position of the blobs also varies, the blobs NEVER overlap!</p>
  
  <p>Here is what an image "looks" like:</p>
  
  <pre><code>-------------------------
  ----WWW---------WWWWW----
  ---WWWWWWW----WWWWWW-----
  -----WWWW-------WWW------
  -------------------------
  </code></pre>
  
  <p>The resulting split would be something like this:</p>
  
  <pre><code>------------     -------------
  ----WWW-----     ----WWWWW----
  ---WWWWWWW--     --WWWWWW-----
  -----WWWW---     ----WWW------
  ------------     -------------
  </code></pre>
  
  <p>Steps I plan to take in order to split the image:</p>
  
  <ol>
  <li>Scan the image from one side to the other.</li>
  <li>Determine the edges of the blobs.</li>
  <li>Take the distance between the two inside edges.</li>
  <li>Split the image at the middle of the inside distance.</li>
  <li>Save the two images as separate files.</li>
  </ol>
  
  <p>It would be nice if I normalize the image widths, so all of my images have a uniform width when they're saved.</p>
  
  <p>I have no experience in image manipulation, so I don't know what's an efficient way to do this.  I'm currently using a BufferedImage, getting the width/height, iterating over each pixel, etc.  There is no wrong solution for my problem, but I'm looking for a more efficient one (less code + faster).  I've also been looking into java.awt.Graphics...</p>
  
  <p>I would appreciate if I get some ideas for more efficient ways to do this task.  I want to stick with Java's built-in libraries, so is BufferedImage or Graphics2D the most efficient thing to use in this case?</p>
  
  <p>EDIT: 
  Here is the code after reading the suggestions:</p>
  
  <pre><code>public void splitAndSaveImage( BufferedImage image ) throws IOException
  {
      // Process image ------------------------------------------         
      int height = image.getHeight();
      int width = image.getWidth();
      boolean edgeDetected = false;
      double averageColor = 0;
      int threshold = -10;
      int rightEdge = 0;
      int leftEdge = 0;
      int middle = 0;
  
      // Scan the image and determine the edges of the blobs.
      for(int w = 0; w &lt; width; ++w)
      {               
          for(int h = 0; h &lt; height; ++h)
          {
              averageColor += image.getRGB(w, h);
          }
  
          averageColor = Math.round(averageColor/(double)height);
  
          if( averageColor /*!=-1*/&lt; threshold &amp;&amp; !edgeDetected )
          {
              // Detected the beginning of the right blob
              edgeDetected = true;
              rightEdge = w;
          }else if( averageColor &gt;= threshold &amp;&amp; edgeDetected )
          {
              // Detected the end of the left blob
              edgeDetected = false;
              leftEdge = leftEdge==0? w:leftEdge;
          }
  
          averageColor = 0;
      }
  
      // Split the image at the middle of the inside distance.
      middle = (leftEdge + rightEdge)/2;
  
      // Crop the image
      BufferedImage leftImage = image.getSubimage(0, 0, middle, height);
  
      BufferedImage rightImage = image.getSubimage(middle, 0, (width-middle), height);
  
      // Save the image
      // Save to file -------------------------------------------
      ImageIO.write(leftImage, "jpeg", new File("leftImage.jpeg"));
  
      ImageIO.write(rightImage, "jpeg", new File("rightImage.jpeg"));
  }
  </code></pre>