示例#1
0
  /**
   * Checks to see if the BufferedImage has the same intensity values as the ImageUInt8
   *
   * @param imgA BufferedImage
   * @param imgB ImageUInt8
   */
  public static void checkEquals(BufferedImage imgA, ImageInt16 imgB) {

    if (imgA.getRaster() instanceof ByteInterleavedRaster
        && imgA.getType() != BufferedImage.TYPE_BYTE_INDEXED) {
      ByteInterleavedRaster raster = (ByteInterleavedRaster) imgA.getRaster();

      if (raster.getNumBands() == 1) {
        int strideA = raster.getScanlineStride();
        int offsetA = raster.getDataOffset(0) - raster.getNumBands() + 1;

        // handle a special case where the RGB conversion is screwed
        for (int i = 0; i < imgA.getHeight(); i++) {
          for (int j = 0; j < imgA.getWidth(); j++) {
            int valB = imgB.get(j, i);
            int valA = raster.getDataStorage()[offsetA + i * strideA + j];
            if (!imgB.getTypeInfo().isSigned()) valA &= 0xFFFF;

            if (valA != valB)
              throw new RuntimeException("Images are not equal: " + valA + " " + valB);
          }
        }
        return;
      }
    } else if (imgA.getRaster() instanceof ShortInterleavedRaster) {
      ShortInterleavedRaster raster = (ShortInterleavedRaster) imgA.getRaster();

      if (raster.getNumBands() == 1) {
        int strideA = raster.getScanlineStride();
        int offsetA = raster.getDataOffset(0) - raster.getNumBands() + 1;

        // handle a special case where the RGB conversion is screwed
        for (int i = 0; i < imgA.getHeight(); i++) {
          for (int j = 0; j < imgA.getWidth(); j++) {
            int valB = imgB.get(j, i);
            int valA = raster.getDataStorage()[offsetA + i * strideA + j];
            if (!imgB.getTypeInfo().isSigned()) valA &= 0xFFFF;

            if (valA != valB)
              throw new RuntimeException("Images are not equal: " + valA + " " + valB);
          }
        }
      }
    } else {
      for (int y = 0; y < imgA.getHeight(); y++) {
        for (int x = 0; x < imgA.getWidth(); x++) {
          int rgb = imgA.getRGB(x, y);

          int gray = ((((rgb >>> 16) & 0xFF) + ((rgb >>> 8) & 0xFF) + (rgb & 0xFF)) / 3);
          int grayB = imgB.get(x, y);
          if (!imgB.getTypeInfo().isSigned()) gray &= 0xFFFF;

          if (Math.abs(gray - grayB) != 0) {
            throw new RuntimeException(
                "images are not equal: (" + x + " , " + y + ") A = " + gray + " B = " + grayB);
          }
        }
      }
    }
  }
  /**
   * Computes the mean squared error (MSE) between the two images.
   *
   * @param imgA first image. Not modified.
   * @param imgB second image. Not modified.
   * @return error between the two images.
   */
  public static double computeMeanSquaredError(ImageInt16 imgA, ImageInt16 imgB) {
    final int h = imgA.getHeight();
    final int w = imgA.getWidth();

    double total = 0;

    for (int y = 0; y < h; y++) {
      for (int x = 0; x < w; x++) {
        double difference = imgA.get(x, y) - imgB.get(x, y);
        total += difference * difference;
      }
    }

    return total / (w * h);
  }