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
Example #2
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()
Example #3
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()
Example #4
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()
Example #5
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()

            #os.remove(fn)
            atexit.register(os.remove, fn)
Example #6
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)
Example #7
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
Example #8
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()
Example #9
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))
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)
Example #11
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)
Example #12
0
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
Example #13
0
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
Example #14
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
Example #15
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'],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()
Example #16
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()
Example #17
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
Example #18
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_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)
Example #20
0
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)
Example #21
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
Example #22
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)
Example #23
0
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]) +
                            '_' + str(y[i, 1]) + '_ch3.tif',
                            compression='none')
        tiff_ch4.write_file(p1 + 'id' + str(i) + '_class' + str(y[i, 0]) +
                            '_' + str(y[i, 1]) + '_ch4.tif',
                            compression='none')
    if y[i, 0] == 1:
        tiff_ch1.write_file(p2 + 'id' + str(i) + '_class' + str(y[i, 0]) +
                            '_' + str(y[i, 2]) + '_ch1.tif',
                            compression='none')
        tiff_ch2.write_file(p2 + 'id' + str(i) + '_class' + str(y[i, 0]) +
Example #24
0
                    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')
Example #25
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')
Example #26
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)
Example #27
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'))
Example #28
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))
Example #29
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
        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
Example #31
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')
Example #32
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')
    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.')