Skip to content

Commit

Permalink
Merge changes to include reshaping #3
Browse files Browse the repository at this point in the history
  • Loading branch information
danobohud committed Oct 25, 2021
2 parents a6f6a0d + 4949014 commit ee70a5b
Show file tree
Hide file tree
Showing 3 changed files with 182 additions and 39 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
**/.DS_Store
venv/**
venv/**
data/*.tif
92 changes: 92 additions & 0 deletions coloc/backend/classes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
from datetime import datetime
import os
from PIL import Image


class pipeline_object():
"""Pipeline object class. Any image becomes an instance of this class.
The class operates using its methods to move an image through the pipeline.
"""
def __init__(self, filepath):
self.filepath = filepath
self.timestamp = (datetime.now()).strftime('%Y%m%d-%H%M%S')
self.temp_dir_path = os.path.join('./data/output/',
self.timestamp + '/')
if not os.path.exists(self.temp_dir_paths):
os.mkdir(self.temp_dir_path)

def reshape_im(self):
"""Reshapes an image such that the dimensions are square, using the dimension
of the smallest side.
:param sourcefile: input multi-image .tiff
:type sourcefile: string
:return resized_file_path: Path to the new, downsized file.
:type resized_file_path: string
"""
im = Image.open(self.filepath)
# Find smallest dimension n and set image size to square n x n.
smallest_dim = min(im.size)
im.resize([smallest_dim, smallest_dim], resample=Image.LANCZOS)
resized_file_path = os.path.join(self.filepath, 'resized',
os.path.basename(self.filepath))
# Save and keep the new path as a class variable.
im.save(resized_file_path)
self.resized_file_path = resized_file_path

return

def split(self):
"""Split input .tiff file into separate RGB files and save to a sub-directory
:param sourcefile: input multi-image .tiff
:type sourcefile: string
:return: list of file paths for image import'''
"""
im = Image.open(self.resized_file_path)
self.num_frames = im.n_frames

for i in range(self.num_frames):
im.save(os.path.join(self.temp_dir_path, 'page_{0}.tif'.format(i)))

return

def rescale(im_2D, threshold=False):
"""Minmax rescale a 2D image
:param im_2D: input array
:type im_2D: numpy array
return: rescaled image"""

if len(np.shape(im_2D)) != 2:
raise ValueError("Input image should have two dimensions")
if im_2D.all() == 0:
return im_2D
elif threshold:
im_2D = (im_2D-im_2D.min())/(im_2D.max()-im_2D.min())
trim = im_2D < threshold
im_2D[trim] = 0
return im_2D
else:
return (im_2D-im_2D.min())/(im_2D.max()-im_2D.min())

def rescale_stack(im_3d, threshold=False):
"""Rescale RGB image using minmax rescaling
:param im_3d: input RGB image
:type im_3d: numpy array"""

stack = ['R', 'G', 'B']
out = []
s = 0
for channel in [im_3d[:, :, i] for i in range(im_3d.shape[-1])]:
out.append(rescale(channel, threshold))
s += 1
return np.dstack(out)




126 changes: 88 additions & 38 deletions coloc/backend/preprocessing.py
Original file line number Diff line number Diff line change
@@ -1,27 +1,71 @@
from PIL import Image
import numpy as np
import matplotlib.pyplot as plt
from numpy.lib.index_tricks import ix_
from numpy.lib.utils import source
import os
from sklearn.cluster import KMeans
from datetime import datetime

inputdict = {
'in_path': './data/in/test.tiff',
'out_path': './data/out/test.tiff',
'threshold': 0.3
}


def generate_timestamp():
"""Generates a timestamp, based on the current time.
:return timestamp: The current time, in format YYYYMMDD-hhmmss
"""
timestamp = (datetime.now()).strftime('%Y%m%d-%H%M%S')
return timestamp


def listfiles():
return [file for file in os.listdir(os.getcwd())]

def split(sourcefile,outpath):
'''Split input .tiff file into separate RGB files and save to an output directory

def reshape_im(sourcefile, timestamp):
"""Reshapes an image such that the dimensions are square, using the dimension
of the smallest side.
:param sourcefile: input multi-image .tiff
:type sourcefile: string
:return: list of file paths for image import'''
im=Image.open(sourcefile)
names=[]
files=[file for file in os.listdir(outpath)]
for i in range(32): # Need to genericise this for any length of multiimage array
n=outpath+'/img_%s.tif'%(i,)
:return resized_file_path: Path to the new, downsized file.
:type resized_file_path: string
"""
im = Image.open(sourcefile)
temp_dir_path = os.path.join('./data/output/', timestamp + '/')
if not os.path.exists(temp_dir_paths):
os.mkdir(temp_dir_path)

# Find smallest dimension n and set image size to square n x n.
smallest_dim = min(im.size)
im.resize([smallest_dim, smallest_dim], resample=Image.LANCZOS)
resized_file_path = os.path.join(temp_dir_path, os.path.basename(filename))

im.save(resized_file_path)

return resized_file_path


def split(sourcefile):
"""Split input .tiff file into separate RGB files and save to a sub-directory
:param sourcefile: input multi-image .tiff
:type sourcefile: string
:return: list of file paths for image import'''
"""
im = Image.open(sourcefile)
names = []
if 'data' not in listfiles():
os.mkdir('data/')
files = [file for file in os.listdir(os.getcwd()+'/data')]

for i in range(im.n_frames):
n = 'data/page_%s.tif' % (i,)
if n not in files:
names.append(n)
im.seek(i)
Expand All @@ -30,10 +74,10 @@ def split(sourcefile,outpath):

def parse_ims(sourcefile,outpath=False):
"""Load images from a stacked .tiff file
:param sourcefile: source file
:type sourcefile: string
return: array of Z-stacked images"""
if not outpath:
outpath = "data/output"
Expand All @@ -43,62 +87,68 @@ def parse_ims(sourcefile,outpath=False):
im_arr.append(np.asarray(Image.open(im)))
return np.asarray(im_arr)

def rescale(im_2D,threshold=False):

def rescale(im_2D, threshold=False):
"""Minmax rescale a 2D image
:param im_2D: input array
:type im_2D: numpy array
return: rescaled image"""

if len(np.shape(im_2D))!=2:
if len(np.shape(im_2D)) != 2:
raise ValueError("Input image should have two dimensions")
if im_2D.all() == 0:
return im_2D
elif threshold:
im_2D= (im_2D-im_2D.min())/(im_2D.max()-im_2D.min())
trim=im_2D<threshold
im_2D[trim]=0
im_2D = (im_2D-im_2D.min())/(im_2D.max()-im_2D.min())
trim = im_2D < threshold
im_2D[trim] = 0
return im_2D
else:
return (im_2D-im_2D.min())/(im_2D.max()-im_2D.min())
return (im_2D-im_2D.min())/(im_2D.max()-im_2D.min())


def rescale_stack(im_3d,threshold=False):
def rescale_stack(im_3d, threshold=False):
"""Rescale RGB image using minmax rescaling
:param im_3d: input RGB image
:type im_3d: numpy array
:return: numpy array"""

out=[]
s=0
for channel in [im_3d[:,:,i] for i in range(im_3d.shape[-1])]:
out.append(rescale(channel,threshold))
s+=1
stack = ['R', 'G', 'B']
out = []
s = 0
for channel in [im_3d[:, :, i] for i in range(im_3d.shape[-1])]:
out.append(rescale(channel, threshold))
s += 1
return np.dstack(out)

# Split source file

sourcefile="data/input/colocsample1bRGB_BG.tif"
threshold=0.5
sourcefile = "./data/input/colocsample1bRGB_BG.tif"
threshold = 0.5


def preprocess(sourcefile,threshold,visualise=True):
im_arr=parse_ims(sourcefile)
scaled_ims=np.asarray([rescale_stack(im,threshold=threshold) for im in im_arr])
s=0
def preprocess(sourcefile, threshold, visualise=True):
im_arr = parse_ims(sourcefile)
scaled_ims = np.asarray(
[rescale_stack(im, threshold=threshold) for im in im_arr])
s = 0
if visualise:
for i in range(len(im_arr)):
plt.imshow(im_arr[i])
plt.title("Original image %s"%(str(s+1)))
plt.title("Original image %s" % (str(s+1)))
plt.show()
plt.imshow(scaled_ims[i])
plt.title("Processed image %s"%(str(s+1)))
plt.title("Processed image %s" % (str(s+1)))
plt.show()
s+=1
s += 1
return im_arr, scaled_ims

original, preprocessed = preprocess(sourcefile,threshold,visualise=False)

original, preprocessed = preprocess(sourcefile, threshold, visualise=False)

def fit_clusters(im,num_clusters):
mask= im >0
Expand Down

0 comments on commit ee70a5b

Please sign in to comment.