예제 #1
0
def local_df(img_folder, seg_length=50, winsize=50, step=25):
    """
    Compute local density fluctuations of given image sequence in img_folder
    
    Args:
    img_folder -- folder containing .tif image sequence
    seg_length -- number of frames of each segment of video, for evaluating standard deviations
    winsize --
    step --
    
    Returns:
    df -- dict containing 't' and 'local_df', 't' is a list of time (frame), 'std' is a list of 2d array 
          with local standard deviations corresponding to 't'
    """

    l = corrLib.readseq(img_folder)
    num_frames = len(l)
    assert (num_frames > seg_length)

    stdL = []
    tL = range(0, num_frames, seg_length)
    for n in tL:
        img_nums = range(n, min(n + seg_length, num_frames))
        l_sub = l.loc[img_nums]
        img_seq = []
        for num, i in l_sub.iterrows():
            img = io.imread(i.Dir)
            X, Y, I = corrLib.divide_windows(img, windowsize=[50, 50], step=25)
            img_seq.append(I)
        img_stack = np.stack([img_seq], axis=0)
        img_stack = np.squeeze(img_stack)
        std = np.std(img_stack, axis=0)
        stdL.append(std)

    return {'t': tL, 'std': stdL}
예제 #2
0
def convection(pivData, image, winsize, step=None, shape=None):
    """
    Compute convection term u.grad(c) based on piv data (x, y, u, v) and image.
    
    Args:
    pivData -- DataFrame of (x, y, u, v)
    image -- the image corresponding to pivData
    winsize -- coarse-graining scheme of image
    step -- (optional) distance (pixel) between adjacent PIV vectors
    shape -- (optional) shape of piv matrices
    
    Returns:
    udc -- convection term u.grad(c). unit: [u][c]/pixel, [u] is the unit of u, usually px/s, [c] is the unit of concentration 
           measured from image intensity, arbitrary.
    """
    x = pivData.sort_values(by=['x']).x.drop_duplicates()
    if step == None:
        # Need to infer the step size from pivData
        step = x.iat[1] - x.iat[0]

    if shape == None:
        # Need to infer shape from pivData
        y = pivData.y.drop_duplicates()
        shape = (len(y), len(x))

    # check coarse-grained image shape
    X, Y, I = corrLib.divide_windows(image,
                                     windowsize=[winsize, winsize],
                                     step=step)
    assert (I.shape == shape)

    X = np.array(pivData.x).reshape(shape)
    Y = np.array(pivData.y).reshape(shape)
    U = np.array(pivData.u).reshape(shape)
    V = np.array(pivData.v).reshape(shape)

    # compute gradient of concentration
    # NOTE: concentration is negatively correlated with intensity.
    # When computing gradient of concentration, the shifting direction should reverse.

    dcx = np.gradient(I, -step, axis=1)
    dcy = np.gradient(I, -step, axis=0)

    udc = U * dcx + V * dcy

    return udc
예제 #3
0
def PIV_masked(I0, I1, winsize, overlap, dt, mask):
    """Apply PIV analysis on masked images
    Args:
    I0, I1 -- adjacent images in a sequence
    winsize, overlap, dt -- PIV parameters
    mask -- a boolean array, False marks masked region and True marks the region of interest
    mask_procedure -- the option chosen to apply the mask, used for testing, remove in the future.
    Returns:
    frame_data -- x, y, u, v DataFrame, here x, y is wrt original image, (u, v) are in px/s

    This function is rewritten based on the PIV_droplet() function in piv_droplet.py script.
    The intended usage is just to pass one additional `mask` parameter, on top of conventional parameter set.

    EDIT
    ====
    Dec 14, 2021 -- Initial commit.
    Dec 15, 2021 -- After testing 2 masking procedure, option 1 is better.
                    Two procedures produce similar results, but option 1 is faster.
                    So this function temporarily uses option 1, until a better procedure comes.
    Jan 07, 2022 -- Change mask threshold from 1 to 0.5, this will include more velocities.

    MASKING PROCEDURE
    =================
    Option 1:
    i) Mask on raw image: I * mask, perform PIV
    ii) Divide mask into windows: mask_w
    iii) use mask_w to mask resulting velocity field: u[~mask_w] = np.nan
    ---
    Option 2:
    i) Perform PIV on raw images
    ii) Divide mask into windows:mask_w
    iii) use mask_w to mask resulting velocity field: u[~mask_w] = np.nan
    ---
    """
    assert (mask.shape == I0.shape)
    mask = mask >= mask.mean()  # convert mask to boolean array
    I0_mask = I0 * mask
    I1_mask = I1 * mask
    x, y, u, v = PIV(I0_mask, I1_mask, winsize, overlap, dt)
    mask_w = divide_windows(
        mask, windowsize=[winsize, winsize], step=winsize - overlap)[2] >= 0.5
    assert (mask_w.shape == x.shape)
    u[~mask_w] = np.nan
    v[~mask_w] = np.nan
    return x, y, u, v
예제 #4
0
def divcn(pivData, img, winsize, step=None, shape=None):
    """
    Compute the div(cn) term from PIV and image.
    
    Args:
    pivData -- (X, Y, U, V)
    img -- 2d matrix
    winsize -- window size of PIV, used to scale down image
    step -- step of PIV, used to scale down image
    
    Returns:
    div_cn -- 2d matrix
    """

    x = pivData.sort_values(by=['x']).x.drop_duplicates()
    if step == None:
        # Need to infer the step size from pivData
        step = x.iat[1] - x.iat[0]

    if shape == None:
        # Need to infer shape from pivData
        y = pivData.y.drop_duplicates()
        shape = (len(y), len(x))

    X, Y, I = corrLib.divide_windows(img,
                                     windowsize=[winsize, winsize],
                                     step=step)
    c = 186 - 0.19 * I

    assert (I.shape == shape)
    X = np.array(pivData.x).reshape(shape)
    Y = np.array(pivData.y).reshape(shape)
    U = np.array(pivData.u).reshape(shape)
    V = np.array(pivData.v).reshape(shape)

    cu = c * U
    cv = c * V

    dudx = np.gradient(cu, step, axis=1)
    dvdy = np.gradient(cv, step, axis=0)
    div_cn = dudx + dvdy

    return div_cn
예제 #5
0
def down_size_imseq(folder, windowsize=[50, 50], step=25):
    """
    Downsizing an image sequence of k images in given folder and save them as an numpy array of size k*m*n, 
    where m and n are the downsized dimension of each image. 
    
    Args:
    folder -- folder of image sequence
    windowsize -- parameter of corrLib.divide_windows(), pixel
    step -- parameter of corrLib.divide_windows(), pixel
    
    Returns:
    stack -- numpy array of size k*m*n
    """

    l = corrLib.readseq(folder)
    I_list = []
    for num, i in l.iterrows():
        img = io.imread(i.Dir)
        X, Y, I = corrLib.divide_windows(img, windowsize=windowsize, step=step)
        I_list.append(I)
    stack = np.stack(I_list, axis=0)

    return stack
예제 #6
0
파일: dc_adv.py 프로젝트: ZLoverty/Python
    f.write('fps = {:d}\n'.format(fps))
    f.write('step = {:d}\n'.format(step))

limg = cl.readseq(folder_img)

# load piv and corresponding images
l = cl.readdata(folder_piv)
for num, i in l.iterrows():
    if num >= int(len(l)/3*2):
        name = i.Name
        n0 = int(name.split('-')[0])
        n1 = n0 + interval
        if n1 <= len(limg) - 1:        
            I0 = io.imread(os.path.join(folder_img, '{:04d}.tif'.format(n0)))
            I1 = io.imread(os.path.join(folder_img, '{:04d}.tif'.format(n1)))
            X, Y, I0s = cl.divide_windows(I0, windowsize=[50, 50], step=25)
            X, Y, I1s = cl.divide_windows(I1, windowsize=[50, 50], step=25)
            pivData = pd.read_csv(i.Dir)
            ux = np.array(pivData.u).reshape(I0s.shape)
            uy = np.array(pivData.v).reshape(I0s.shape)
            dcx = I0s - np.roll(I0s, 1, axis=1)
            dcy = I0s - np.roll(I0s, 1, axis=0)
            dc = -(I1s - I0s) # high concentration -> low intensity, so intensity increase -> concentration decrease
            adv = dc/interval + ux/fps*dcx/step + uy/fps*dcy/step # unit: /frame
            vdc = ux/fps*dcx/step + uy/fps*dcy/step # unit: /frame
            data = pd.DataFrame().assign(x=pivData.x, y=pivData.y, dcx=dcx.flatten(), dcy=dcy.flatten(), dc=dc.flatten(), adv=adv.flatten(), vdc=vdc.flatten())
            data.to_csv(os.path.join(folder_out, '{:04d}-{:04d}.csv'.format(n0, n1)), index=False)
            with open(os.path.join(folder_out, 'log.txt'), 'a') as f:
                f.write(time.asctime() + ' // {:04d}-{:04d} calculated\n'.format(n0, n1))

예제 #7
0
파일: df2.py 프로젝트: ZLoverty/Python
l = readseq(folder)
img = io.imread(l.Dir.loc[0])
size_min = 20
L = min(img.shape)
boxsize = np.unique(np.floor(np.logspace(np.log10(size_min),
                    np.log10((L-size_min)/2),100)))
df = pd.DataFrame()
for num, i in l.iterrows():
    with open(os.path.join(output_folder, 'log.txt'), 'a') as f:
        f.write(time.asctime() + ' // ' + i.Name + ' calculated\n')
    img = io.imread(i.Dir)
    bp = bpass(img, 3, 100)
    bp_mh = match_hist(bp, img)
    framedf = pd.DataFrame()
    for bs in boxsize: 
        X, Y, I = divide_windows(bp_mh, windowsize=[bs, bs], step=50*size_min)
        tempdf = pd.DataFrame().assign(I=I.flatten(), t=int(i.Name), size=bs, 
                       number=range(0, len(I.flatten())))
        framedf = framedf.append(tempdf)
    df = df.append(framedf)
    
df_out = pd.DataFrame()
for number in df.number.drop_duplicates():
    subdata1 = df.loc[df.number==number]
    for s in subdata1['size'].drop_duplicates():
        subdata = subdata1.loc[subdata1['size']==s]
        d = s**2 * np.log(np.array(subdata.I)).std()
        n = s**2 
        tempdf = pd.DataFrame().assign(n=[n], d=d, size=s, number=number)
        df_out = df_out.append(tempdf)
        
예제 #8
0
파일: df2_nobp.py 프로젝트: ZLoverty/Python
                         100)))

if os.path.exists(output_folder) == False:
    os.makedirs(output_folder)
with open(os.path.join(output_folder, 'log.txt'), 'w') as f:
    f.write('size_min = {0:d}\n'.format(size_min))
    f.write('step = {0:d}\n'.format(step))

df = pd.DataFrame()
for num, i in l.iterrows():
    with open(os.path.join(output_folder, 'log.txt'), 'a') as f:
        f.write(time.asctime() + ' // ' + i.Name + ' calculated\n')
    img = io.imread(i.Dir)
    framedf = pd.DataFrame()
    for bs in boxsize:
        X, Y, I = divide_windows(img, windowsize=[bs, bs], step=step)
        tempdf = pd.DataFrame().assign(I=I.flatten(),
                                       t=int(i.Name),
                                       size=bs,
                                       number=range(0, len(I.flatten())))
        framedf = framedf.append(tempdf)
    df = df.append(framedf)

df_out = pd.DataFrame()
for number in df.number.drop_duplicates():
    subdata1 = df.loc[df.number == number]
    for s in subdata1['size'].drop_duplicates():
        subdata = subdata1.loc[subdata1['size'] == s]
        d = s**2 * np.array(subdata.I).std()
        n = s**2
        tempdf = pd.DataFrame().assign(n=[n], d=d, size=s, number=number)
예제 #9
0
     img_name = str('{:04d}'.format(int(name) - tau))
     if os.path.exists(os.path.join(folder_den,
                                    img_name + '.tif')) == False:
         print('no match image')
         continue
     img = io.imread(os.path.join(folder_den, img_name + '.tif'))
     if options == 'raw':
         print('performing bpass on ' + img_name)
         img = cl.match_hist(mil.bpass(img, 3, 500), img)
     print('tau={:d}'.format(tau) + ', div-' + name + ' x img-' + img_name)
     xlen = len(div.x.drop_duplicates())
     ylen = len(div.y.drop_duplicates())
     divfield = np.array(div['div']).reshape(ylen, xlen)
     divfield = divfield - divfield.mean()
     divfield_crop = divfield  #[20:36, 8:24]
     X, Y, I = cl.divide_windows(img, windowsize=[50, 50], step=25)
     I_norm = I / abs(I).max()
     I_norm = I_norm - I_norm.mean()
     I_crop = I_norm  #[20:36, 8:24]
     tL.append(int(img_name))
     C = (divfield_crop *
          I_crop).mean() / divfield_crop.std() / I_crop.std()
     CL.append(C)
     data = pd.DataFrame().assign(t=tL, autocorr=CL)
     saveDir = os.path.join(folder_ixdiv, 'tau={:d}'.format(tau))
     if os.path.exists(saveDir) == False:
         os.makedirs(saveDir)
     data.to_csv(os.path.join(saveDir, 'data.csv'), index=False)
 with open(os.path.join(folder_ixdiv, 'log.txt'), 'a') as f:
     f.write(time.asctime() + ' // tau=' + str(tau) + ' calculated\n')
 CLL.append(CL)
예제 #10
0
if os.path.exists(output_folder) == 0:
    os.makedirs(output_folder)
with open(os.path.join(output_folder, 'log.txt'), 'w') as f:
    f.write('Input folder: ' + str(input_folder) + '\n')
    f.write('Output folder: ' + str(output_folder) + '\n')
    f.write('wsize: ' + str(wsize) + '\n')
    f.write('step: ' + str(step) + '\n')

l = readseq(input_folder)
num_frames = len(l)
num_sample = 100  # can modify in the future
if num_sample <= num_frames:
    for num, i in l.iterrows():
        if num % int(num_frames / num_sample) == 0:
            img = io.imread(i.Dir)
            X, Y, I = divide_windows(img, windowsize=[wsize, wsize], step=step)
            XI, YI, CI = corrI(X, Y, I)
            dc = distance_corr(XI, YI, CI)
            dc.to_csv(os.path.join(output_folder, i.Name + '.csv'),
                      index=False)
            with open(os.path.join(output_folder, 'log.txt'), 'a') as f:
                f.write(time.asctime() + ' // ' + i.Name + ' calculated\n')
else:
    for num, i in l.iterrows():
        img = io.imread(i.Dir)
        X, Y, I = divide_windows(img, windowsize=[wsize, wsize], step=step)
        XI, YI, CI = corrI(X, Y, I)
        dc = distance_corr(XI, YI, CI)
        dc.to_csv(os.path.join(output_folder, i.Name + '.csv'), index=False)
        with open(os.path.join(output_folder, 'log.txt'), 'a') as f:
            f.write(time.asctime() + ' // ' + i.Name + ' calculated\n')
예제 #11
0
파일: div2.py 프로젝트: ZLoverty/Python
if os.path.exists(output_folder) == 0:
    os.makedirs(output_folder)
with open(os.path.join(output_folder, 'log.txt'), 'w') as f:
    pass

count = 0
ld = corrLib.readdata(piv_folder)

for num, i in ld.iterrows():
    pivData = pd.read_csv(os.path.join(piv_folder, i.Dir))
    folder, file = os.path.split(i.Dir)
    name = file.split('-')[0]
    img = io.imread(os.path.join(folder_img, name + '.tif'))
    c, v, divcn, divcv, divv = corrLib.div_field(img, pivData, winsize, step)
    X, Y, I = corrLib.divide_windows(img,
                                     windowsize=[winsize, winsize],
                                     step=step)
    data = pd.DataFrame().assign(x=X.flatten(),
                                 y=Y.flatten(),
                                 divcn=divcn.flatten(),
                                 divcv=divcv.flatten(),
                                 divv=divv.flatten())
    data.to_csv(os.path.join(output_folder, i.Name + '.csv'), index=False)
    with open(os.path.join(output_folder, 'log.txt'), 'a') as f:
        f.write(time.asctime() + ' // ' + name + ' calculated\n')
""" SYNTAX
python div2.py piv_folder folder_img output_folder winsize step
"""
""" TEST PARAMS
piv_folder = I:\Github\Python\Correlation\test_images\div
folder_img = I:\Github\Python\Correlation\test_images\div