/** Sets a rectangle inside the image with the specified value. */
  public static void fillRectangle(
      ImageSInt32 img, int value, int x0, int y0, int width, int height) {
    int x1 = x0 + width;
    int y1 = y0 + height;

    for (int y = y0; y < y1; y++) {
      for (int x = x0; x < x1; x++) {
        if (img.isInBounds(x, y)) img.set(x, y, value);
      }
    }
  }
  /**
   * Fills the whole image with the specified pixel value
   *
   * @param img An image.
   * @param value The value that the image is being filled with.
   */
  public static void fill(ImageSInt32 img, int value) {
    final int h = img.getHeight();
    final int w = img.getWidth();

    int[] data = img.data;

    for (int y = 0; y < h; y++) {
      int index = img.getStartIndex() + y * img.getStride();
      for (int x = 0; x < w; x++) {
        data[index++] = value;
      }
    }
  }
  /**
   * 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(ImageSInt32 imgA, ImageSInt32 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);
  }
  /**
   * Sets each value in the image to a value drawn from an uniform distribution that has a range of
   * min <= X < max.
   */
  public static void randomize(ImageSInt32 img, Random rand, int min, int max) {
    final int h = img.getHeight();
    final int w = img.getWidth();

    int range = max - min;

    int[] data = img.data;

    for (int y = 0; y < h; y++) {
      int index = img.getStartIndex() + y * img.getStride();
      for (int x = 0; x < w; x++) {
        data[index++] = (rand.nextInt(range) + min);
      }
    }
  }
  /** Flips the image from top to bottom */
  public static void flipVertical(ImageSInt32 img) {
    int h2 = img.height / 2;

    for (int y = 0; y < h2; y++) {
      int index1 = img.getStartIndex() + y * img.getStride();
      int index2 = img.getStartIndex() + (img.height - y - 1) * img.getStride();

      int end = index1 + img.width;

      while (index1 < end) {
        int tmp = img.data[index1];
        img.data[index1++] = img.data[index2];
        img.data[index2++] = (int) tmp;
      }
    }
  }
  /** Adds Gaussian/normal i.i.d noise to each pixel in the image. */
  public static void addGaussian(ImageSInt32 img, Random rand, double sigma, int min, int max) {
    final int h = img.getHeight();
    final int w = img.getWidth();

    int[] data = img.data;

    for (int y = 0; y < h; y++) {
      int index = img.getStartIndex() + y * img.getStride();
      for (int x = 0; x < w; x++) {
        int value = (data[index]) + (int) (rand.nextGaussian() * sigma);
        if (value < min) value = min;
        if (value > max) value = max;

        data[index++] = value;
      }
    }
  }
Exemple #7
0
 // convert a blob image into quadrilaterals bounding each of the blobs
 public static List<List<Point2D_F64>> quadsFromBlobs(ImageSInt32 blobImage, int numBlobs) {
   @SuppressWarnings("unchecked")
   List<List<Point2D_F64>> points = new ArrayList();
   for (int i = 1; i < numBlobs; i++) {
     // TODO: make fast
     ArrayList<Point2D_F64> inClass = new ArrayList<Point2D_F64>();
     for (int x = 0; x < blobImage.getWidth(); x++) {
       for (int y = 0; y < blobImage.getHeight(); y++) {
         if (blobImage.get(x, y) == i) {
           inClass.add(new Point2D_F64(x, y));
         }
       }
     }
     // Transform blobs into quadrilaterals, ignoring blobs of very small size
     if (inClass.size() > BLOB_SIZE_THRESHOLD) {
       points.add(
           boofcv.alg.feature.detect.quadblob.FindBoundingQuadrilateral.findCorners(inClass));
     }
   }
   return points;
 }