示例#1
0
def find_center_blurred(img, msk, x0=0, y0=0, threshold=None, blur_radius=4., dmax=5):
    """
    Find the center using blurred version of 'pixelwise' method.

    usage: 
    ======
    x,y = find_center_blurred(img, msk, x0=0, y0=0, threshold=None, blur_radius=4, dmax=5)
    """
    if threshold is None: threshold = img.min()
    I = spimage.sp_image_alloc(img.shape[1],img.shape[0],1)
    I.image[:] = img * (img >= threshold)
    I.mask[:] = msk[:]
    I.detector.image_center[:] = np.array([center_to_pos(x0,img.shape[1]),
                                           center_to_pos(y0,img.shape[0]), 0 ])
    kernel = spimage.sp_gaussian_kernel(float(blur_radius),int(blur_radius*8+1),int(blur_radius*8+1),1)
    c = spimage.sp_image_convolute_with_mask(I,kernel,np.array([1,1,1]).astype(np.int32))
    ds = spimage.sp_image_alloc(img.shape[1]/4,img.shape[0]/4,1)
    ds.image[:] = c.image[:-3:4,:-3:4]
    ds.mask[:] = c.mask[:-3:4,:-3:4]
    ds.detector.image_center[:] = c.detector.image_center[:] / 4.0
    spimage.sp_find_center_refine_minimal_mask(ds, int(1+dmax/4), 0)
    c.detector.image_center[:] = ds.detector.image_center[:] * 4.0
    spimage.sp_image_free(ds)
    score = spimage.sp_find_center_refine_minimal_mask(c, 4, 0)
    x = pos_to_center(c.detector.image_center[0],img.shape[1])
    y = pos_to_center(c.detector.image_center[1],img.shape[0])
    spimage.sp_image_free(I)
    spimage.sp_image_free(kernel)
    spimage.sp_image_free(c)
    return (x,y,score)
示例#2
0
def allocate_image(shape):
    """Allocate spimage image object from numpy type shape"""
    if len(shape) == 2:
        img = _spimage.sp_image_alloc(shape[0], shape[1], 1)
    elif len(shape) == 3:
        img = _spimage.sp_image_alloc(shape[0], shape[1], shape[2])
    else:
        raise ValueError("Array must be 2 or 3 dimensional")
    return img
示例#3
0
def allocate_image(shape):
    """Allocate spimage image object from numpy type shape"""
    if len(shape) == 2:
        img = _spimage.sp_image_alloc(shape[0], shape[1], 1)
    elif len(shape) == 3:
        img = _spimage.sp_image_alloc(shape[0], shape[1], shape[2])
    else:
        raise ValueError("Array must be 2 or 3 dimensional")
    return img
示例#4
0
def numpy_array_to_image(img, msk=None):
    s = img.shape
    d = len(list(s))
    if d == 3:
        sp_img = spimage.sp_image_alloc(s[2], s[1], s[0])
    else:
        sp_img = spimage.sp_image_alloc(s[1], s[0], 1)
    sp_img.image[:] = img[:]
    if msk is not None:
        sp_img.mask[:] = msk[:]
    return sp_img
示例#5
0
def numpy_array_to_image(img,msk=None):
    s = img.shape
    d = len(list(s))
    if d == 3:
        sp_img = spimage.sp_image_alloc(s[2],s[1],s[0])
    else:
        sp_img = spimage.sp_image_alloc(s[1],s[0],1)
    sp_img.image[:] = img[:]
    if msk is not None:
        sp_img.mask[:] = msk[:]
    return sp_img
 def _init_initial_support(self):
     if not self._initial_support_dirty:
         self._log("Initial support already initialised.","DEBUG")
         return
     for img in [self._sp_initial_support,self._sp_initial_support_sh]:
         if img is not None:
             spimage.sp_image_free(img)
     self._sp_initial_support = spimage.sp_image_alloc(self._Ny,self._Nx,1)
     if "radius" in self._initial_support_config or "area" in self._initial_support_config:
         X,Y = np.meshgrid(np.arange(self._Nx),np.arange(self._Ny))
         X = X-(self._Nx-1)/2.
         Y = Y-(self._Ny-1)/2.
         R = np.sqrt(X**2 + Y**2)
         self._phaser_dirty = True
         if "radius" in self._initial_support_config:
             r = self._initial_support_config["radius"]
         else:
             r = np.sqrt( self._initial_support_config["area"] * self._Nx * self._Ny / np.pi )
         self._sp_initial_support.image[:] = np.float32(np.fft.fftshift(R) < r)
     else:
         self._phaser_dirty = True
         S = self._initial_support_config["support_mask"]
         self.sep_initial_support.image[:] = np.float32(S)
     self._initial_support_dirty = False
     self._log("Initial support initialised.","DEBUG")
示例#7
0
def split_pnccd(filename):
    f = h5py.File(filename)

    i1 = f.keys().index('data')
    i2 = f.values()[i1].keys().index('data1')
    i2 = f.values()[i1].keys().index('data1')

    data2 = f.values()[i1].values()[i2].value

    data2_1 = spimage.sp_image_alloc(data2.shape[1]/2,data2.shape[0],1)
    data2_2 = spimage.sp_image_alloc(data2.shape[1]/2,data2.shape[0],1)
    data2_1.image[:,:] = data2[:,:(data2.shape[1]/2)]
    data2_2.image[:,:] = data2[:,(data2.shape[1]/2):]

    spimage.sp_image_write(data2_1,filename[:-3]+"_part1.h5",0)
    spimage.sp_image_write(data2_2,filename[:-3]+"_part2.h5",0)
示例#8
0
def slice_3D(fn, output_dir=''):
    #Takes 3 slices through the center along x,y and z from a 3D image and saves it as three new 2D images.
    img_3D = spimage.sp_image_read(fn, 0)
    dim = shape(img_3D.image)[0]
    img_2D = spimage.sp_image_alloc(dim, dim, 1)
    img_2D.shifted = img_3D.shifted
    img_2D.scaled = img_3D.scaled
    if img_3D.shifted == 0:
        s = dim / 2
    else:
        s = 0

    img_2D.image[:, :] = img_3D.image[s, :, :]
    img_2D.mask[:, :] = img_3D.mask[s, :, :]
    spimage.sp_image_write(img_2D,
                           output_dir + fn.split('.')[0] + '_x_slice.h5', 0)

    img_2D.image[:, :] = img_3D.image[:, s, :]
    img_2D.mask[:, :] = img_3D.mask[:, s, :]
    spimage.sp_image_write(img_2D,
                           output_dir + fn.split('.')[0] + '_y_slice.h5', 0)

    img_2D.image[:, :] = img_3D.image[:, :, s]
    img_2D.mask[:, :] = img_3D.mask[:, :, s]
    spimage.sp_image_write(img_2D,
                           output_dir + fn.split('.')[0] + '_z_slice.h5', 0)
示例#9
0
def intensities_array_to_h5(img0,msk0,cx=None,cy=None,cropLength=None,save_to_file=None):
    import spimage,imgtools
    Nx = img0.shape[1]
    Ny = img0.shape[0]
    img = img0
    msk = msk0
    if cropLength != None:
        img = imgtools.crop(img0,cropLength,center=[cy,cx])
        msk = imgtools.crop(msk0,cropLength,center=[cy,cx])
        Nx = cropLength
        Ny = cropLength
    elif cx != None and cy != None:
        img = imgtools.recenter(img0,cx,cy)
        msk = imgtools.recenter(msk0,cx,cy)
    I = spimage.sp_image_alloc(Nx,Ny,1)
    I.image[:,:] = img[:,:]
    I.mask[:,:] = msk[:,:]
    I2 = spimage.sp_image_shift(I)
    I2.shifted = 0
    I2.phased = 0
    spimage.sp_image_free(I)
    if save_to_file != None:
        spimage.sp_image_write(I2,save_to_file,0)
    else:
        return I2
示例#10
0
def crop_image(in_file, out_file, side, center=None):
    """Function to crop an h5 image and pad with zeros around it"""

    img = spimage.sp_image_read(in_file, 0)

    if not center:
        center = img.detector.image_center[:2]

    shifted = 0
    if img.shifted:
        shifted = 1
        img = spimage.sp_image_shift(img)

    cropped = spimage.sp_image_alloc(side, side, 1)
    cropped.image[:, :] = image_manipulation.crop_and_pad(
        img.image, center, side)
    cropped.mask[:, :] = image_manipulation.crop_and_pad(
        img.mask, center, side)

    if shifted:
        cropped = spimage.sp_image_shift(cropped)

    spimage.sp_image_write(cropped, out_file, 16)

    spimage.sp_image_free(img)
    spimage.sp_image_free(cropped)

    print("end")
示例#11
0
def image_from_array(image, mask=None):
    """Create spimage image type from numpy array with optional mask"""
    if len(image.shape) == 2:
        img = _spimage.sp_image_alloc(image.shape[0], image.shape[1], 1)
    elif len(image.shape) == 3:
        img = _spimage.sp_image_alloc(image.shape[0], image.shape[1], image.shape[2])
    else:
        raise ValueError("Array must be 2 or 3 dimensional")
    img.image[:] = image
    if mask is not None:
        img.mask[:] = _numpy.int32(mask)
    else:
        img.mask[:] = 1
    img.shifted = 0
    img.phased = int(bool(_numpy.iscomplex(image).sum() > 0))
    return img
示例#12
0
def image_from_array(image, mask=None):
    """Create spimage image type from numpy array with optional mask"""
    if len(image.shape) == 2:
        img = _spimage.sp_image_alloc(image.shape[0], image.shape[1], 1)
    elif len(image.shape) == 3:
        img = _spimage.sp_image_alloc(image.shape[0], image.shape[1],
                                      image.shape[2])
    else:
        raise ValueError("Array must be 2 or 3 dimensional")
    img.image[:] = image
    if mask is not None:
        img.mask[:] = _numpy.int32(mask)
    else:
        img.mask[:] = 1
    img.shifted = 0
    img.phased = int(bool(_numpy.iscomplex(image).sum() > 0))
    return img
示例#13
0
 def _init_amplitudes(self):
     if not self._amplitudes_dirty:
         self._log("Amplitudes already initialised.","DEBUG")
         return
     A = self._amplitudes
     if len(A.shape) == 2:
         self._Nx = A.shape[1]
         self._Ny = A.shape[0]
         if self._mask is not None:
             M = self._mask.copy()
         else:
             M = np.ones(shape=A.shape,dtype="bool")
         for img in [self._sp_amplitudes]:
             if img is not None:
                 spimage.sp_image_free(img)
         self._sp_amplitudes = spimage.sp_image_alloc(A.shape[0],A.shape[1],1)
         self._sp_amplitudes.image[:,:] = np.float32(A[:,:])
         self._sp_amplitudes.mask[:,:] = np.int32(M[:,:])
         self._sp_amplitudes.scaled = 1
         self._sp_amplitudes.phased = 0
         self._amplitudes_dirty = False
         self._log("Amplitudes initialised.","DEBUG")
     if len(A.shape) == 3:
         self._Nx = A.shape[2]
         self._Ny = A.shape[1]
         self._Nz = A.shape[0]
         if self._mask is not None:
             M = self._mask.copy()
         else:
             M = np.ones(shape=A.shape,dtype="bool")
         for img in [self._sp_amplitudes]:
             if img is not None:
                 spimage.sp_image_free(img)
         self._sp_amplitudes = spimage.sp_image_alloc(A.shape[0],A.shape[1],A.shape[2])
         self._sp_amplitudes.image[:,:,:] = np.float32(A[:,:,:])
         self._sp_amplitudes.mask[:,:,:] = np.int32(M[:,:,:])
         self._sp_amplitudes.scaled = 1
         self._sp_amplitudes.phased = 0
         self._amplitudes_dirty = False
         self._log("Amplitudes initialised.","DEBUG")
示例#14
0
def pnccd_to_image(infile, outfile):
    try:
        f = h5py.File(infile)
    except:
        raise IOError("Can't read %s. It may not be a pnCCD file." % filename)

    i1 = f.keys().index("data")
    i2 = f.values()[i1].keys().index("data1")

    data = f.values()[i1].values()[i2].value

    img = spimage.sp_image_alloc(pylab.shape(data)[0], pylab.shape(data)[1], 1)
    img.image[:, :] = data[:, :]
    spimage.sp_image_write(img, outfile, 0)
    spimage.sp_image_free(img)
示例#15
0
def mask_center(img_file_name, radius, output_file_name=None, save_file=True):
    """Create a new mask around the center with given radius"""
    img = spimage.sp_image_read(img_file_name, 0)
    r_array = stuff.r_array_3D(img.image.shape[0])
    img.mask[r_array < radius] = 0
    img.mask[r_array > radius] = 1
    new_mask = spimage.sp_image_alloc(*np.shape(img.mask))
    new_mask.mask[:, :, :] = img.mask
    new_mask.image[:, :, :] = img.image
    new_mask.shifted = img.shifted
    new_mask.scaled = img.scaled
    new_mask.detector = img.detector
    if save_file:
        spimage.sp_image_write(new_mask, output_file_name, 0)
    else:
        return (new_mask)
示例#16
0
def downsample_by_average(image, file_type, sf):
    """image can be either spimage object or numpy array, file_type = spimage_object or numpy_array, sf is the downsampling factor"""
    if file_type == 'spimage_object':
        model_side = shape(image.image)[0]
    if file_type == 'numpy_array':
        model_side = shape(image)[0]
    downsampled = spimage.sp_image_alloc(model_side / sf, model_side / sf, 1)
    for i, x in enumerate(range(0, model_side, sf)):
        for j, y in enumerate(range(0, model_side, sf)):
            if file_type == 'spimage_object':
                downsampled.image[i, j] = average(image.image[x:x + sf,
                                                              y:y + sf])
                downsampled.mask[i, j] = int((image.mask[x:x + sf,
                                                         y:y + sf]).all())
            if file_type == 'numpy_array':
                downsampled.image[i, j] = average(image[x:x + sf, y:y + sf])
    return downsampled
示例#17
0
def add_mask(path, number_of_files, output_file):
    """Reads in files in paths, outputs h5 file output_file with accumulative mask from input images saved in both image and mask"""
    imgs = [
        os.path.join(path, i) for i in os.listdir(path) if i.endswith('.h5')
    ]
    for img_file in imgs[:number_of_files]:
        img = spimage.sp_image_read(img_file, 0)
        try:
            msk_array = msk_array + img.mask
        except NameError:
            msk_array = img.mask
    msk_array[msk_array < msk_array.max()] = 0
    msk_array[msk_array != 0] = 1
    new = spimage.sp_image_alloc(
        numpy.shape(img.image)[0],
        numpy.shape(img.image)[1], 1)
    new.mask[:, :] = msk_array
    new.image[:, :] = msk_array
    spimage.sp_image_write(new, output_file, 0)
示例#18
0
def find_center_pixelwise_fast(img, msk, x0=0, y0=0, dmax=5, rmax=None):
    """
    Find center of diffraction pattern using a pixelwise comparison of centry-symmetric pixels.

    This is a faster C implementation.

    usage:
    ======
    x,y = find_center_pixelwise_fast(img, msk, x0, y0, dmax=5, rmax=None)
    """
    if rmax is not None: msk &= (spimage.rgrid(msk.shape, (x0,y0)) < rmax)
    I = spimage.sp_image_alloc(int(np.ceil(img.shape[1])), int(np.ceil(img.shape[0])), 1)
    I.image[:] = img
    I.mask[:]  = msk
    I.detector.image_center[:] = np.array([center_to_pos(x0,img.shape[1]), center_to_pos(y0,img.shape[0]), 0 ])
    success = spimage.sp_find_center_refine(I, dmax, 0, None)
    x = pos_to_center(I.detector.image_center[0],img.shape[1])
    y = pos_to_center(I.detector.image_center[1],img.shape[0])
    spimage.sp_image_free(I)
    return (x,y,success)
示例#19
0
def calc_average_img(r2, r1=0, r_skip=None, iteration=None):
    rundir_range = range(r1, r2)
    if r_skip != None:
        for i in r_skip:
            rundir_range.remove(i)
    fail = 0
    for r in rundir_range:
        run_dir = 'run_%04d' % r
        print run_dir
        if iteration == None:
            iteration = return_last_iteration_integer(
                '{r}/output_mnt/'.format(r=run_dir))
        with h5py.File('{r}/output_mnt/fmodel_{i}.h5'.format(
                r=run_dir, i=iteration)) as f:
            try:
                added_real += f['real'][...]
                added_imag += f['imag'][...]
            except NameError:
                added_real = f['real'][...]
                added_imag = f['imag'][...]
            except KeyError:
                print 'fail'
                fail += 1
            if r == r2 - 1:
                mask = f['mask'][:]
    added_real /= float(len(rundir_range) - fail)
    added_imag /= float(len(rundir_range) - fail)
    complex_fmodel = added_real + 1j * added_imag
    new = spimage.sp_image_alloc(*np.shape(added_real))
    new.phased = 1
    new.mask[:] = mask
    new.image[:] = complex_fmodel
    spimage.sp_image_write(
        new, 'avg_fmodel_runs_{i}_{j}_iteration{k}.h5'.format(i=r1,
                                                              j=r2,
                                                              k=iteration), 0)
    new.image[:, :, :] = fft.fftn(complex_fmodel)
    spimage.sp_image_write(
        new, 'avg_model_runs_{i}_{j}_iteration{k}.h5'.format(i=r1,
                                                             j=r2,
                                                             k=iteration), 0)
示例#20
0
def calc_average_img_translated(r2,
                                r1=0,
                                r_skip=None,
                                iteration=None,
                                reference_model=None):
    rundir_range = range(r1, r2)
    if r_skip != None:
        for i in r_skip:
            rundir_range.remove(i)
    for r in rundir_range:
        run_dir = 'run_%04d' % r
        print run_dir
        if iteration == None:
            iteration = return_last_iteration_integer(
                '{r}/output_mnt/'.format(r=run_dir))
        f = spimage.sp_image_read(
            '{r}/output_mnt/fmodel_{i}.h5'.format(r=run_dir, i=iteration), 0)
        if r == r1:
            if reference_model == None:
                ref = f
            else:
                ref = reference_model
            added_img = f.image[:]
            mask = f.mask[:]
        if r != r1:
            spimage.sp_image_superimpose(ref, f, 0)
        added_img += f.image[:]
    added_img /= float(len(rundir_range))
    new = spimage.sp_image_alloc(*np.shape(added_img))
    new.phased = 1
    new.mask[:] = mask
    new.image[:] = added_img
    spimage.sp_image_write(
        new, 'avg_fmodel_runs_{i}_{j}_iteration{k}.h5'.format(i=r1,
                                                              j=r2,
                                                              k=iteration), 0)
    new.image[:, :, :] = fft.fftn(added_img)
    spimage.sp_image_write(
        new, 'avg_model_runs_{i}_{j}_iteration{k}.h5'.format(i=r1,
                                                             j=r2,
                                                             k=iteration), 0)
示例#21
0
def calc_average_img(path, cutoff=None):
    if cutoff == None:
        run_folders = []
    run_folders = select_by_error_cutoff(path, cutoff)['run']
    for r in run_folders:
        img = spimage.sp_image_read(
            r +
            '/output_mnt/model_{n}.h5'.format(n=return_last_iteration_integer(
                os.path.join(r, 'output_mnt'))), 0)
        try:
            added_imgs = added_imgs + img.image
        except NameError:
            added_imgs = img.image
    avg_img = added_imgs / float(len(run_folders))
    new = spimage.sp_image_alloc(*np.shape(img.image))
    new.image[:, :, :] = avg_img
    spimage.sp_image_write(
        new,
        '{p}/average_final_model_{c}_{t}.h5'.format(p=path,
                                                    c=cutoff,
                                                    t=time.strftime('%Y%m%d')),
        0)
示例#22
0
def crop_image(in_file, out_file, side, center=None):
    """Function to crop an h5 image and pad with zeros around it"""

    img = spimage.sp_image_read(in_file, 0)

    if not center:
        center = img.detector.image_center[:2]

    shifted = 0
    if img.shifted:
        shifted = 1
        img = spimage.sp_image_shift(img)

    print "shifted = ", shifted

    # cropped = spimage.rectangle_crop(img,lowX,lowY,highX,highY)
    cropped = spimage.sp_image_alloc(side, side, 1)
    cropped.image[:, :] = image_manipulation.crop_and_pad(img.image, center, side)
    cropped.mask[:, :] = image_manipulation.crop_and_pad(img.mask, center, side)

    print "did crop"

    if shifted:
        cropped = spimage.sp_image_shift(cropped)

    print "shifted (or not)"

    print "write ", out_file

    # print "orientation = ", cropped.detector.orientation
    # print spimage.sp_3matrix_get(cropped.detector.orientation,0,0,0)

    spimage.sp_image_write(cropped, out_file, 16)

    spimage.sp_image_free(img)
    spimage.sp_image_free(cropped)

    print "end"