コード例 #1
0
ファイル: PanelImage.java プロジェクト: pbmartins/P3
 /**
  * @param pixels - Byte Array with Pixels
  * @param w - Image Width (columns)
  * @param h - Image Heigth (row)
  */
 PanelImage(byte[] pixels, int w, int h, boolean quad) {
   if (quad) {
     bi = new BufferedImage(w, h, BufferedImage.TYPE_4BYTE_ABGR);
     bi.setRGB(0, 0, w, h, byteArrayToIntArrayQuad(pixels), 0, w);
   } else {
     bi = new BufferedImage(w, h, BufferedImage.TYPE_3BYTE_BGR);
     bi.setRGB(0, 0, w, h, byteArrayToIntArray(pixels, w, h), 0, w);
   }
   this.setPreferredSize(new Dimension(w, h));
   this.setVisible(true);
 }
コード例 #2
0
  private void stuffWithLeastSignificantBit(BufferedImage image, byte[] message) {
    int[] pixelValues = getImagePixels(image);
    if (message.length * 8 > pixelValues.length * 3) {
      System.out.printf("Cannot stuff message in least significant bit\n");
    }

    int messageIndex = 0;
    int currentByteOffset = -1;

    pixls:
    for (int i = 0; i < pixelValues.length; i++) {
      for (int offset = 0; offset < 17; offset += 8) {
        currentByteOffset++;
        if (currentByteOffset > 7) {
          currentByteOffset = 0;
          messageIndex++;
        }
        if (messageIndex >= message.length) break pixls;
        int bitToEncode = (message[messageIndex] >> currentByteOffset) & 1;
        // color = (byte) ((color & 0xfe) | bitToEncode);
        // zero
        pixelValues[i] &= ~(0x1 << offset);
        // fill
        pixelValues[i] |= bitToEncode << offset;
      }
    }

    // put pixel ints back into BufferedImage
    for (int i = 0; i < pixelValues.length; i++) {
      int x = i % image.getWidth();
      int y = i / image.getWidth();
      image.setRGB(x, y, pixelValues[i]);
    }
  }
コード例 #3
0
ファイル: PixelColorDemo.java プロジェクト: hamodp/schoolwork
  public static void main(String[] args) {
    int x = 500, y = 80;
    DrawingKit dk = new DrawingKit("Daffodils", 800, 800);
    BufferedImage pict = dk.loadPicture("daffodils.jpg");

    // get pixel value at location (500, 80)
    int encodedPixelColor = pict.getRGB(x, y);
    Color pixelColor = new Color(encodedPixelColor);
    System.out.println(pixelColor);
    int red = pixelColor.getRed();
    int green = pixelColor.getGreen();
    int blue = pixelColor.getBlue();
    // change the color of the pixel to be pure red
    red = 255;
    green = 0;
    blue = 0;

    // update the pixel color in picture
    Color newPixelColor = new Color(red, green, blue);
    int newRgbvalue = newPixelColor.getRGB();
    pict.setRGB(x, y, newRgbvalue);
    // display the approximate location of the pixel
    dk.drawPicture(pict, 0, 0);
    BasicStroke s = new BasicStroke(3);
    dk.setStroke(s);
    Ellipse2D.Float e = new Ellipse2D.Float(x - 3, y - 3, 8, 8);
    dk.draw(e);
    dk.drawString("(600, 150)", x - 3, y - 5);
  }
コード例 #4
0
ファイル: Mandelbrot.java プロジェクト: bitmorse/visualizer
  public void paint(Graphics g) {

    double x1, y1;
    for (a = amin; a < amax; a += ((amax - amin) / width)) {
      for (b = bmin; b < bmax; b += ((bmax - bmin) / height)) {
        x = 0.0;
        y = 0.0;
        int iteration = 0;
        while ((x * x + y * y <= 4.0) && (iteration != times)) {
          x1 = xmapMandelbrot(x, y, a);
          y1 = ymapMandelbrot(x, y, b);
          x = x1;
          y = y1;
          iteration++;
        }
        if (iteration <= times && iteration > 0) {
          scaleda = scaleX(a, amin, amax);
          scaledb = scaleY(b, bmin, bmax);
          pixels[scaledb * width + scaleda] = alpha << 24 | red << 16 | iteration << 8 | blue;
        }
      }
    }

    bufferedImage.setRGB(0, 0, width, height, pixels, 0, width);

    g.drawImage(bufferedImage, 0, 0, width, height, null);

    // we save the image
    try {
      File outputfile = new File("saved.png");
      ImageIO.write(bufferedImage, "png", outputfile);
    } catch (IOException e) {
      e.printStackTrace();
    }
  }
コード例 #5
0
ファイル: Viewer3D.java プロジェクト: psava/cwp12
  public void saveFrametoPNG(String filename) {
    final int frameWidth = _canvas.getWidth();
    final int frameHeight = _canvas.getHeight();
    final ByteBuffer pixelsRGB = Direct.newByteBuffer(frameWidth * frameHeight * 3);
    _canvas.runWithContext(
        new Runnable() {
          public void run() {
            // glPushAttrib(GL_PIXEL_MODE_BIT);
            glReadBuffer(GL_BACK);
            glPixelStorei(GL_PACK_ALIGNMENT, 1);
            glReadPixels(0, 0, frameWidth, frameHeight, GL_RGB, GL_UNSIGNED_BYTE, pixelsRGB);
            // glPopAttrib();
          }
        });
    int[] pixelInts = new int[frameWidth * frameHeight];
    int p = frameWidth * frameHeight * 3;
    int q; // Index into ByteBuffer
    int i = 0; // Index into target int[]
    int w3 = frameWidth * 3; // Number of bytes in each row
    for (int row = 0; row < frameHeight; row++) {
      p -= w3;
      q = p;
      for (int col = 0; col < frameWidth; col++) {
        int iR = pixelsRGB.get(q++);
        int iG = pixelsRGB.get(q++);
        int iB = pixelsRGB.get(q++);
        pixelInts[i++] =
            0xFF000000 | ((iR & 0x000000FF) << 16) | ((iG & 0x000000FF) << 8) | (iB & 0x000000FF);
      }
    }

    // Create a new BufferedImage from the pixeldata.
    BufferedImage bufferedImage =
        new BufferedImage(frameWidth, frameHeight, BufferedImage.TYPE_INT_ARGB);
    bufferedImage.setRGB(0, 0, frameWidth, frameHeight, pixelInts, 0, frameWidth);

    try {
      javax.imageio.ImageIO.write(bufferedImage, "PNG", new File(filename));
    } catch (IOException e) {
      System.out.println("Error: ImageIO.write.");
      e.printStackTrace();
    }

    /* End code taken from: http://www.felixgers.de/teaching/jogl/imagingProg.html */

    /*
     final BufferedImage image = new BufferedImage(
     this.getWidth(), this.getHeight(), BufferedImage.TYPE_INT_ARGB);
     Graphics gr = image.getGraphics();
     this.printAll(gr);
     gr.dispose();
    try {
    	ImageIO.write(image, "PNG", new File(filename));
      } catch (IOException e) {
           System.out.println( "Error: ImageIO.write." );
           e.printStackTrace();
      }
    */
  }
コード例 #6
0
 /**
  * A convenience method for setting ARGB pixels in an image. This tries to avoid the performance
  * penalty of BufferedImage.setRGB unmanaging the image.
  *
  * @param image a BufferedImage object
  * @param x the left edge of the pixel block
  * @param y the right edge of the pixel block
  * @param width the width of the pixel arry
  * @param height the height of the pixel arry
  * @param pixels the array of pixels to set
  * @see #getRGB
  */
 public void setRGB(BufferedImage image, int x, int y, int width, int height, int[] pixels) {
   int type = image.getType();
   if (type == BufferedImage.TYPE_INT_ARGB || type == BufferedImage.TYPE_INT_RGB) {
     image.getRaster().setDataElements(x, y, width, height, pixels);
   } else {
     image.setRGB(x, y, width, height, pixels, 0, width);
   }
 }
コード例 #7
0
  /** Prototype give player a leveled up look */
  public void Opify(double Fac) {

    for (int i = 0; i < imgCHARAC.getWidth(); i++) {
      for (int j = 0; j < imgCHARAC.getHeight(); j++) {
        if (imgCHARAC.getRGB(i, j) != imgCHARAC.getRGB(2, 2)) {
          imgCHARAC.setRGB(i, j, (int) Math.round(imgCHARAC.getRGB(i, j) * Fac));
        }
      }
    }
  }
コード例 #8
0
  public synchronized void paint(Graphics g) {
    if (mLastFrame == null) {
      return;
    }

    int framePosX = 0;
    int framePosY = 0;

    VideoFrameRef depthFrame = mLastFrame.getDepthFrame();
    if (depthFrame != null) {
      int width = depthFrame.getWidth();
      int height = depthFrame.getHeight();

      // make sure we have enough room
      if (mBufferedImage == null
          || mBufferedImage.getWidth() != width
          || mBufferedImage.getHeight() != height) {
        mBufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
      }

      mBufferedImage.setRGB(0, 0, width, height, mDepthPixels, 0, width);

      framePosX = (getWidth() - width) / 2;
      framePosY = (getHeight() - height) / 2;

      g.drawImage(mBufferedImage, framePosX, framePosY, null);
    }

    for (UserData user : mLastFrame.getUsers()) {
      if (user.getSkeleton().getState() == SkeletonState.TRACKED) {
        drawLimb(g, framePosX, framePosY, user, JointType.HEAD, JointType.NECK);

        drawLimb(g, framePosX, framePosY, user, JointType.LEFT_SHOULDER, JointType.LEFT_ELBOW);
        drawLimb(g, framePosX, framePosY, user, JointType.LEFT_ELBOW, JointType.LEFT_HAND);

        drawLimb(g, framePosX, framePosY, user, JointType.RIGHT_SHOULDER, JointType.RIGHT_ELBOW);
        drawLimb(g, framePosX, framePosY, user, JointType.RIGHT_ELBOW, JointType.RIGHT_HAND);

        drawLimb(g, framePosX, framePosY, user, JointType.LEFT_SHOULDER, JointType.RIGHT_SHOULDER);

        drawLimb(g, framePosX, framePosY, user, JointType.LEFT_SHOULDER, JointType.TORSO);
        drawLimb(g, framePosX, framePosY, user, JointType.RIGHT_SHOULDER, JointType.TORSO);

        drawLimb(g, framePosX, framePosY, user, JointType.LEFT_HIP, JointType.TORSO);
        drawLimb(g, framePosX, framePosY, user, JointType.RIGHT_HIP, JointType.TORSO);
        drawLimb(g, framePosX, framePosY, user, JointType.LEFT_HIP, JointType.RIGHT_HIP);

        drawLimb(g, framePosX, framePosY, user, JointType.LEFT_HIP, JointType.LEFT_KNEE);
        drawLimb(g, framePosX, framePosY, user, JointType.LEFT_KNEE, JointType.LEFT_FOOT);

        drawLimb(g, framePosX, framePosY, user, JointType.RIGHT_HIP, JointType.RIGHT_KNEE);
        drawLimb(g, framePosX, framePosY, user, JointType.RIGHT_KNEE, JointType.RIGHT_FOOT);
      }
    }
  }
コード例 #9
0
  public synchronized void paint(Graphics g) {
    if (mLastFrame == null) {
      return;
    }

    int framePosX = 0;
    int framePosY = 0;

    VideoFrameRef depthFrame = mLastFrame.getDepthFrame();
    if (depthFrame != null) {
      int width = depthFrame.getWidth();
      int height = depthFrame.getHeight();

      // make sure we have enough room
      if (mBufferedImage == null
          || mBufferedImage.getWidth() != width
          || mBufferedImage.getHeight() != height) {
        mBufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
      }

      mBufferedImage.setRGB(0, 0, width, height, mDepthPixels, 0, width);

      framePosX = (getWidth() - width) / 2;
      framePosY = (getHeight() - height) / 2;

      g.drawImage(mBufferedImage, framePosX, framePosY, null);
    }

    for (UserData user : mLastFrame.getUsers()) {
      if (user.getSkeleton().getState() == SkeletonState.TRACKED) {
        drawSkeleton(g, framePosX, framePosY, user);

        // spatial joint's coordinates
        //				giveMeSpatialCoordinateJoints(user);

        try {
          createSkeletonInstances(user);
        } catch (Exception e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
      }

      if (user.getSkeleton().getState() == SkeletonState.TRACKED && startedTest) {
        try {
          createSkeletonInstances(user);
        } catch (Exception e) {
          // TODO Auto-generated catch block
          e.printStackTrace();
        }
        // System.out.println("send");
      }
    }
  }
コード例 #10
0
ファイル: ImageFont.java プロジェクト: nocookies92/JavaPlay
 public void drawTileNumC(int tileNum, int x, int y, Color c, Graphics g) {
   BufferedImage coloredTile = tiles[tileNum];
   for (int i = 0; i < this.tW; i++) {
     for (int j = 0; j < this.tH; j++) {
       Color originalColor = new Color(coloredTile.getRGB(i, j), true);
       Color nc = new Color(c.getRed(), c.getGreen(), c.getBlue(), originalColor.getAlpha());
       coloredTile.setRGB(i, j, nc.getRGB());
     }
   }
   g.drawImage(tiles[tileNum], x, y, null);
 }
コード例 #11
0
  /**
   * do damage to character
   *
   * @param Dmg Amount of Damage
   */
  public void Damage(int Dmg, int Type) {
    // If character is already dead then dont do damage
    if (ISDEAD) return;

    // Do damage
    if (Type == 1) {
      // DAMAGE FROM PHYSICAL ATTACK
      if (STATS.ARMOR > Dmg) return;
      HEALTH = HEALTH - Dmg + STATS.ARMOR;
    } else if (Type == 2) {
      // DAMAGE FROM MAGIC ATTACK
      if (STATS.MAGIC_RESIST > Dmg) return;
      HEALTH = HEALTH - Dmg + STATS.MAGIC_RESIST;
    }
    // If an Npc then run and hide
    if (NAI != null) {
      NAI.State = "alarmed";
    }

    // Death condition
    if (HEALTH <= 0) {
      // If player is dead
      if (this.CLASS.equals("Player")) {

        HEALTH = 0;

        // Quit game
        JOptionPane.showMessageDialog(null, "You are dead");
        X = 100;
        Y = 100;
        Opify(50);
        HEALTH = (int) MAX_HEALTH;

      } else {
        // If other character
        // set Death stats
        ISDEAD = true;
        HEALTH = 0;
        // display death
        CLASS = Cl + " (Dead)";

        // Rot effect
        for (int i = 0; i < imgCHARAC.getWidth(); i++) {
          for (int j = 0; j < imgCHARAC.getHeight(); j++) {
            imgCHARAC.setRGB(i, j, imgCHARAC.getRGB(i, j) * (int) Math.pow(3, 3));
          }
        }

        // Make inventory open to looting
        INVENTORY.OPEN_INVEN = true;
      }
    }
  }
コード例 #12
0
ファイル: ImageFilterOp.java プロジェクト: helma-org/helma
    public void setPixels(
        int x, int y, int w, int h, ColorModel model, int[] pixels, int off, int scansize) {
      // Cropping may be necessary: It's possible that the size of the
      // specified destination image is not the same as the size the
      // ImageFilter would produce!
      if (x < width && y < height) {
        if (x + w > width) w = width - x;
        if (y + h > height) h = height - y;

        if (w > 0 && h > 0) image.setRGB(x, y, w, h, pixels, off, scansize);
      }
    }
コード例 #13
0
ファイル: ImageEditor.java プロジェクト: njhazelh/Personal
  /*
   * USE AS A GUIDE
   *Method clears red values from jpg
   */
  public static BufferedImage depleteRed(int arr[][], BufferedImage x) {
    // local variables
    int arrWidth = arr.length;
    int arrHeight = arr[0].length;

    for (int i = 0; i < arrWidth; i++) {
      for (int j = 0; j < arrHeight; j++) {
        arr[i][j] = arr[i][j] & 0xFF00FFFF; // uses bitwise operator to turn off all red pixels
        x.setRGB(i, j, arr[i][j]);
      }
    }

    return x;
  }
コード例 #14
0
 public void overlay() {
   for (int y = 0; y < height; ++y) {
     for (int x = 0; x < width; ++x) {
       // System.out.println(nonMax.getRGB(x,y));
       if (nonMax.getRGB(x, y) != -1) {
         int rgb = img.getRGB(x, y);
         Color color = new Color(rgb);
         Color res = new Color(255, color.getGreen(), color.getBlue());
         img.setRGB(x, y, res.getRGB());
       }
     }
   }
   ImageIcon icon1 = new ImageIcon(img);
   lbl1.setIcon(icon1);
 }
コード例 #15
0
ファイル: GLImage.java プロジェクト: Mitsugaru/TwoDotFiveD
  /** Scale this GLImage so width and height are powers of 2. Recreate pixels and pixelBuffer. */
  public void convertToPowerOf2() {
    // make BufferedImage from original pixels
    BufferedImage image = new BufferedImage(w, h, BufferedImage.TYPE_INT_ARGB);
    image.setRGB(0, 0, w, h, pixels, 0, w);

    // scale into new image
    BufferedImage scaledImg = convertToPowerOf2(image);

    // resample pixel data
    w = scaledImg.getWidth(null);
    h = scaledImg.getHeight(null);
    pixels = getImagePixels(scaledImg); // pixels in default Java ARGB format
    pixelBuffer = convertImagePixelsRGBA(pixels, w, h, false); // convert to bytes in RGBA format
    textureW = GLApp.getPowerOfTwoBiggerThan(w); // the texture size big enough to hold this image
    textureH = GLApp.getPowerOfTwoBiggerThan(h); // the texture size big enough to hold this image
  }
コード例 #16
0
ファイル: GLImage.java プロジェクト: Mitsugaru/TwoDotFiveD
 /**
  * Save an array of ARGB pixels to a PNG file. If flipY is true, flip the pixels on the Y axis
  * before saving.
  */
 public static void savePixelsToPNG(
     int[] pixels, int width, int height, String imageFilename, boolean flipY) {
   if (pixels != null && imageFilename != null) {
     if (flipY) {
       // flip the pixels vertically (opengl has 0,0 at lower left, java is upper left)
       pixels = GLImage.flipPixels(pixels, width, height);
     }
     try {
       // Create a BufferedImage with the RGB pixels then save as PNG
       BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
       image.setRGB(0, 0, width, height, pixels, 0, width);
       javax.imageio.ImageIO.write(image, "png", new File(imageFilename));
     } catch (Exception e) {
       GLApp.err("GLImage.savePixelsToPNG(" + imageFilename + "): exception " + e);
     }
   }
 }
コード例 #17
0
  public static java.awt.Image ApplyAlphaMask(java.awt.Image original, java.awt.Image alphamask) {
    int width = original.getWidth(null), height = original.getHeight(null);
    if (width <= 0) width = 1;
    if (height <= 0) height = 1;
    BufferedImage resultImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
    BufferedImage image = toBufferedImage(original);
    BufferedImage mask = toBufferedImage(alphamask);
    for (int y = 0; y < image.getHeight(null); y++) {
      for (int x = 0; x < image.getWidth(null); x++) {
        try {
          Color c = new Color(image.getRGB(x, y));
          Color maskC = new Color(mask.getRGB(x, y));
          Color maskedColor = new Color(c.getRed(), c.getGreen(), c.getBlue(), maskC.getRed());
          resultImg.setRGB(x, y, maskedColor.getRGB());
        } catch (Exception e) {

        }
      }
    }
    return toImage(resultImg);
  }
コード例 #18
0
  public GroundTexture(int size, Ground grnd) {

    System.out.println("Calculating Ground Texture...");

    img = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB);

    double r, g, b;
    Random rnd = new Random();

    System.out.print("Initializing...");

    for (int x = 0; x < size; x++) {
      for (int y = 0; y < size; y++) {
        double slope = 0;
        for (int nx = x - 1; nx < x + 1; nx++)
          for (int ny = y - 1; ny < y + 1; ny++)
            if ((nx >= 0) && (nx < size))
              if ((ny >= 0) && (ny < size)) slope += Math.abs(grnd.topo[nx][ny] - grnd.topo[x][y]);
        if (slope < 1) slope = 1;
        g = 5d + 80d / (grnd.topo[x][y] / 2000d + 1d) + rnd.nextDouble() * 30d / (slope);
        r =
            g
                * (1.17
                    + (-.3 + (rnd.nextDouble() * .3))
                        / (grnd.topo[x][y] / 200d + 1d)
                        / (slope / 5 + 1));
        b =
            r
                * (.7
                    + (-.3 + (rnd.nextDouble() * .2))
                        / (grnd.topo[x][y] / 200d + 1d)
                        / (slope / 5 + 1));

        img.setRGB(x, y, colorRGB((int) r, (int) g, (int) b));
      }
    }

    /**/

    //		save("bodentextur2","png");

    System.out.print("                \rRandom Growth");

    for (int i = 0; i < size * size * 8; i++) {
      if (i % (size * size / 2) == 0) System.out.print(".");
      int x = 3 + rnd.nextInt(size - 6);
      int y = 3 + rnd.nextInt(size - 6);
      int nx = x - 1 + rnd.nextInt(3);
      int ny = y - 1 + rnd.nextInt(3);
      while ((nx < 0) || (nx >= size) || (ny < 0) || (ny >= size)) {
        nx = x - 1 + rnd.nextInt(3);
        ny = y - 1 + rnd.nextInt(3);
      }
      Color dis = getColorAt(x, y);
      Color col = getColorAt(nx, ny);
      if (grnd.topo[nx][ny] >= 4.5)
        if (col.getGreen() / col.getRed() > dis.getGreen() / dis.getRed())
          if (Math.abs(grnd.topo[x][y] - grnd.topo[nx][ny]) < .65) {
            int c = colorRGB(col.getRed(), col.getGreen(), col.getBlue());
            // img.setRGB(x,y, colorRGB(col.getRed(), col.getGreen(), col.getBlue()));
            // img.setRGB(x-1+rnd.nextInt(3),y-1+rnd.nextInt(3), colorRGB(col.getRed(),
            // col.getGreen(), col.getBlue()));
            for (nx = x - 1 - rnd.nextInt(3); nx < 1 + x + rnd.nextInt(3); nx++)
              for (ny = y - 1 - rnd.nextInt(3); ny < y + 1 + rnd.nextInt(3); ny++) {
                img.setRGB(nx, ny, c);
                grnd.topo[nx][ny] += 1.8;
              }
          }
    }

    System.out.print("                 \rAntialiasing...");
    /*		for (int x = 0; x < size; x++){
    	for (int y = 0; y < size; y++){
    		double sumr = 0;
    		double sumg = 0;
    		double sumb = 0;
    		double div = 0;
    		for (int nx=x-1;nx<x+1;nx++)
    			for (int ny=y-1;ny<y+1;ny++)
    				if ((nx>=0) && (nx < size)) if ((ny>=0) && (ny < size)) {
    					Color col = getColorAt(nx,ny);
    					sumr+=col.getRed();
    					sumg+=col.getGreen();
    					sumb+=col.getBlue();
    					div++;
    				}
    		r=sumr/div;
    		g=sumg/div;
    		b=sumb/div;
    		img.setRGB(x,y, colorRGB((int)r, (int)g, (int)b) );

    	}
    }*/

    System.out.print("        \r");
    //		save("bodentextur3","png");
    save("bodentextur", "png");
  }
コード例 #19
0
ファイル: Viewer.java プロジェクト: jaasilva/ReDstm
 public void point(int x, int y, int col) {
   image.setRGB(x, y, col);
 }
コード例 #20
0
ファイル: Viewer.java プロジェクト: jaasilva/ReDstm
 public void drawSquare(int x1, int y1, int x2, int y2, int col) {
   for (int i = x1; i <= x2; i++) for (int j = y1; j <= y2; j++) image.setRGB(i, j, col);
 }
コード例 #21
0
  public void run() {
    while (true) {
      BALL = null;
      ball_t ballLCM = new ball_t();

      synchronized (depthMonitor) {
        try {
          depthMonitor.wait();
        } catch (Exception e) {
          e.printStackTrace();
        }
      }
      ballLCM.nanoTime = depthStream.latestTimestamp;
      ArrayList<Statistics> blobs;
      depthStream.getReadLock().lock();
      try {
        blobs = finder.analyze2(depthStream.getValidImageArray());
      } finally {
        depthStream.getReadLock().unlock();
      }
      Statistics ball = null;
      Statistics robot = null;
      int minSize = pg.gi("blobThresh");

      // find robot and ball by blob size
      Collections.sort(blobs, ComparatorFactory.getStatisticsCompareSize());
      // find robot and ball by y pixel
      // Collections.sort(blobs, ComparatorFactory.getStatisticsCompareYPix());
      //			if (tracking) {
      //				System.out.println("num blobs: " + blobs.size());
      //				System.out.println("biggest blob size: " + blobs.get(0).N);
      //			}
      //			for (Statistics blob : blobs) {
      //				if (blob.N > 10) {
      //					System.out.println("blob size: " + blob.N);
      //				}
      //				else {
      //					break;
      //				}
      //			}
      if (blobs.size() == 1) {
        Statistics first = blobs.get(0);
        if (first.N > minSize) {
          ball = first;
        }
      } else if (blobs.size() >= 2) {
        Statistics first = blobs.get(0);
        Statistics second = blobs.get(1);
        if (first.N > minSize) {
          ball = first;
        }
        if (second.N > minSize) {
          robot = first;
          ball = second;
        }
      }

      // System.out.println("balls points " + depthStream.trajectory.size());

      // if not tracking keep kv.depthStream.trajectory to just one index
      if (!tracking) {
        depthStream.trajectory.clear();
      }

      if (ball != null) {
        depthStream.trajectory.add(ball);

        Point depthPix = ball.center();
        Point depthCoord = new Point();
        depthCoord.x = depthPix.x - KinectVideo.C_X;
        depthCoord.y = KinectVideo.C_Y - depthPix.y;

        // System.out.println("avg depth " + ball.Uz());

        // get depth from average depth of blob
        double realDepth = raw_depth_to_meters(ball.Uz());
        Point3D coord = depthStream.getWorldCoords(depthCoord, realDepth);
        if (depthPix != null) {
          for (int y = depthPix.y - 3; y < depthPix.y + 3; y++) {
            for (int x = depthPix.x - 3; x < depthPix.x + 3; x++) {
              try {
                depthImg.setRGB(x, y, 0xFFFF0000);
              } catch (Exception e) {
                // System.out.println(x + " " + y);
              }
              ;
            }
          }
          // if (tracking) {
          // 	//save image
          // 	depthStream.getReadLock().lock();
          // 	try {
          // 		File imgFile = new File("image" + ballNum++ + ".png");
          // 		try {
          // 			ImageIO.write(depthImg, "png", imgFile);
          // 		}
          // 		catch(Exception e) {
          // 			System.out.println("can't save img");
          // 		}
          // 	}
          // 	finally {
          // 		depthStream.getReadLock().unlock();
          // 	}
          // }

          if (tracking) {
            ballLCM.x = coord.x;
            ballLCM.y = coord.y;
            ballLCM.z = coord.z;
            // if(tracking)
            System.out.println("updating new ball (" + System.currentTimeMillis() + ")");
            //						if (ballLCM.x > CatchController.TARGET_MAX_X)
            lcm.publish("6_BALL", ballLCM);
            //						else
            //							System.out.println("ball past target zone");
          }
        }
      }
    }
  }
コード例 #22
0
ファイル: JREImage.java プロジェクト: kazuyakuza/wh40kh
 /**
  * Create RGBA-8 image from ints (each int = AABBGGRR). why isn't there a built in method to do
  * this?
  */
 public static BufferedImage createRGBA8Image(int[] pix, int width, int height) {
   BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB);
   img.setRGB(0, 0, width, height, pix, 0, width);
   return img;
 }