rg314 / pytraction

Bayesian Traction Force Microscopy
BSD 3-Clause "New" or "Revised" License
3 stars 1 forks source link

Edge artefact after align_slice() #44

Closed rg314 closed 3 years ago

rg314 commented 3 years ago

Samples that undergo large drift cause the PIV measurements to artifacts on the edges. After the drift correction using align_slice() the drift is corrected but the PIV thinks this is a displacement.

rg314 commented 3 years ago

@l-kaplan I've just added the base code to fix this bug. I will implement it in the core package tomorrow. Did you get a chance to look at your data? Let me know if you want to jump on another call.

def remove_boarder_from_aligned(aligned_img):
    _,thresh = cv2.threshold(aligned_img,0,255,cv2.THRESH_BINARY)
    kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(9,9))
    res = cv2.morphologyEx(thresh,cv2.MORPH_CLOSE,kernel)
    contours,hierarchy = cv2.findContours(thresh,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
    cnt = sorted(contours, key=lambda x: cv2.contourArea(x))[0]
    x, y, w, h = cv2.boundingRect(cnt)
    return aligned_img[y:y+h, x:x+h]
rg314 commented 3 years ago

Hi @l-kaplan

I have fixed the bug with the edge artefacts. It would be good if you could test it. There's still something funny going on with one of the images I'll look into this. I'm going merge into master soon

image
l-kaplan commented 3 years ago

Wow, that looks fantastic! I will test it right away. The first image might indeed be correct, since I see a few spots of moving beads rather than a homogeneous displacement around the cell in the aligned stack. Whatever that means... Is there a reason why you inverted the color scale?

l-kaplan commented 3 years ago

Do I need to make any changes in the config.yaml? I am getting following error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
/tmp/ipykernel_282/1756007441.py in <module>
      4 config = '../config/config.yaml'
      5 
----> 6 traction_config = TractionForceConfig(E=E, scaling_factor=pix_per_mu, config=config)
      7 
      8 

~/miniconda3/envs/pytraction/lib/python3.8/site-packages/pytraction/core.py in __init__(self, scaling_factor, E, min_window_size, dt, s, meshsize, device, segment, config)
     35             self.config = self._get_config(min_window_size, dt, E, s, meshsize, scaling_factor, segment)
     36         else:
---> 37             self.config = self._config_ymal(config, min_window_size, dt, E, s, meshsize, scaling_factor)
     38 
     39 

~/miniconda3/envs/pytraction/lib/python3.8/site-packages/pytraction/core.py in _config_ymal(config, min_window_size, dt, E, s, meshsize, scaling_factor)
     71     @staticmethod
     72     def _config_ymal(config, min_window_size, dt, E, s, meshsize, scaling_factor):
---> 73         config['tfm']['E'] = E,
     74         config['tfm']['pix_per_mu'] = scaling_factor
     75         config['tfm']['meshsize'] = meshsize

TypeError: string indices must be integers
rg314 commented 3 years ago

Please can you send me a snippet of the code that you're trying to run

l-kaplan commented 3 years ago
pix_per_mu = 6.16 # The number of pixels per micron 
G = 100 #enter shear modulus
E = 2*G*(1+0.48); #calculate Young modulus
config = '../config/config.yaml'

traction_config = TractionForceConfig(E=E, scaling_factor=pix_per_mu, config=config)

gt = "_wt"

def tf(i):
    j = str(i)
    img_path_cell = 'images'+gt+ '/pos_'+ j + '_wc_ch00_cr.tif'
    img_path_bead = 'images'+gt+ '/pos_'+ j + '_wc_ch01_cr.tif'
    ref_path = 'images'+gt+ '/pos_'+ j + '_woc_ch01_cr.tif'
    roi_path = 'images'+gt+ '/pos_'+ j + '.roi'

    cell = io.imread(img_path_cell)
    bead = io.imread(img_path_bead)
    ref = io.imread(ref_path)

    img = np.stack([[bead, cell]])
    ref = np.stack([ref, ref])
    io.imsave('stacked_images'+gt+ '/img_pos_'+ j + '.tif', img)
    io.imsave('stacked_images'+gt+ '/ref_pos_'+ j + '.tif', ref)

    img_path = 'stacked_images'+gt+ '/img_pos_'+ j + '.tif'
    ref_path = 'stacked_images'+gt+ '/ref_pos_'+ j + '.tif'

    img, ref, roi = traction_config.load_data(img_path, ref_path, roi_path) # we can load the dat using the load_data method

    log1 = process_stack(img, ref, config = traction_config, roi = roi, verbose=0)
    log1.save("out"+gt+ "/pos_"+ j + "_.h5")

    plot(log1, frame = 0, mask = True)[0].savefig("out"+gt+ "/pos_" + j + "_fig.pdf")

#    return(log1)

start_time = time.time()

#if __name__ == '__main__':
#    with Pool() as p:
#        p.map(tf,range(1,17))

log1 = tf(0)        
print("--- %s seconds ---" % (time.time() - start_time))
rg314 commented 3 years ago

It looks like the install of the latest version hasn't worked since the new code should read as followed:

    @staticmethod
    def _config_yaml(config, E, min_window_size, s, meshsize, scaling_factor):
        with open(config, 'r') as f:
            config = yaml.load(f, Loader=yaml.FullLoader)

        config['tfm']['E'] = E,
        config['tfm']['pix_per_mu'] = scaling_factor
        config['piv']['min_window_size'] = min_window_size if min_window_size is not None else config['piv']['min_window_size']
        config['tfm']['s'] = s if s is not None else config['tfm']['s']
        config['tfm']['meshsize'] = meshsize if meshsize is not None else config['tfm']['meshsize']
        return config

Try uninstalling and reinstalling. Make sure you pull the latest changes as well :)

i.e. something like this:

cd GitHub/pytraction
git pull
conda actviate [your pytraction env]
pip uninstall pytraction
pip install .