Exemple #1
0
def test_rw_rgb():
    itype = uint8
    dt = dtype(dict(names = list('rgb'), formats = [itype]*3))
    
    image = zeros((2,3), dtype=dt)
    image['r'][:,0] = 250
    image['g'][:,1] = 251
    image['b'][:,2] = 252

    fn = mktemp('.tif')
    tif = TIFFimage(image)
    tif.write_file(fn,compression='lzw')#, samples='rgb')
    del tif

    tif = TIFFfile(fn)
    data, names = tif.get_samples()
    #os.remove(fn)
    atexit.register(os.remove, fn)
    print image
    print data

    assert itype == data[0].dtype, `itype, data[0].dtype`
    assert (image['r']==data[0]).all()
    assert (image['g']==data[1]).all()
    assert (image['b']==data[2]).all()
Exemple #2
0
def convert(filename, fileext='tif', force=False, compress=False):
    """Converts a RAXIS file to an image

       Arguments
       ---------
       filename : string
       fileext : string
         Either 'tif' or 'tiff'; since RAXIS data is 16-bit, it is of
         dubious utility to use other formats. Support for other 16-bit
         formats may be added in future
       force : string
         Whether to overwrite an existing image file of the same
         name.
    """
    #### Initial sanity checks
    print("Converting {}".format(filename))

    if fileext not in ['tif', 'tiff']:
        print("RAXIS data is 16-bit; at the moment, we therefore only support "
              "saving it to TIFF files.")
        return

    newfilename = ''.join([splitext(filename)[0], '.', fileext])
    if exists(newfilename):
        if not force:
            print("The file {} already exists. Re-run with --force "
                  "if you want to overwrite it.".format(newfilename))
            return

    data = read_raxis_file(filename)

    tiff = TIFFimage(data, description='RAXIS file converted to TIFF by raxis_to_image 0.0.3')
    tiff.write_file(newfilename, compression='none' if not compress else 'lzw')
    print('Converted {} to {}'.format(filename, newfilename))
Exemple #3
0
def test_write_read():

    for compression in [None, 'lzw']:
        for itype in [uint8, uint16, uint32, uint64, 
                      int8, int16, int32, int64,
                      float32, float64,
                      complex64, complex128]:
            image = array([[1,2,3], [4,5,6]], itype)
            fn = mktemp('.tif')

            if 0:
                tif = TIFF.open(fn,'w')
                tif.write_image(image, compression=compression)
                tif.close()
            else:
                tif = TIFFimage(image)
                tif.write_file(fn, compression=compression)
                del tif

            tif = TIFFfile(fn)
            data, names = tif.get_samples()
            assert names==['sample0'],`names`
            assert len(data)==1, `len(data)`
            assert image.dtype==data[0].dtype, `image.dtype, data[0].dtype`
            assert (image==data[0]).all()
            
            #os.remove(fn)
            atexit.register(os.remove, fn)
Exemple #4
0
def sci2tiff(input_file,
            force_input_min=None,
            force_input_max=None,
            fill_null_stripes=False,
            fillsat=False,
            dohisteq=False,
            minpercent=None,
            maxpercent=None,
            std_mult=None,
            resize=None,
            band=0,
            trim=0,
            outputformat="uint16"):

    pixel_matrix = load_image_matrix(input_file, band=band)
    pixel_matrix = process_data(pixel_matrix,
                                force_input_min,
                                force_input_max,
                                fill_null_stripes,
                                fillsat,
                                dohisteq,
                                minpercent,
                                maxpercent,
                                std_mult,
                                resize,
                                trim,
                                outputformat=outputformat)

    output_filename = build_output_filename(input_file)
    print "Writing", output_filename

    # Create output tiff
    tiff = TIFFimage(pixel_matrix, description='')
    tiff.write_file(output_filename, compression='none', verbose=False)
    return True
def sliding_window_crop(img, img_name, output_path, usage):
    height, width, _ = img.shape

    col_count = 0
    for y in range(0, height, int(height / 4)):
        row_count = 0
        for x in range(0, width, int(width / 4)):
            img_crop = img[y:y + int(height / 4), x:x + int(width / 4)]

            if usage == "img":
                crop_name = "_".join(
                    [img_name, "{:04d}_{:04d}".format(row_count, col_count)])
                img_file = os.path.join(output_path,
                                        ".".join([crop_name, "tif"]))
                tif = TIFFimage(img_crop)
                tif.write_file(img_file)
            else:
                crop_name = "_".join([
                    img_name,
                    "{:04d}_{:04d}_label".format(row_count, col_count)
                ])
                img_file = os.path.join(output_path,
                                        ".".join([crop_name, "tif"]))
                mask = cv2.cvtColor(img_crop, cv2.COLOR_BGR2RGB)
                cv2.imwrite(img_file, mask)

            row_count += 1

        col_count += 1
Exemple #6
0
def save_band(band, band_num, filter_num):
    img = np.copy(band)

    inds = np.where(np.isnan(img))
    img[inds] = np.nanmin(img)

    data_matrix = img #.astype(np.uint16)
    tiff = TIFFimage(data_matrix, description='')
    if not os.path.exists("bands"):
        os.mkdir("bands")
    tiff.write_file("bands/band_%d_%d.tif"%(filter_num, band_num), compression='none', verbose=False)
def FromProto(name, expe, var, alpha, nx = 100, ny = 100, sx = 25, sy = 25):
    image = np.zeros((nx * sx, ny * sy), dtype = np.uint16)
    proto = np.load(name)
    for i in range(nx):
        for j in range(ny):
            for m in range(sx):
                for n in range(sy):
                    esti = proto[i, j, m, n]
                    image[sx*i + m, sy*j + n] = int(round(random.normal(esti+expe, sqrt(alpha*esti + var))))
    tiff = TIFFimage(image, description = '')
    tiff.write_file('image', compression = 'none')
    del tiff
Exemple #8
0
def image_shifted(mean, std, sx = 21, sy = 21, nx = 101, ny = 101, kx=1, ky=1, initial = False, noisy = True, rep = 100):
    mx, my = mean.shape
    if mx<sx+1 or my<sy+1:
        return 0
    image = np.zeros((sx*nx,sy*ny),dtype=np.uint16)
    fimage = np.zeros((sx*nx,sy*ny))
    fvar = np.zeros((sx*nx,sy*ny))
    if initial:
       for m in range(nx):
           for n in range(ny):
               image[m*sx:(m+1)*sx, n*sy:(n+1)*sy] = np.int_(np.round_(mean[0:sx,0:sy]))
       tiff = TIFFimage(image, description = '')
       tiff.write_file('image_ini', compression = 'none')
       del tiff
       return 0

    accum=np.zeros((sx+2, sy+2))
    #sub = np.zeros((sx+1, sy+1))

    for m in range(nx):
        for n in range(ny):
            for i in range(sx+2):
                for j in range(sy+2):
                    accum[i,j] = mean[0:i,0:j].sum()

            spline = interpolate.RectBivariateSpline(np.arange(0,sx+2),np.arange(0,sy+2),accum,kx=kx,ky=ky,s=0)
            subimage = fimage[m*sx:(m+1)*sx,n*sy:(n+1)*sy]
            for i in range(sx):
                for j in range(sy):
                    subimage[i,j] = spline.ev(m/(nx-1.)+i+1,n/(ny-1.)+j+1)-spline.ev(m/(nx-1.)+i,n/(ny-1.)+j+1)-spline.ev(m/(nx-1.)+i+1,n/(ny-1.)+j)+spline.ev(m/(nx-1.)+i,n/(ny-1.)+j)
    var = std**2
    if noisy:
        for m in range(nx):
            for n in range(ny):
                for i in range(sx+2):
                    for j in range(sy+2):
                        accum[i,j] = var[0:i,0:j].sum()
                spline = interpolate.RectBivariateSpline(np.arange(0,sx+2),np.arange(0,sy+2),accum,kx=kx,ky=ky,s=0)
                subvar = fvar[m*sx:(m+1)*sx,n*sy:(n+1)*sy]
                for i in range(sx):
                    for j in range(sy):
                        subvar[i,j] = spline.ev(m/(nx-1.)+i+1,n/(ny-1.)+j+1)-spline.ev(m/(nx-1.)+i,n/(ny-1.)+j+1)-spline.ev(m/(nx-1.)+i+1,n/(ny-1.)+j)+spline.ev(m/(nx-1.)+i,n/(ny-1.)+j)
        for k in range(100):
            for i in range(sx*nx):
                for j in range(sy*ny):
                    image[i,j] = int(round(random.normal(fimage[i,j],np.sqrt(fvar[i,j]))))
            tiff = TIFFimage(image, description = '')
            tiff.write_file('image%i'%k, compression = 'none')
            del tiff
    else:
        for i in range(sx*nx):
            for j in range(sy*ny):
                image[i,j] = int(round(fimage[i,j]))
        tiff = TIFFimage(image, description = '')
        tiff.write_file('image', compression = 'none')
        del tiff
    return 0
def test_rw_rgb():
    itype = uint8
    dt = dtype(dict(names=list('rgb'), formats=[itype]*3))

    image = zeros((2, 3), dtype=dt)
    image['r'][:, 0] = 250
    image['g'][:, 1] = 251
    image['b'][:, 2] = 252

    fn = mktemp('.tif')
    tif = TIFFimage(image)
    tif.write_file(fn, compression='lzw')  # , samples='rgb')
    del tif

    tif = TIFFfile(fn)
    data, names = tif.get_samples()
    tif.close()
    atexit.register(os.remove, fn)
    print(image)
    print(data)

    assert itype == data[0].dtype, repr((itype, data[0].dtype))
    assert (image['r'] == data[0]).all()
    assert (image['g'] == data[1]).all()
    assert (image['b'] == data[2]).all()
Exemple #10
0
def test_write_read():

    for compression in [None, 'lzw']:
        for itype in [
                uint8, uint16, uint32, uint64, int8, int16, int32, int64,
                float32, float64, complex64, complex128
        ]:
            image = array([[1, 2, 3], [4, 5, 6]], itype)
            fn = mktemp('.tif')

            if 0:
                tif = TIFF.open(fn, 'w')
                tif.write_image(image, compression=compression)
                tif.close()
            else:
                tif = TIFFimage(image)
                tif.write_file(fn, compression=compression)
                del tif

            tif = TIFFfile(fn)
            data, names = tif.get_samples()
            assert names == ['sample0'], repr(names)
            assert len(data) == 1, repr(len(data))
            assert image.dtype == data[0].dtype, repr(
                (image.dtype, data[0].dtype))
            assert (image == data[0]).all()
            tif.close()
            atexit.register(os.remove, fn)
Exemple #11
0
def test_write_lzw():
    for itype in [
            uint8, uint16, uint32, uint64, int8, int16, int32, int64, float32,
            float64, complex64, complex128
    ]:
        #image = array([[1,2,3], [4,5,6]], itype)
        image = array([range(10000)], itype)
        #image = array([[0]*14000], itype)
        fn = mktemp('.tif')
        tif = TIFFimage(image)
        tif.write_file(fn, compression='lzw')
        del tif

        #os.system('wc %s; echo %s' % (fn, image.nbytes))

        tif = TIFF.open(fn, 'r')
        image2 = tif.read_image()
        tif.close()
        #os.remove(fn)
        atexit.register(os.remove, fn)
        for i in range(image.size):
            if image.flat[i] != image2.flat[i]:
                print ` i, image.flat[i - 5:i + 5].view(
                    dtype=uint8), image2.flat[i - 5:i + 5].view(dtype=uint8) `
                break

        assert image.dtype == image2.dtype
        assert (image == image2).all()
Exemple #12
0
def process_image(img_path, save_file_path=None, verbose=False):

    if verbose:
        print("Filling dead pixels in %s" % img_path)

    data = open_image(img_path)

    fillpixels(data, verbose=verbose)

    if save_file_path is None:
        save_file_path = "%s-filled.tif" % (img_path[0:-4])

    if verbose:
        print("Saving processed data to", save_file_path)

    tiff = TIFFimage(data, description='')
    tiff.write_file(save_file_path, compression='none', verbose=False)

    return save_file_path
Exemple #13
0
def main ():
    from libtiff import TIFFimage
    #import matplotlib.pyplot as plt

    N = 400
    for i in range (N):
        L = 1.8 + 0.2*numpy.cos(max(0,i-N//5)/N*2*numpy.pi)
        print i,L
        image = compute_image(L, 2.25, 1.5, 100, 640, 0.274)
        tif = TIFFimage(image.astype(numpy.uint8).T, description='''
VoxelSizeX: 0.274e-6
VoxelSizeY: 0.274e-6
VoxelSizeZ: 1
MicroscopeType: widefield
ObjectiveNA: 1.2
ExcitationWavelength: 540.0
RefractiveIndex: 1.33
''')
        tif.write_file ('fakesacromere_exact/image_%06d.tif' % i)
        del tif
def test_write_read():
    for compression in ['none', 'lzw']:
        for itype in [uint8, uint16, uint32, uint64, 
                      int8, int16, int32, int64,
                      float32, float64,
                      complex64, complex128]:
            image = array([[1,2,3], [4,5,6]], itype)
            fn = mktemp('.tif')
            tif = TIFFimage(image)
            tif.write_file(fn, compression=compression)
            del tif
            
            tif = TIFFfile(fn)
            data, names = tif.get_samples()
            #os.remove(fn)
            atexit.register(os.remove, fn)
            assert names==['sample0'],repr(names)
            assert len(data)==1, repr(len(data))
            assert image.dtype==data[0].dtype, repr((image.dtype,data[0].dtype))
            assert (image==data[0]).all()
Exemple #15
0
def main():
    from libtiff import TIFFimage
    #import matplotlib.pyplot as plt

    N = 400
    for i in range(N):
        L = 1.8 + 0.2 * numpy.cos(max(0, i - N // 5) / N * 2 * numpy.pi)
        print i, L
        image = compute_image(L, 2.25, 1.5, 100, 640, 0.274)
        tif = TIFFimage(image.astype(numpy.uint8).T,
                        description='''
VoxelSizeX: 0.274e-6
VoxelSizeY: 0.274e-6
VoxelSizeZ: 1
MicroscopeType: widefield
ObjectiveNA: 1.2
ExcitationWavelength: 540.0
RefractiveIndex: 1.33
''')
        tif.write_file('fakesacromere_exact/image_%06d.tif' % i)
        del tif
Exemple #16
0
def test_write_lzw():
    for itype in [uint8, uint16, uint32, uint64, 
                  int8, int16, int32, int64,
                  float32, float64,
                  complex64, complex128]:
        #image = array([[1,2,3], [4,5,6]], itype)
        image = array([range(10000)], itype)
        #image = array([[0]*14000], itype)
        fn = mktemp('.tif')
        tif = TIFFimage(image)
        tif.write_file(fn, compression='lzw')
        del tif

        #os.system('wc %s; echo %s' % (fn, image.nbytes))

        tif = TIFF.open(fn,'r')
        image2 = tif.read_image()
        tif.close()
        #os.remove(fn)
        atexit.register(os.remove, fn)
        for i in range(image.size):
            if image.flat[i] != image2.flat[i]:
                print `i, image.flat[i-5:i+5].view(dtype=uint8),image2.flat[i-5:i+5].view(dtype=uint8)`
                break

        assert image.dtype==image2.dtype
        assert (image==image2).all()
def test_simple_slicing():
    for planar_config in [1,2]:
        for compression in [None, 'lzw']:
            for itype in [uint8, uint16, uint32, uint64, 
                          int8, int16, int32, int64,
                          float32, float64,
                          complex64, complex128]:
                image = random.randint(0, 100, size=(10,6,7)).astype(itype)
                fn = mktemp('.tif')

                if 0:
                    if planar_config == 2:
                        continue
                    tif = TIFF.open(fn, 'w')
                    tif.write_image(image, compression=compression)
                    tif.close()
                else:
                    tif = TIFFimage(image)
                    tif.write_file(fn,
                                   compression=compression,
                                   planar_config=planar_config)
                    del tif

                tif = TIFFfile(fn)
                arr = tif.get_tiff_array()
                data = arr[:]
                assert len(data) == len(image), repr(len(data))
                assert image.dtype == data.dtype, repr((image.dtype,
                                                       data[0].dtype))
                assert (image == data).all()
                assert arr.shape == image.shape

                _indices = [0, slice(None), slice(0, 2), slice(0, 5, 2)]
                for _i0 in _indices[:1]:
                    for i1 in _indices:
                        for i2 in _indices:
                            sl = (_i0, i1, i2)
                            assert (arr[sl] == image[sl]).all(),repr(sl)
                atexit.register(os.remove, fn)
def test_issue69():
    itype = np.uint32
    image = np.array([[[1, 2, 3], [4, 5, 6]]], itype)
    fn = mktemp('issue69.tif')
    tif = TIFFimage(image)
    tif.write_file(fn)
    del tif
    tif = lt.TIFF3D.open(fn)
    tif.close()
    atexit.register(os.remove, fn)
def rwlibtiff(x, fn):
    """
    It seems with verion 0.4.0 that it requires Python 2.7, but I get a
    segmentation fault even with Python 2.7
    """
    from libtiff import TIFFimage, TIFF
    with TIFFimage(x, description='my test data') as tf:
        print('libtiff write ' + fn)

        #write demo
        tf.write_file(str(fn), compression='none')

    #read demo
    with TIFF.open(str(fn), mode='r') as tif:

        return tif.read_image()
def rwlibtiff(x, fn):
    """
    It seems with verion 0.4.0 that it requires Python 2.7, but I get a
    segmentation fault even with Python 2.7
    """
    from libtiff import TIFFimage, TIFF
    with TIFFimage(x, description='my test data') as tf:
        print(f'libtiff write {fn}')

        #write demo
        tf.write_file(str(fn), compression='none')

    #read demo
    with TIFF.open(str(fn), mode='r') as tif:

        return tif.read_image()
        # for image in tif.iter_images():

# %% tifffile
    if 'tifffile' in modules:
        ofn = mkstemp('.tif', 'tifffile')[1]

        tic = time()
        write_multipage_tiff(imgs,
                             ofn,
                             descr='0 to 9 numbers',
                             tags=[
                                 (65000, 's', None, 'My custom tag #1', True),
                                 (65001, 's', None, 'My custom tag #2', True),
                                 (65002, 'f', 2, [123456.789,
                                                  9876.54321], True)
                             ])
        y = read_multipage_tiff(ofn)
        print(f'{time()-tic:.6f} seconds to read/write {ofn} with tifffile.')
        assert (y == imgs).all(), 'tifffile read/write equality failure'

        # %%
    if 'libtiff' in modules:
        tic = time()
        ofn = mkstemp('.tif', 'libtiff')[1]
        y = rwlibtiff(imgs, ofn)
        print(f'{time()-tic:.6f} seconds to read/write {ofn} with libtiff.')
        assert (y == imgs).all(), 'libtiff read/write equality failure'
    return y
Exemple #21
0
def test_issue19():
    size = 1024 * 32  # 1GB

    # size = 1024*63  # almost 4GB, test takes about 60 seconds but succeeds
    image = ones((size, size), dtype=uint8)
    print('image size:', image.nbytes / 1024**2, 'MB')
    fn = mktemp('issue19.tif')
    tif = TIFFimage(image)
    try:
        tif.write_file(fn)
    except OSError as msg:
        if 'Not enough storage is available to process this command'\
           in str(msg):
            # Happens in Appveyour CI
            del tif
            atexit.register(os.remove, fn)
            return
        else:
            raise
    del tif
    tif = TIFFfile(fn)
    tif.get_tiff_array()[:]  # expected failure
    tif.close()
    atexit.register(os.remove, fn)
def test_simple_slicing():
    for planar_config in [1, 2]:
        for compression in [None, 'lzw']:
            for itype in [
                    uint8, uint16, uint32, uint64, int8, int16, int32, int64,
                    float32, float64, complex64, complex128
            ]:
                image = random.randint(0, 100, size=(10, 6, 7)).astype(itype)
                fn = mktemp('.tif')

                if 0:
                    if planar_config == 2:
                        continue
                    tif = TIFF.open(fn, 'w')
                    tif.write_image(image, compression=compression)
                    tif.close()
                else:
                    tif = TIFFimage(image)
                    tif.write_file(fn,
                                   compression=compression,
                                   planar_config=planar_config)
                    del tif

                tif = TIFFfile(fn)
                arr = tif.get_tiff_array()
                data = arr[:]
                assert len(data) == len(image), repr(len(data))
                assert image.dtype == data.dtype, repr(
                    (image.dtype, data[0].dtype))
                assert (image == data).all()
                assert arr.shape == image.shape

                _indices = [0, slice(None), slice(0, 2), slice(0, 5, 2)]
                for _i0 in _indices[:1]:
                    for i1 in _indices:
                        for i2 in _indices:
                            sl = (_i0, i1, i2)
                            assert (arr[sl] == image[sl]).all(), repr(sl)
                tif.close()
                atexit.register(os.remove, fn)
def SpotArray(es_l, es_h, num = 100):
    es_limage, es_himage = np.load(es_l), np.load(es_h)
    sizex, sizey = es_limage.shape
    limage, himage = np.zeros((sizex, sizey * num), np.uint16), np.zeros((sizex, sizey * num), np.uint16)
    for k in range(num):
        for i in range(sizex):
            for j in range(sizey):
                lesti, hesti = es_limage[i, j], es_himage[i, j]
                limage[i, j + sizey * k] = int(round(random.normal(lesti+expe, sqrt(alpha*lesti + var))))
                himage[i, j + sizey * k] = int(round(random.normal(hesti+expe, sqrt(alpha*hesti + var))))
    tiff = TIFFimage(limage, description = '')
    tiff.write_file('limage', compression = 'none')
    del tiff
    tiff = TIFFimage(himage, description = '')
    tiff.write_file('himage', compression = 'none')
    del tiff
    return limage, himage
Exemple #24
0
def test_write_read():
    for compression in ['none', 'lzw']:
        for itype in [
                uint8, uint16, uint32, uint64, int8, int16, int32, int64,
                float32, float64, complex64, complex128
        ]:
            image = array([[1, 2, 3], [4, 5, 6]], itype)
            fn = mktemp('.tif')
            tif = TIFFimage(image)
            tif.write_file(fn, compression=compression)
            del tif

            tif = TIFFfile(fn)
            data, names = tif.get_samples()
            #os.remove(fn)
            atexit.register(os.remove, fn)
            assert names == ['sample0'], ` names `
            assert len(data) == 1, ` len(data) `
            assert image.dtype == data[0].dtype, ` image.dtype, data[0].dtype `
            assert (image == data[0]).all()
Exemple #25
0
    acq_time = dt.datetime.strptime(meta.get_item('EXIF:CreateDate'),
                                    '%Y:%m:%d %H:%M:%S')
    # Convert acquisition time to julian to do time-dependent interpolation
    acq_time_julian = pd.Series([0], index=[acq_time]).index \
     .to_julian_date().values

    # Calculate rad2refl factor
    m = refl_factors.loc[band, 'm']
    c = refl_factors.loc[band, 'c']
    rad2refl = m * acq_time_julian + c

    # Apply corrections/conversions
    fl_im_refl_cor = mc.calibrate_correct_image(fl_im_raw, meta, rad2refl)

    # Create a tiff structure from image data
    tiff = TIFFimage(fl_im_refl_cor, 'reflectance')
    filename = args.flight_loc + 'refl/' + fl_im_name.split(args.flight_loc +
                                                            'raw/')[1]
    tiff.write_file(filename, compression='none')  # or 'lzw'
    del tiff  # flushes data to disk

    # Copy metadata to refl TIFF from radiance TIFF
    # Note that if calibration model is provided by user then this won't be
    # written to output tiff.
    cmd = 'exiftool %s -overwrite_original -q -tagsFromFile %s' % (filename,
                                                                   fl_im_name)
    subprocess.call(cmd, shell=True)

    # Increment display counter
    n += 1
Exemple #26
0
def vic2tif(input_file,
            force_input_min=None,
            force_input_max=None,
            fill_null_stripes=False,
            fillsat=False,
            dohisteq=False,
            minpercent=None,
            maxpercent=None,
            resize=None):

    pixel_matrix, value_pairs = vicar.load_vic(input_file)
    if pixel_matrix is None or value_pairs is None:
        return False

    # Scale to 0-65535 and convert to UInt16
    if force_input_min is not None:
        pixel_min = force_input_min
    else:
        pixel_min = np.nanmin(pixel_matrix)

    if force_input_max is not None:
        pixel_max = force_input_max
    else:
        pixel_max = np.nanmax(pixel_matrix)

    print "Minimum Native:", pixel_min, "Maximum Native:", pixel_max


    if fill_null_stripes is True:
        stripes = detect_null_stripes(pixel_matrix)
        fill_stripes(pixel_matrix, stripes)

    # The min/max percent stuff isn't correct. TODO: Make it correct.
    if minpercent is not None:
        diff = pixel_min + ((pixel_max - pixel_min) * (minpercent / 100.0))
        print "Min:", diff
        pixel_matrix[pixel_matrix < diff] = diff
        pixel_min = diff

    if maxpercent is not None:
        diff = pixel_min + ((pixel_max - pixel_min) * (maxpercent / 100.0))
        print "Max:", diff
        pixel_matrix[pixel_matrix > diff] = diff
        pixel_max = diff

    if fillsat is True:
        inds = np.where(np.isnan(pixel_matrix))
        pixel_matrix[inds] = np.nanmax(pixel_matrix)

    pixel_matrix = pixel_matrix - pixel_min
    pixel_matrix = pixel_matrix / (pixel_max - pixel_min)
    pixel_matrix[pixel_matrix < 0] = 0

    # Format for UInt16
    pixel_matrix = pixel_matrix * 65535.0
    pixel_matrix = pixel_matrix.astype(np.uint16)
    #print pixel_matrix.min(), pixel_matrix.max()

    if dohisteq is True:
        pixel_matrix = histeq(pixel_matrix)

    if resize is not None:
        pixel_matrix = imresize(pixel_matrix, size=resize, interp='bicubic')

    output_filename = build_output_filename(value_pairs)
    print "Writing", output_filename

    # Create output tiff
    tiff = TIFFimage(pixel_matrix, description='')
    tiff.write_file(output_filename, compression='none')
    return True
Exemple #27
0
    def iter_Image(self, func):
        sys.stdout.write('iter_Image: reading image data from TIFF files\n')
        for detector in self.data:
            sys.stdout.write('  detector: %s\n' % (detector))
            d_index = self.data[detector]

            # write the content of tiff files to a single raw files
            f,fn,dtype = None, None, None
            time_set = set()
            mn, mx = None, None
            mnz = float(self.config['PROTOCOL_Z_STACKER_Minimum'])
            mxz = float(self.config['PROTOCOL_Z_STACKER_Maximum'])
            nz = int(self.config['PROTOCOL_Z_STACKER_NumberOfFrames'])
            if nz > 1:
                dz = (mxz-mnz)/(nz-1)
            else:
                dz = 0
            plane_l = []
            ti = -1

            exptime = '0'
            if detector=='Confocal':
                exptime = float(self.config['CONFOCAL_PixelAcqusitionTime']) * 1e-6
            elif detector=='Andor':
                exptime = self.config['CAMERA_ANDOR_ExposureTime']
            elif detector=='Imperx':
                for line in  self.config['CAMERA_IMPERX_HardwareInformation'].split('\n'):
                    if line.startswith ('Exposure time:'):
                        v,u = line[14:].lstrip().split()
                        v = v.strip (); u = u.strip ()
                        if u=='usec': exptime = float(v)*1e-6
                        elif u=='msec': exptime = float(v)*1e-3
                        elif u=='sec': exptime = float(v)
                        else:
                            raise NotImplementedError (`v,u,line`)
            else:
                raise NotImplementedError(`detector`)

            for t, index in sorted(d_index):
                if t not in time_set:
                    time_set.add(t)
                    ti += 1
                    zi = 0
                else:
                    zi += 1
                z = mnz + dz * zi
                d = dict(DeltaT=str(t), TheT=str(ti), TheZ = str(zi), PositionZ=str(z), TheC='0', ExposureTime=str(exptime))
                plane_l.append(d)

                tif = TIFFfile(d_index[t, index])
                samples, sample_names = tif.get_samples()
                assert len (sample_names)==1,`sample_names`
                data = samples[0]
                if mn is None:
                    mn, mx = data.min(), data.max()
                else:
                    mn = min (data.min(), mn)
                    mx = min (data.max(), mx)
                if f is None:
                    shape = list(data.shape)
                    dtype = data.dtype
                    fn = tempfile.mktemp(suffix='.raw', prefix='%s_%s_' % (detector, dtype))
                    f = open (fn, 'wb')
                else:
                    assert dtype is data.dtype,`dtype,data.dtype`
                    shape[0] += 1
                data.tofile(f)

                sys.stdout.write('\r  copying TIFF image data to RAW file: %5s%% done' % (int(100.0*(index+1)/len(d_index))))
                sys.stdout.flush()

            if f is None:
                continue
            f.close ()
            shape = tuple (shape)

            xsz = shape[2]
            ysz = shape[1]
            tsz = len(time_set)
            zsz = shape[0] // tsz
            order = 'XYZTC'
            sys.stdout.write("\n  RAW file contains %sx%sx%sx%sx%s [%s] array, dtype=%s, MIN/MAX=%s/%s\n" \
                                 % (xsz, ysz,zsz,tsz,1,order, dtype, mn,mx))
            assert zsz*tsz==shape[0],`zsz,tsz,shape`

            tif_filename = '%s%s.ome.tif' % (self.file_prefix, detector)
            sys.stdout.write("  creating memmap image for OME-TIF file %r..." % (tif_filename))
            sys.stdout.flush()
            mmap = numpy.memmap(fn, dtype=dtype, mode='r', shape=shape)
            tif_image = TIFFimage(mmap)
            atexit.register(os.remove, fn)
            tif_uuid = self._mk_uuid()
            self.tif_images[detector, tif_filename, tif_uuid] = tif_image
            sys.stdout.write (' done\n')
            sys.stdout.flush()


            pixels_d = {}
            channel_d = dict(SamplesPerPixel='1')
            lpath_l = []
            #channel_d todo: ExcitationWavelength, EmissionWavelength, Fluor, NDFilter, PockelCellSetting, Color 
            if detector in ['Confocal']:
                objective = ome.ObjectiveSettings(ID='Objective:%s' % (self.config['olympus_optics_objective']))
                instrument_id = 'Instrument:Airy'
                pixels_d['PhysicalSizeX'] = str(self.config['CONFOCAL_PixelSizeX'])
                pixels_d['PhysicalSizeY'] = str(self.config['CONFOCAL_PixelSizeY'])
                pixels_d['TimeIncrement'] = str(self.config['CONFOCAL_TimeBetweenFrames'])
                channel_d['Name'] = 'Confocal'
                channel_d['IlluminationType'] = 'Epifluorescence'
                channel_d['PinholeSize'] = '180'
                # todo: FluorescenceCorrelationSpectroscopy
                channel_d['AcquisitionMode'] = 'LaserScanningConfocalMicroscopy'

                for i in range (1,5):
                    d1 = 'AOTFLine%s' % i
                    ft = confocal_filters.get(d1)
                    if ft is None:
                        continue
                    fn = ft['ex'][0]
                    fn = get_aotf_filter_name (fn, self.config)
                    if 'OFF' in fn:
                        continue
                    lpath_l.append(ome.ExcitationFilterRef(ID='Filter:%s:%s' % (d1,fn)))
                fn = confocal_filters['OpticalTableSplitter']['di'][0]
                lpath_l.append(ome.DichroicRef(ID='Dichroic:OpticalTableSplitter:%s' % (fn)))
                d1 = 'ThorlabsWheelPosition%s' % (self.config['thorlabs_filter_wheel_position'][3])
                ft = confocal_filters.get(d1)
                if ft is not None:
                    fn = ft['em'][0]
                    lpath_l.append(ome.EmissionFilterRef (ID='Filter:%s:%s' % (d1,fn)))
            elif detector in ['Andor', 'Imperx']:
                objective = ome.ObjectiveSettings(ID='Objective:%s' % (self.config['optics_objective']))
                instrument_id = 'Instrument:Suga'
                channel_d['Name'] = '%s camera' % (detector)
                channel_d['AcquisitionMode'] = 'WideField'
                pixels_d['PhysicalSizeX'] = pixels_d['PhysicalSizeY'] = str(self.config['CAMERA_%s_PixelSize' % (detector.upper ())])
                tbf = float(self.config['CAMERA_%s_TimeBetweenFrames' % (detector.upper ())])
                d1 = 'NikonTurretTopCube%s' % (self.config['top_turret_cube'][3])
                d2 = 'NikonTurretBottomCube%s' % (self.config['bottom_turret_cube'][3])
                top_cube = nikon_filters[d1]
                bottom_cube = nikon_filters[d2]
                if detector=='Andor':
                    channel_d['IlluminationType'] = 'Epifluorescence'
                    if self.config['CAMERA_ANDOR_FrameTransferMode']=='1':
                        m = re.search(r'Kinetic cycle time:\s*(?P<time>\d+[.]\d*)\s*sec', self.config['CAMERA_ANDOR_HardwareInformation'], re.M)
                        pixels_d['TimeIncrement'] = str(m.group('time'))
                    else:
                        pixels_d['TimeIncrement'] = str(tbf)
                    if 'ex' in top_cube:
                        fn = top_cube['ex'][0]
                        lpath_l.append(ome.ExcitationFilterRef(ID='Filter:%s:%s' % (d1,fn)))
                    if 'di' in top_cube:
                        fn = top_cube['di'][0]
                        lpath_l.append(ome.DichroicRef(ID='Dichroic:%s:%s' % (d1,fn)))
                    if 'em' in top_cube:
                        fn = top_cube['em'][0]
                        lpath_l.append(ome.EmissionFilterRef(ID='Filter:%s:%s' % (d1,fn)))
                    if 'ex' in bottom_cube:
                        fn = bottom_cube['ex'][0]
                        lpath_l.append(ome.EmissionFilterRef(ID='Filter:%s:%s' % (d2,fn)))
                else:
                    #m = re.search(r'Exposure time:\s*(?P<time>\d+[.]\d*)\s*msec', self.config['CAMERA_IMPERX_HardwareInformation'], re.M)
                    #exp_time = float (m.group ('time'))
                    if self.config['main_protocol_mode'].startswith('MyocyteMechanicsFluorescence'):
                        tbf = float(self.config['PROTOCOL_MYOCYTE_MECHANICS_TimeBetweenSavedFrames'])
                    pixels_d['TimeIncrement'] = str(tbf)
                    channel_d['IlluminationType'] = 'Transmitted'
                    if self.config['optics_transmission_light_filter']!='Empty':
                        fn = nikon_filters['NikonIllumination']['ex'][0]
                        lpath_l.append(ome.ExcitationFilterRef(ID='Filter:NikonIllumination:%s' % (fn)))
                    if 'di' in top_cube:
                        fn = top_cube['di'][0]
                        lpath_l.append(ome.EmissionFilterRef(ID='Filter:%s:%s' % (d1, fn)))
                    if 'em' in top_cube:
                        fn = top_cube['em'][0]
                        lpath_l.append(ome.EmissionFilterRef(ID='Filter:%s:%s' % (d1,fn)))
                    if 'em' in bottom_cube:
                        fn = bottom_cube['em'][0]
                        lpath_l.append(ome.EmissionFilterRef(ID='Filter:%s:%s' % (d2,fn)))
                    if 'di' in bottom_cube:
                        fn = bottom_cube['di'][0]
                        lpath_l.append(ome.EmissionFilterRef(ID='Filter:%s:%s' % (d2,fn)))
            else:
                raise NotImplementedError (`detector`)

            if zsz>1:
                pixels_d['PhysicalSizeZ'] = str(dz)
            channel = ome.Channel(ID='Channel:%s' % (detector),
                                  **channel_d)

            lpath = ome.LightPath(*lpath_l)
            channel.append (lpath)

            #todo attributes: 
            #todo elements: BIN:BinData, MetadataOnly, SA:AnnotationRef
            tiffdata = ome.TiffData(ome.UUID (tif_uuid, FileName=tif_filename))
            pixels = ome.Pixels(channel,
                                tiffdata, 
                                DimensionOrder=order, ID='Pixels:%s' % (detector),
                                SizeX = str(xsz), SizeY = str(ysz), SizeZ = str(zsz), SizeT=str(tsz), SizeC = str(1),
                                Type = self.dtype2PixelIType (dtype),
                                **pixels_d
                                )
            for d in plane_l:
                pixels.append(ome.Plane(**d))
            #todo attributes: Name
            #todo elements: Description, ExperimentRef, DatasetRef ,
            #               ImagingEnvironment, StageLabel, ROIRef, MicrobeamManipulationRef, AnnotationRef

            image = ome.Image (ome.AcquiredDate (self.get_AcquiredDate()),
                               ome.ExperimenterRef(ID='Experimenter:%s' % (self.current_user)),
                               ome.GroupRef(ID='Group:SysBio'),
                               ome.InstrumentRef(ID=instrument_id),
                               objective,
                               pixels, 
                               ID='Image:%s' % (detector))

            if 0:
                image.append(sa.AnnotationRef (ID='Annotation:configuration.txt'))
            yield image
        return
Exemple #28
0
 def save(self, path):
     data_matrix = self.data.astype(np.uint16)
     tiff = TIFFimage(data_matrix, description='')
     tiff.write_file(path, compression='none', verbose=False)
    bzero = hdu.header['bzero']

    #is the image flipped? No worries!
    d = hdu.data[::-1,:]

    #removing mask (if I understood this bit of the spec correctly)
    d[d<bzero] = bzero

    mi = numpy.min(d)
    ma = numpy.max(d)
    print d.shape, d.dtype,mi,ma

    #mostly, we want 16 bit images...
    if bitpix != 16:
        d = 65535*(d - mi)/(ma-mi)

    print "writing 16 bit tiff..."
    d16 = d.astype(numpy.uint16)
    tiff = TIFFimage(d16,
        description='http://tdc-www.harvard.edu/postage_stamp/')
    tiff.write_file(fn+'.tif', compression='lzw')
    #flush the file to disk:
    del tiff

    print "writing equalized 8 bit png and jpeg..."
    d8,cdf = histeq(d,65536)
    i = Image.fromarray(d8)
    i.save(fn+'.png')
    i.save(fn+'.jpg')

import tifffile as tf
from libtiff import TIFFimage

src_p = r"Z:\rabies_tracing_project\M533921" \
        r"\2020-10-12-align-sections\step02_align_sections" \
        r"\201026-M533921-merged-GCaMP.tif"

curr_folder = os.path.dirname(os.path.realpath(__file__))
os.chdir(curr_folder)

save_folder, save_name = os.path.split(src_p)
save_name = f'{os.path.splitext(save_name)[0]}_libtiff.tif'
save_path = os.path.join(save_folder, save_name)

print(f'reading {src_p} ...')
img = tf.imread(src_p)

print(f'\timage datatype: {img.dtype}.')
print(f'\timage shape: {img.shape}.')

if len(img.shape) > 3:
    raise NotImplementedError(
        f'pylibtiff cannot convert image with more than 3 dimensions. '
        f'Current number of dimensions: {len(img.shape)}.')

print(f'\tsaving {save_path} ...')
tiff = TIFFimage(img, description='')
tiff.write_file(save_name, compression='none')  # or 'lzw'
del tiff

print('\tdone.')
                    esti = proto[i, j, m, n]
                    image[sx*i + m, sy*j + n] = int(round(random.normal(esti+expe, sqrt(alpha*esti + var))))
    tiff = TIFFimage(image, description = '')
    tiff.write_file('image', compression = 'none')
    del tiff
"""
mean_var = np.load('mean_var.npy')
var = -mean_var[0]
expe = mean_var[1]
fit = np.load('hklfit.npy')
alpha = fit.item().values()[0][0]
FromProto('prototype new.npy', expe = expe, var = var, alpha = alpha)
"""
proto = np.load('prototype new.npy')
subimage = np.zeros((25, 25), dtype=np.uint16)
mean_var = np.load('mean_var.npy')
var = -mean_var[0]
expe = mean_var[1]
fit = np.load('hklfit.npy')
alpha = fit.item().values()[0][0]
for m in range(25):
    for n in range(25):
        esti = proto[0, 0, m, n]
        subimage[m, n] = int(round(random.normal(esti+expe, sqrt(alpha*esti + var))))
image = np.zeros((2500, 2500), dtype=np.uint16)
for i in range(100):
    for j in range(100):
        image[i*25: (i+1)*25, j*25: (j+1)*25] = subimage
tiff = TIFFimage(image, description = '')
tiff.write_file('ini_image', compression = 'none')
Exemple #32
0
def save_tif(img, newname, headers):
    print("Opening filename")
    img_tif = numpy.array(img, dtype=numpy.uint16)
    newname_tif = newname
    newname_tif += ".tif"
    info_tiff = []
    try:
        binning = int(headers['Binning'])
        binning += 1
    except Exception as e:
        print("Binning Type Error ->" + str(e))
        binning = " ??? "
    try:
        print("Tricat of save_tif")
        day_hour = get_date_hour_image_for_headers(str(headers['Start Time']))

        try:
            info_tiff.append('Binning: ' + str(binning) + "x" + str(binning) +
                             ';')
            info_tiff.append('CCD SET TEMP: ' +
                             str(headers['Set Temperature']) + ' Deg.' + ';')
            info_tiff.append('CCD Temperature: ' +
                             str(headers['Temperature']) + ' Deg.' + ';')
            info_tiff.append('CCD Type: ' + str(headers['Imager ID']) + ';')
            info_tiff.append('Exposure: ' + str(headers['Exposure']) + '0 ms' +
                             ';')
            info_tiff.append('Filter Label: ' + str(headers['Filter Label']) +
                             ';')
            info_tiff.append('Filter Wavelength: ' +
                             str(headers['Filter Wavelength']) + ';')
            info_tiff.append('Image Type: ' + 'TIF' + ';')
            info_tiff.append('Latitude: ' + str(headers['Latitude']) +
                             ' Deg.' + ';')
            info_tiff.append('Longitude: ' + str(headers['Longitude']) +
                             ' Deg.' + ';')
            info_tiff.append('Moon Elevation: ' +
                             str(headers['Moon Elevation']) + ' Deg.' + ';')
            info_tiff.append('Moon Phase: ' + str(headers['Moon Phase']) +
                             " %" + ';')
            info_tiff.append('Site ID: ' + str(headers['Observatory']) + ';')
            info_tiff.append('Start Time: ' + str(day_hour) + " UTC;")
            info_tiff.append('Sun Elevation:' + str(headers['Sun Elevation']) +
                             ' Deg.;')
            info_tiff.append('Version: ' + str(software_version) + '')
        except Exception as e:
            print("info.add_text: " + e)

        try:
            if sys.platform.startswith("linux"):
                imgarray = numpy.array(img_tif, dtype='uint16')
                im3 = Image.fromarray(imgarray)
                im3.save(newname_tif)
            elif sys.platform.startswith("win"):
                # imgarray = numpy.array(img_tif, dtype=numpy.int16)
                imgarray_tiff2 = numpy.asarray(img_tif, dtype=numpy.uint32)

                im3 = Image.fromarray(imgarray_tiff2)
                tiff = im3.resize((int(512), int(512)))
                imgarray_tiff2 = numpy.asarray(tiff, dtype=numpy.uint32)

                tiff2 = TIFFimage(imgarray_tiff2, description=info_tiff)
                tiff2.write_file(newname_tif, compression='lzw')
                print(info_tiff)
        except Exception as e:
            print(e)

    except Exception as e:
        print("Exception -> {}".format(e))
Exemple #33
0
                        type=str)
    parser.add_argument("-i",
                        "--delay",
                        help="Interframe Delay",
                        required=True,
                        type=float)
    args = parser.parse_args()

    is_verbose = args.verbose
    kernelbase = args.kernelbase
    allow_predicted = args.predicted
    source = args.data
    start_date = args.start
    interframe_delay = args.delay

    if is_verbose:
        print_r("Loading spice kernels...")
    jcspice.load_kernels(kernelbase, allow_predicted)

    print_r("Loading Image Data...")
    data = open_image(source)

    delambert(data, start_date, interframe_delay, is_verbose)

    data /= data.max()
    data *= 65535.0
    data_matrix = data.astype(np.uint16)

    tiff = TIFFimage(data_matrix, description='')
    tiff.write_file("test_data.tif", compression='none')
Exemple #34
0
def train_EM_Seg():
    # 数据准备
    train_data_path = os.path.join("EMseg", 'train-volume.tif')
    train_label_path = os.path.join("EMseg", 'train-labels.tif')
    test_data_path = os.path.join("EMseg", 'test-volume.tif')

    train_data = openTIF(train_data_path)
    train_label = openTIF(train_label_path)
    test_data = openTIF(test_data_path)
    train_label[train_label > 0.5] = 1.0
    train_label[train_label < 0.5] = 0.0

    validation_data = train_data[:3, :, :, :]
    validation_label = train_label[:3, :, :, :]
    train_data = train_data[3:, :, :, :]
    train_label = train_label[3:, :, :, :]
    train_data, train_label = data_augmentation(train_data, train_label)
    model = Unet(1, image_shape)
    step_counter = 0
    data_order = np.arange(train_data.shape[0])
    step_in_epoch = (train_data.shape[0] // batch_size) + 1
    current_time = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
    train_log_dir = 'logs/UNet_EM_SEG_' + current_time
    model_path = os.path.join(train_log_dir, 'best_model.h5')
    summary_writer = tf.summary.create_file_writer(train_log_dir)
    best_iou = -1
    print('train dataset shape:', train_data.shape)
    print('train label shape:', train_label.shape)
    # 训练环节
    for ep in range(epoch_num):
        # shuffle
        np.random.shuffle(data_order)
        train_data = train_data[data_order, :, :, :]
        train_label = train_label[data_order, :, :, :]
        for step in range(step_in_epoch):
            step_counter += 1
            batch_start = step * batch_size
            batch_end = batch_start + batch_size
            batch_data = train_data[batch_start:batch_end, :, :, :]
            batch_label = train_label[batch_start:batch_end, :, :, :]
            with tf.GradientTape() as tape:
                predictions = model(batch_data, training=True)
                loss = loss_function(batch_label, predictions)
            gradients = tape.gradient(loss, model.trainable_variables)
            optimizer.apply_gradients(zip(gradients,
                                          model.trainable_variables))
            train_loss(loss)
            with summary_writer.as_default():
                tf.summary.scalar('Stats/train_loss',
                                  train_loss.result(),
                                  step=step_counter)
            train_loss.reset_states()

        predictions = model.predict(validation_data)
        v_loss = loss_function(validation_label, predictions)
        validation_loss(v_loss)
        predictions[predictions >= 0.5] = 1.0
        predictions[predictions < 0.5] = 0.0
        validation_iou.update_state(validation_label, predictions)

        with summary_writer.as_default():
            tf.summary.scalar('stats/val_loss',
                              validation_loss.result(),
                              step=ep)
            tf.summary.scalar('stats/val_iou',
                              validation_iou.result(),
                              step=ep)
        if validation_iou.result() > best_iou:
            model.save_weights(model_path)
            best_iou = validation_iou.result()
        validation_loss.reset_states()
        validation_iou.reset_states()
    print("best validation iou:", best_iou)
    # 评价环节

    # 打印validation set里面的图片
    model.load_weights(model_path)
    valid_pred = model.predict(validation_data)
    for im_idx in range(valid_pred.shape[0]):
        valid_im = valid_pred[im_idx, :, :, :]
        valid_im[valid_im >= 0.5] = 255
        valid_im[valid_im < 0.5] = 0
        imsave(os.path.join(train_log_dir, 'validation_%d.png' % im_idx),
               valid_im)
        imsave(os.path.join(train_log_dir, 'validation_label_%d.png' % im_idx),
               validation_label[im_idx])
    # 生成测试集的输出
    test_pred = model.predict(test_data)
    test_tiff = []
    for im_idx in range(test_data.shape[0]):
        test_im = test_pred[im_idx, :, :, :]
        test_im[test_im >= 0.5] = 255
        test_im[test_im < 0.5] = 0
        test_tiff.append(np.squeeze(test_im))
    tiff = TIFFimage(test_tiff)
    tiff.write_file(os.path.join(train_log_dir, 'test_result.tif'))
Exemple #35
0
    bitpix = hdu.header['bitpix']
    bzero = hdu.header['bzero']

    #is the image flipped? No worries!
    d = hdu.data[::-1, :]

    #removing mask (if I understood this bit of the spec correctly)
    d[d < bzero] = bzero

    mi = numpy.min(d)
    ma = numpy.max(d)
    print d.shape, d.dtype, mi, ma

    #mostly, we want 16 bit images...
    if bitpix != 16:
        d = 65535 * (d - mi) / (ma - mi)

    print "writing 16 bit tiff..."
    d16 = d.astype(numpy.uint16)
    tiff = TIFFimage(d16,
                     description='http://tdc-www.harvard.edu/postage_stamp/')
    tiff.write_file(fn + '.tif', compression='lzw')
    #flush the file to disk:
    del tiff

    print "writing equalized 8 bit png and jpeg..."
    d8, cdf = histeq(d, 65536)
    i = Image.fromarray(d8)
    i.save(fn + '.png')
    i.save(fn + '.jpg')
        I = I | (L2 / A > ip_threshold).as_matrix()

        # Find the proper index in the labeled image
        idx = properties.index[I] - 10000 * position
        # Filter bad idx from labeled image
        notrightsize = np.isnan(nuclei)
        for i in idx:
            notrightsize = notrightsize | (nuclei == i)

        # Save only good nuclei in image and properties table
        good_labels = nuclei.copy()
        good_labels[notrightsize] = 0
        nucs = properties[~I]

        # Save the segmentation as 32-bit TIFF
        # NB: only use the libtiff package since we need 32-bit
        tiff = TIFFimage(good_labels, description='')
        tiff.write_file(''.join((base, 'nuclei.tif')), compression='none')

        #    DEBUG ONLY: also save the raw segmentation
        #    tiff = TIFFimage(nuclei, description='')
        #    tiff.write_file(''.join((base, 'raw_nuclei.tif')),
        #                    compression='none')

        # Save the DataFrame as CSV
        nucs.to_csv(''.join((base, 'quantification.csv')), sep='\t')

    all_props = pd.concat([all_props, nucs])
    raw_props = pd.concat([raw_props, properties])
    print "Done with ", position
Exemple #37
0
def save_image(image_data, path):
    data_matrix = image_data.astype(np.uint16)
    tiff = TIFFimage(data_matrix, description='')
    tiff.write_file(path, compression='none')
Exemple #38
0
# +/+ path
p1 = "/Volumes/MoritzBertholdHD/CellData/Experiments/Ex3/LabeledImages/full_ex3_no_zeros_66_shifted/++/"
# +/- path
p2 = "/Volumes/MoritzBertholdHD/CellData/Experiments/Ex3/LabeledImages/full_ex3_no_zeros_66_shifted/+-/"
# -/+ path
p3 = "/Volumes/MoritzBertholdHD/CellData/Experiments/Ex3/LabeledImages/full_ex3_no_zeros_66_shifted/-+/"
# -/- path
p4 = "/Volumes/MoritzBertholdHD/CellData/Experiments/Ex3/LabeledImages/full_ex3_no_zeros_66_shifted/--/"
# outliers
# p5 = "/Volumes/MoritzBertholdHD/CellData/Experiments/Ex3/LabeledImages/full_ex3/outliers/"

y = labels
# for i in range(y.shape[0]):
for i in range(y.shape[0]):
    # to create a tiff structure from image data
    tiff_ch1 = TIFFimage(X[i, 0, :, :].astype('uint16'), description='')
    tiff_ch2 = TIFFimage(X[i, 1, :, :].astype('uint16'), description='')
    tiff_ch3 = TIFFimage(X[i, 2, :, :].astype('uint16'), description='')
    tiff_ch4 = TIFFimage(X[i, 3, :, :].astype('uint16'), description='')

    if i % 100 == 0:
        print "Cell no.", i

    if y[i, 0] == 0:
        tiff_ch1.write_file(p1 + 'id' + str(i) + '_class' + str(y[i, 0]) +
                            '_' + str(y[i, 1]) + '_ch1.tif',
                            compression='none')
        tiff_ch2.write_file(p1 + 'id' + str(i) + '_class' + str(y[i, 0]) +
                            '_' + str(y[i, 1]) + '_ch2.tif',
                            compression='none')
        tiff_ch3.write_file(p1 + 'id' + str(i) + '_class' + str(y[i, 0]) +