示例#1
0
  boolean SMLconvolveFloat(ImageProcessor dupip, float[] kernel, int kw, int kh) {

    int width = dupip.getWidth();
    int height = dupip.getHeight();

    int x1 = 0;
    int y1 = 0;
    int x2 = x1 + width;
    int y2 = y1 + height;
    int uc = kw / 2;
    int vc = kh / 2;
    float[] pixels = (float[]) dupip.getPixels();
    float[] pixels2 = (float[]) dupip.getSnapshotPixels();
    if (pixels2 == null) pixels2 = (float[]) dupip.getPixelsCopy();

    double sum;
    int offset, i;
    boolean edgePixel;
    int xedge = width - uc;
    int yedge = height - vc;

    for (int y = y1; y < y2; y++) {
      for (int x = x1; x < x2; x++) {
        sum = 0.0;
        i = 0;
        edgePixel = y < vc || y >= yedge || x < uc || x >= xedge;
        for (int v = -vc; v <= vc; v++) {
          offset = x + (y + v) * width;
          for (int u = -uc; u <= uc; u++) {
            if (edgePixel) {
              if (i >= kernel.length) // work around for JIT compiler bug on Linux
              IJ.log("kernel index error: " + i);
              sum += SMLgetPixel(x + u, y + v, pixels2, width, height) * kernel[i++];
            } else sum += pixels2[offset + u] * kernel[i++];
          }
        }
        pixels[x + y * width] = (float) (sum);
      }
    }

    return true;
  }
示例#2
0
  /**
   * @param ip
   * @param v
   */
  public static void run(final ImageProcessor ip, final float v) {
    final int w = ip.getWidth();
    final int h = ip.getHeight();
    final int wh = w * h;

    final ImageProcessor ipTarget = ip.duplicate();

    int numSaturatedPixels = 0;
    do {
      numSaturatedPixels = 0;
      for (int i = 0; i < wh; ++i)
        if (ip.getf(i) == v) {
          ++numSaturatedPixels;

          final int y = i / w;
          final int x = i % w;

          float s = 0;
          float n = 0;
          if (y > 0) {
            if (x > 0) {
              final float tl = ip.getf(x - 1, y - 1);
              if (tl != v) {
                s += 0.5f * tl;
                n += 0.5f;
              }
            }
            final float t = ip.getf(x, y - 1);
            if (t != v) {
              s += t;
              n += 1;
            }
            if (x < w - 1) {
              final float tr = ip.getf(x + 1, y - 1);
              if (tr != v) {
                s += 0.5f * tr;
                n += 0.5f;
              }
            }
          }

          if (x > 0) {
            final float l = ip.getf(x - 1, y);
            if (l != v) {
              s += l;
              n += 1;
            }
          }
          if (x < w - 1) {
            final float r = ip.getf(x + 1, y);
            if (r != v) {
              s += r;
              n += 1;
            }
          }

          if (y < h - 1) {
            if (x > 0) {
              final float bl = ip.getf(x - 1, y + 1);
              if (bl != v) {
                s += 0.5f * bl;
                n += 0.5f;
              }
            }
            final float b = ip.getf(x, y + 1);
            if (b != v) {
              s += b;
              n += 1;
            }
            if (x < w - 1) {
              final float br = ip.getf(x + 1, y + 1);
              if (br != v) {
                s += 0.5f * br;
                n += 0.5f;
              }
            }
          }

          if (n > 0) ipTarget.setf(i, s / n);
        }
      ip.setPixels(ipTarget.getPixelsCopy());
    } while (numSaturatedPixels > 0);
  }