Exemplo n.º 1
0
def syntheticEM(fillLineIndices,
                width, height,
                lineValue, backgroundValue,
                rois=None,
                sigma=1.4,
                noise=False,
                noise_sd=25.0,
                show=False):
  ip = FloatProcessor(width, height)
  pixels = ip.getPixels()
  # Fill background
  Arrays.fill(pixels, backgroundValue)
  # Paint black horizontal line
  for i in fillLineIndices:
    Arrays.fill(pixels, i * width, (i + 1) * height, lineValue)
  if rois:
    ip.setColor(lineValue)
    for roi in rois:
      ip.draw(roi)
  # Blur
  GaussianBlur().blurFloat(ip, 0, sigma, 0.02)
  if noise:
    ip.noise(noise_sd)
  ip.setMinAndMax(0, 255)
  imp = ImagePlus("synth", ip)
  if show:
    imp.show()
  return imp
Exemplo n.º 2
0
 def test_zeros(self):
     if self.type is None:
         return
     array = jarray.zeros(2, self.type)
     self.assertValueEqual(len(array), 2)
     self.assertValueEqual(array[0], self.default_value)
     self.assertValueEqual(array[1], self.default_value)
     array[0] = self.instance
     self.assertValueEqual(array[0], self.instance)
     with self.assertRaises(TypeError):
         array[0] = self.bad_instance
     Arrays.fill(array, self.instance)
     self.assertValueEqual(array[0], self.instance)
     self.assertValueEqual(array[1], self.instance)
     self.assertRaises(TypeError, jarray.zeros, None)
Exemplo n.º 3
0
def unpackBits2(bytes_packedbits, tags, use_imagereader=False):
    # Decompress a packBits-compressed image, returns an array of n_bytes.
    # ra: a RandomAccessFile with the pointer at the right place to start reading.
    # PackBits actually packages bytes: a byte-wise RLE most efficient at encoding runs of bytes
    # 3 types of data packets:
    # 1. two-byte encoded run packet:
    #     - first byte is the number of bytes in the run.
    #       Ranges from -127 to -1, meaning +1: from 2 to 128 (-count + 1)
    #     - second byte value of each byte in the run.
    # 2. literal run packet: stores 1 to 128 bytes literally without compression.
    #     - first byte is the number of bytes in the run.
    #       Ranges from 0 to 127, indicating 1 to 128 values (count + 1)
    #     - then the sequence of literal bytes
    # 3. no-op packet: never used, value -128.
    # See documentation: http://paulbourke.net/dataformats/tiff/tiff_summary.pdf
    # (note documentation PDF has its details flipped when it comes to the ranges for literal runs and packed runs)
    # See also: ij.io.ImageReader.packBitsUncompress (a public method without side effects)

    n_bytes = tags["width"] * tags["height"]

    if use_imagereader:
        return ImageReader(FileInfo()).packBitsUncompress(
            bytes_packedbits, n_bytes)

    bytes = zeros(n_bytes, 'b')
    try:
        indexP = 0  # packed
        indexU = 0  # unpacked
        while indexU < n_bytes:
            count = bytes_packedbits[indexP]
            if count >= 0:
                # Literal run
                System.arraycopy(bytes_packedbits, indexP + 1, bytes, indexU,
                                 count + 1)
                indexP += count + 2  # one extra for the 'count' byte
                indexU += count + 1
            else:
                # Packed run
                Arrays.fill(bytes, indexU, indexU - count + 1,
                            bytes_packedbits[indexP + 1])
                indexP += 2
                indexU += -count + 1
    except:
        print sys.exc_info()
    finally:
        return bytes
Exemplo n.º 4
0
def unpackBits(ra, tags, use_imagereader=False):
    # Decompress a packBits-compressed image, write into bytes array starting at indexU.
    # ra: a RandomAccessFile with the pointer at the right place to start reading.
    # PackBits actually packages bytes: a byte-wise RLE most efficient at encoding runs of bytes
    # 3 types of data packets:
    # 1. two-byte encoded run packet:
    #     - first byte is the number of bytes in the run.
    #       Ranges from -127 to -1, meaning +1: from 2 to 128 (-count + 1)
    #     - second byte value of each byte in the run.
    # 2. literal run packet: stores 1 to 128 bytes literally without compression.
    #     - first byte is the number of bytes in the run.
    #       Ranges from 0 to 127, indicating 1 to 128 values (count + 1)
    #     - then the sequence of literal bytes
    # 3. no-op packet: never used, value -128.
    # See documentation: http://paulbourke.net/dataformats/tiff/tiff_summary.pdf
    # (note documentation PDF has its details flipped when it comes to the ranges for literal runs and packed runs)
    # See also: ij.io.ImageReader.packBitsUncompress (a public method without side effects)

    if use_imagereader:
        return ImageReader(FileInfo()).packBitsUncompress(
            getIFDImageBytes(ra, tags), tags["width"] * tags["height"])

    try:
        bytes = zeros(tags["width"] * tags["height"], 'b')
        indexU = 0  # index over unpacked bytes
        for strip_offset, strip_length in zip(tags["StripOffsets"],
                                              tags["StripByteCounts"]):
            ra.seek(strip_offset)
            indexP = 0
            while indexP < strip_length and indexU < len(bytes):
                count = ra.readByte()
                if count >= 0:
                    # Literal run
                    ra.read(bytes, indexU, count + 1)
                    indexP += count + 2  # one extra for the count byte
                    indexU += count + 1
                else:
                    # Packed run
                    Arrays.fill(bytes, indexU, indexU - count + 1,
                                ra.readByte())
                    indexP += 2
                    indexU += -count + 1
    except:
        print sys.exc_info()
    finally:
        return bytes
Exemplo n.º 5
0
        dp[0] = 0
        for money in range(1, amount+1):
            res = amount + 1
            for coin in coins:
                if money >= coin and dp[money-coin] != amount+1:
                    res = min(dp[money-coin]+1, res)
            dp[money] = res
        if dp[amount] == amount + 1:
            return -1
        return dp[amount]

class Solution {
    public int coinChange(int[] coins, int amount) {
    if (amount < 1) return 0;
    int[] dp = new int[amount + 1]; 
    Arrays.fill(dp, Integer.MAX_VALUE);
    dp[0] = 0;
    for (int coin : coins) {
        for (int i = coin; i <= amount; i++) {
            if (dp[i - coin] != Integer.MAX_VALUE) {
                dp[i] = Math.min(dp[i], dp[i - coin] + 1);
            }
        }
    }
    return dp[amount] == Integer.MAX_VALUE ? -1 : dp[amount];
}
}

class Solution {
    public int coinChange(int[] coins, int amount) {
        int[] dp = new int[amount+1];