bahanonu / ciatah

CIAtah (pronounced cheetah): a software package for calcium imaging analysis of one- and two-photon imaging datasets. Documentation: https://git.io/ciatah_docs. Formerly known as calciumImagingAnalysis (ciapkg).
https://git.io/ciatah_docs
MIT License
80 stars 20 forks source link

Poor cell segmentation #127

Open RandallJEllis opened 1 year ago

RandallJEllis commented 1 year ago

I am getting cell segmentations that look pretty bad, and I think it might be because of how I am preparing the videos. Screenshots of the cell segmentations on the raw bigTIFF video and the preprocessed (downsampled 4x, turbo-reg'd, dfof) video are below:

Raw: raw_movie

Pre-processed dfof_movie

I think the problem might be how I am preparing the bigTIFF video. The video is prepared from TIF images using the following script:

import cv2
import os
import numpy as np
from tqdm import tqdm
from tifffile import *

def tif_to_video(input_path, output_filename):

    # Define the location of the TIF files
    dir_path = f'{input_path}'

    # List of TIF files
    tif_files = [f for f in os.listdir(dir_path) if f.endswith('.TIF')]

    # Sort image filenames by number following 'p' in filename
    framenums = []
    for tif_file in tif_files:
        p_idx = tif_file.find('p')
        underscore_idx = tif_file[p_idx:].find('_')
        framenum = tif_file[p_idx+1:p_idx+underscore_idx]
        framenums.append(int(framenum))

    # order filenames in a list
    tif_files = [tif_file for _, tif_file in sorted(zip(framenums, tif_files))]

    ### bigTIFF ###
    output_file = f'{output_filename}.tif'

    arr_list = []
    # Iterate over the TIF files, import and add each TIF frame to arr_list
    for i, tif_file in tqdm(enumerate(tif_files)):
        img = cv2.imread(os.path.join(dir_path, tif_file), 0)
        img = img[...,None] # add third dimension (i.e., frame)
        arr_list.append(img)

    # concatenate frames into numpy array
    tiff_arr = np.concatenate(arr_list, axis=2)

    # export numpy array in bigTIFF format at 20 FPS
    with TiffWriter(output_file, bigtiff=True) as tif:
        for frame in np.transpose(tiff_arr,(2,0,1)):
            tif.write(frame, metadata={'fps':20.0}, compression='JPEG')    

Any help with this is greatly appreciated. Thanks in advance!

bahanonu commented 1 year ago

Would you be able to send me a folder with a subset of the raw TIF files? Also, what region and indicator/sensor are you imaging? That'll help me get a sense for what it should look like and anything that might need to be changed.

I can also get back with a line or two of code to load and save TIFF file series as a HDF5 using ciatah functions if that helps.

RandallJEllis commented 1 year ago

Hi Biafra,

Would you be able to send me a folder with a subset of the raw TIF files? Also, what region and indicator/sensor are you imaging? That'll help me get a sense for what it should look like and anything that might need to be changed.

Just emailed you a tar.gz of the first 100 frames because it was too big to attach here. The indicator is gcamp6f, and the data are from tumor spheroids, not in the brain.

I can also get back with a line or two of code to load and save TIFF file series as a HDF5 using ciatah functions if that helps.

Would definitely appreciate you sending this! Thank you!

Best,

Randy