Example #1
0
def load_data(test_size=0.2, from_npy=True):
    if from_npy:
        X = np.load('/export/home/lparcala/dataFace/Xsaved.npy')
        y = np.load('/export/home/lparcala/dataFace/ysaved.npy')
    else:
        y = readLabels('wanted_attributes.txt', 'list_attr_celeba.txt')
        X = np.array(ImageSequence('../Face/img_align_celeba.zip'))
        X = X.astype('float32')

        # convert labels to boolean
        y[y < 0] = 0
        y[y > 0] = 1

        h5f = h5py.File('data.h5', 'w')
        h5f.create_dataset('images', data=X)
        h5f.create_dataset('labels', data=y)
        # np.save("/export/home/lparcala/dataFace/Xsaved.npy", X)
        np.save("/export/home/lparcala/dataFace/ysaved.npy", y)

    print("Done reading data from disk.")

    X_train, X_test, y_train, y_test = train_test_split(X,
                                                        y,
                                                        test_size=test_size,
                                                        random_state=42)
    return X_train, y_train, X_test, y_test
Example #2
0
    def _load_data(self, filepath: Union[str, Path]):
        if type(filepath) == str:
            filepath = Path(filepath)

        if not filepath.is_dir():
            raise ValueError(
                f'The path you provided does not exist: {filepath}.')

        paths = Path(filepath)
        image_paths = list(paths.glob("*.tif"))
        try:
            meta_data_path = list(paths.glob("*.txt"))[0]
        except IndexError:
            raise ValueError(
                f'Could not find a metadata file in {filepath}. Expected to find a .txt file there.'
            )
        if not image_paths:
            raise ValueError(
                f"Could not find any tif image files in {filepath}")

        with meta_data_path.open("r", encoding="latin-1") as fid:
            text_file = fid.readlines()

        for line in text_file:
            if VOXEL_HEIGHT in line.split():
                self.meta_data[VOXEL_HEIGHT] = float(line.split()[-1][:-1])

        if VOXEL_HEIGHT not in self.meta_data:
            raise ValueError(
                f'Unable to find voxel height in {meta_data_path}')

        images = ImageSequence(list(image_paths), as_grey=True)
        grey_images = self._get_grey_sequence(images)
        self._threshold_and_crop_images(grey_images)
Example #3
0
 def prepare(self):
     directory = os.path.join(path, 'video', 'image_sequence')
     self.v = tp.invert_image(ImageSequence(os.path.join(directory, '*.png')))
     # mass depends on pixel dtype, which differs per reader
     minmass = self.v[0].max() * 2
     self.PARAMS = {'diameter': 11, 'minmass': minmass}
     self.expected = tp.batch(self.v[[0, 1]], engine='python', meta=False,
                              **self.PARAMS)
Example #4
0
def correctImagesCFG(home, imgFolder, imgExt, selectBox, autoBox, crop, stds):
    targetDir = os.path.join(home, imgFolder)
    outDir = os.path.join(targetDir, "corrected")
    fullImg = 1

    if not os.path.exists(outDir):
        os.makedirs(outDir)

    filePattern = os.path.join(targetDir, "*." + imgExt)
    first = 1
    for filename in glob.glob(filePattern):

        images = ImageSequence(filename)
        imgcv = images[0]
        imName = os.path.basename(filename)

        prePost = imName.split(".")
        noEnd = prePost[0]
        if selectBox:
            if first == 1:
                fig, ax = plt.subplots()
                imgplot = ax.imshow(imgcv)
                getter1 = getter(ax, fig)

                plt.show()

                x = getter1.x
                y = getter1.y

                x.sort()
                y.sort()

                for i in range(0, len(x)):
                    x[i] = int(x[i])

                for i in range(0, len(y)):
                    y[i] = int(y[i])
                first = 0

        elif autoBox:
            x = crop[0]
            y = crop[1]
        else:
            dims = imgcv.shape
            x = [0, dims[0]]
            y = [0, dims[1]]

        croppedImg = imgcv[y[0]:y[1], x[0]:x[1]]
        outImg = standardizeCropped(imgcv, croppedImg, stds)
        outImg = outImg * 256
        outImg = outImg.astype(np.uint8)

        im = Image.fromarray(outImg)

        saveName = os.path.join(outDir, imName)
        im.save(saveName)
def run():
    # download to this folder
    current_folder = os.sep.join(__file__.split(os.sep)[:-1])
    dpc_demo_data_path = os.path.join(current_folder, 'SOFC')

    if not os.path.exists(dpc_demo_data_path):
        sofc_file = os.path.join(current_folder, 'SOFC.zip')
        print('The required test data directory was not found.'
              '\nDownloading the test data to %s' % dpc_demo_data_path)
        # todo make this not print every fraction of a second
        call(('wget https://www.dropbox.com/s/963c4ymfmbjg5dm/SOFC.zip -P %s' %
              current_folder),
             shell=True)
        # unzip it into this directory
        unzip(sofc_file)

    # 1. Set parameters
    start_point = [1, 0]
    first_image = 1
    pixel_size = (55, 55)
    focus_to_det = 1.46e6
    scan_xstep = 0.1
    scan_ystep = 0.1
    scan_rows = 121
    scan_cols = 121
    energy = 19.5
    roi = None
    padding = 0
    weighting = 1.
    bad_pixels = None
    solver = 'Nelder-Mead'
    images = ImageSequence(dpc_demo_data_path + "/*.tif")
    img_size = images[0].shape
    ref_image = np.ones(img_size)
    scale = True
    negate = True

    print('running dpc')
    # 2. Use dpc.dpc_runner
    phase, amplitude = dpc.dpc_runner(ref_image, images, start_point,
                                      pixel_size, focus_to_det, scan_rows,
                                      scan_cols, scan_xstep, scan_ystep,
                                      energy, padding, weighting, solver, roi,
                                      bad_pixels, negate, scale)

    # 3. Save intermediate and final results
    scipy.misc.imsave(os.path.join(current_folder, 'phase.jpg'), phase)
    np.savetxt(os.path.join(current_folder, 'phase.txt'), phase)
    scipy.misc.imsave(os.path.join(current_folder, 'amplitude.jpg'), amplitude)
    np.savetxt(os.path.join(current_folder, 'amplitude.txt'), amplitude)
Example #6
0
def build_image_cycle(path):
    """Build image cycles, essentially generators with endless images

    Parameters
    ----------
    path: str
        Path to the files to be used as the base for the cycle, this can
        include some globing

    Returns
    -------
    Cycler:
        The iterable like object to cycle through the images
    """
    imgs = ImageSequence(os.path.join(path, '*.tif*'))
    return cycler(pe1_image=[i for i in imgs])
    def open(self, initializer: Path = None):
        if initializer is not None:
            self.initializer = initializer

        if not self.initializer.is_dir():
            raise ValueError(f'{self.initializer} must be a directory')

        self.imseq = ImageSequence(str(self.initializer))
        setattr(self, 'close', getattr(self.imseq, 'close'))

        self.nfrms = len(self.imseq)
        self.ysize = self.imseq.frame_shape[0]
        self.xsize = self.imseq.frame_shape[1]
        self.dtype = self.imseq._dtype

        if len(self.imseq.frame_shape) > 2:
            self.nchannels = self.imseq.frame_shape[2]
def run():
    # download to this folder
    current_folder = os.sep.join(__file__.split(os.sep)[:-1])
    dpc_demo_data_path = os.path.join(current_folder, 'SOFC')
    if not os.path.exists(dpc_demo_data_path):
        zip_file_url = 'https://www.dropbox.com/s/963c4ymfmbjg5dm/SOFC.zip?dl=1'
        import download
        download.run()
        # download_zip(zip_file_url, current_folder)

    # 1. Set parameters
    start_point = [1, 0]
    first_image = 1
    pixel_size = (55, 55)
    focus_to_det = 1.46e6
    scan_xstep = 0.1
    scan_ystep = 0.1
    scan_rows = 121
    scan_cols = 121
    energy = 19.5
    roi = None
    padding = 0
    weighting = 1.
    bad_pixels = None
    solver = 'Nelder-Mead'
    images = ImageSequence(dpc_demo_data_path + "/*.tif")
    img_size = images[0].shape
    ref_image = np.ones(img_size)
    scale = True
    negate = True

    print('running dpc')
    # 2. Use dpc.dpc_runner
    phase, amplitude = dpc.dpc_runner(ref_image, images, start_point,
                                      pixel_size, focus_to_det, scan_rows,
                                      scan_cols, scan_xstep, scan_ystep,
                                      energy, padding, weighting, solver, roi,
                                      bad_pixels, negate, scale)

    # 3. Save intermediate and final results
    print('saving dpc output to disk in --> %s' % current_folder)
    scipy.misc.imsave(os.path.join(current_folder, 'phase.jpg'), phase)
    np.savetxt(os.path.join(current_folder, 'phase.txt'), phase)
    scipy.misc.imsave(os.path.join(current_folder, 'amplitude.jpg'), amplitude)
    np.savetxt(os.path.join(current_folder, 'amplitude.txt'), amplitude)
from skimage.segmentation import mark_boundaries
from skimage.util import img_as_float
from sklearn.preprocessing import Normalizer
from sklearn import metrics
from sklearn.cluster import KMeans, MiniBatchKMeans
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')


# ## 1-loading Frames And Masks Into Program

# In[3]:

images = ImageSequence('images//birdfall2_000*.png')
masks = ImageSequence('masks//birdfall2_000*.png')


# In[34]:

framecount=29
fig = plt.figure(figsize=(10, 140))
fig.suptitle('20 Object Porposals For 20 Frames')
for k in range(0,framecount):
    img=images[k]       
    ax = fig.add_subplot(framecount, 3, k+1)
    ax.imshow(img)
    
   
Example #10
0
                self.fig.canvas.draw()


targetDir = os.path.join(os.getcwd(), 'annotations')
ext = 'tif'
outDir = targetDir + "\\corrected\\"
fullImg = 1
if not os.path.exists(outDir):
    os.makedirs(outDir)

filePattern = targetDir + "\\*." + ext
first = 1
for filename in glob.glob(filePattern):

    #imgcv = cv2.imread(filename)
    images = ImageSequence(filename)
    imgcv = images[0]
    print(imgcv.dtype)
    fig, ax = plt.subplots()
    imgplot = ax.imshow(imgcv)
    sections = filename.split("\\")
    imName = sections[-1]

    #im.save(outDir+imName)

    #print(result)
    prePost = imName.split(".")
    noEnd = prePost[0]

    if first == 1:
        getter1 = getter(ax, fig)
    #define model function and pass independant variables x and y as a list
    def twoD_Gaussian(x_data_tuple, amplitude, xo, yo, sigma_x, sigma_y,
                      offset):
        (x, y) = x_data_tuple
        xo = float(xo)
        yo = float(yo)
        g = offset + amplitude * np.exp(-(((x - xo) / sigma_x)**2 +
                                          ((y - yo) / sigma_y)**2) / 2)

        return g.ravel()

    try:
        os.stat('fit_images')
    except:
        os.mkdir('fit_images')
    im = ImageSequence('./*.tif')
    im_height, im_width = im[0].shape
    bound_tup = ([15, 5, 5, 0, 0, 0],
                 [+np.Inf, (im_height - 5), (im_width - 5), 1.6, 1.6, +np.Inf])

    df_add = []

    min_pix = np.min(im)
    max_pix = np.max(im)

    for cur_frame in im:
        # Open Image
        print('Frame: ' + str(cur_frame.frame_no))
        data = cur_frame

        # Create x and y indices
# In[19]:


get_ipython().magic('ls')


# In[20]:


from pims import ImageSequence


# In[21]:


images = ImageSequence('fc2_save_2019-05-23-094552-*.jpg')


# In[22]:


images


# In[23]:


red_images = images[::2]


# In[24]:
Created on Wed Nov 28 13:51:13 2018

@author: hschriever
"""

import numpy as np
import cv2
from pims import ImageSequence

#Import the data and convert to np array
#input from command line
DIR_NAME = input('what is the path?\n')

NUM_EIGENVECTORS = 3

raw_data = np.array(ImageSequence(DIR_NAME))
size = raw_data.shape
image_size = (size[2], size[3])
data_PCA_format = imagesToRows(raw_data)  #depth assumed to be in dim 2

mean, eigen_vectors = cv2.PCACompute(data_PCA_format,
                                     mean=None,
                                     maxComponents=NUM_EIGENVECTORS)

#initializing lists that will contain the eigan images
eigen_images = []
eigen_images_pos = []
eigen_images_neg = []
'''

Creating the eigan images
def remove_zeros(arr):
    
    new_arr = []
    for i in range(len(arr)):
        if(arr[i] > 0):
            new_arr.append(arr[i])
    return new_arr


#parent dirrectory of the ome files (ie, one fish)
directory = '/media/lauderdale/mnt/remote_servers/data1/image_data/LightSheetMicroscope/20170803/*.tif'

print('starting analysis of ' + directory)

data = ImageSequence(directory)
print('Loaded in the data')
num_ome = len(data)

video = data[0]
frame = video[100]
img = np.array(frame[0])
print_img(img)

img = np.array(frame[1])
print_img(img)

intensities = np.zeros(num_ome * 512)
print('entering for loop')
for i in range(num_ome):
    print('working on file: ' + str(i))
Example #15
0
def correctImagesCFG(cfg):
    targetDir = os.path.join(cfg['temp']['rootDir'],
                             cfg['imageCorrection']['imgFolder'])
    ext = cfg['imageCorrection']['imgExt']
    outDir = os.path.join(targetDir, "corrected")
    fullImg = 1

    if not os.path.exists(outDir):
        os.makedirs(outDir)

    filePattern = os.path.join(targetDir, "*." + ext)
    first = 1
    print(filePattern)
    for filename in glob.glob(filePattern):

        images = ImageSequence(filename)
        imgcv = images[0]
        #print(imgcv.dtype)

        #sections = filename.split("\\")
        #imName = sections[-1]
        imName = os.path.basename(filename)

        prePost = imName.split(".")
        noEnd = prePost[0]
        if cfg['imageCorrection']['selectBox']:
            if first == 1:
                fig, ax = plt.subplots()
                imgplot = ax.imshow(imgcv)
                getter1 = getter(ax, fig)

                plt.show()

                x = getter1.x
                y = getter1.y

                x.sort()
                y.sort()

                for i in range(0, len(x)):
                    x[i] = int(x[i])

                for i in range(0, len(y)):
                    y[i] = int(y[i])
                first = 0
        elif cfg['imageCorrection']['autoBox']:
            x = cfg['imageCorrection']['cropX']
            y = cfg['imageCorrection']['cropY']
        else:
            dims = imgcv.shape
            x = [0, dims[0]]
            y = [0, dims[1]]

        croppedImg = imgcv[y[0]:y[1], x[0]:x[1]]
        stds = cfg['standard']['stds']
        outImg = standardizeCropped(imgcv, croppedImg, stds)
        outImg = outImg * 256
        outImg = outImg.astype(np.uint8)

        im = Image.fromarray(outImg)

        saveName = os.path.join(outDir, imName)
        im.save(saveName)
from skimage.util import img_as_float
from sklearn.preprocessing import Normalizer
from sklearn import metrics
from sklearn.cluster import KMeans, MiniBatchKMeans
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')

# ## 1-loading Frames And Masks Into Program

# In[3]:

DAGSeg = np.load('Masks/birdfall_masksDagSeg.npy')
KeySeg = np.load('Masks/birdfall_masksKeySeg.npy')
SaliencySeg = ImageSequence('Masks/SaliencySeg//birdfall2_00*.bmp')
SeamSeg = np.load('Masks/birdfall_masksSeamSeg.npy')
JOTSeg = np.load('Masks/birdfall_masksFromJOTS.npy')
GroundTruth = np.load('Masks/birdfall_masksGroundtruth.npy')

# In[4]:

framecount = 20
fig = plt.figure(figsize=(9, 40))
plt.suptitle("GroundTruth", size=36)
for k in range(0, framecount):
    img = GroundTruth[:, :, k]
    ax = fig.add_subplot(framecount, 5, k + 1)
    ax.set_yticklabels([])
    ax.set_xticklabels([])
    ax.imshow(img, cmap=plt.get_cmap('gray'))