Commit 9423a97f authored by Saad Jbabdi's avatar Saad Jbabdi
Browse files

latest changes

parent 5d50ee84
......@@ -23,7 +23,7 @@ def append_file_content(fname,image_list,count_list):
# Load Numpy array
im = np.load(f.strip())
if not check_imshape(im.shape):
print("Error: Bad Image dimensions. Must be square and multiple of {}".format(DB_IMAGE_RES))
raise(Exception("Error: Bad Image dimensions. Must be square and multiple of {}".format(DB_IMAGE_RES)))
sizx, sizy, _ = im.shape
......
#!/usr/bin/env python
import matplotlib as mpl
import sys
if(sys.platform=='darwin'):
mpl.use('wxagg')
# First imports
# (further imports are done after argparse)
import argparse
from matplotlib.widgets import RectangleSelector, Slider
from matplotlib.image import AxesImage
import numpy as np
import os
import os.path as op
import shutil
import matplotlib.pyplot as plt
import argparse
import sys
import time
# use this for BigTiff
import tifffile as tif
from skimage import io as skio
# pilfered from https://stackoverflow.com/a/19306776
......@@ -33,6 +20,11 @@ def get_ax_size(fig, ax):
class ZoneSelector(object):
"""
This class is in charge of the zone selection
Selected zones are written into image files and some info stored in text file
"""
def __init__(self,ax,outbase,img):
self.box = []
self.axis = ax
......@@ -68,7 +60,10 @@ class ZoneSelector(object):
class BigImageViewer(object):
"""
This class deals with displaying a big image
with adaptive resolution
"""
def __init__(self, fig, ax, data):
self.fig = fig
self.ax = ax
......@@ -162,17 +157,34 @@ def main():
parser = argparse.ArgumentParser(
"Select zone of interest in an image"
)
parser.add_argument('-ow', '--overwrite', action='store_true', help='Overwrite output directory if it exists')
parser.add_argument('--overwrite', action='store_true', help='Overwrite output directory if it exists')
required = parser.add_argument_group('Required arguments')
required.add_argument("-i","--input", required=True, help="Input file name of main image including extension.")
required.add_argument("-o","--output_folder", required=True, help="Name of output folder.")
args = parser.parse_args()
# DO ALL IMPORTS AFTER PARSING
import sys
import os
import os.path as op
import shutil
import time
import matplotlib as mpl
if(sys.platform=='darwin'):
mpl.use('wxagg')
import matplotlib.pyplot as plt
import tifffile as tif
# use this for BigTiff
from skimage import io as skio
# END IMPORTS
image_name = args.input
outfolder = args.output_folder
outfolder = args.output_folder
# Deal with existing output
# Deal with existing output folder
overwrite = args.overwrite
if op.exists(outfolder):
if not overwrite:
......@@ -192,6 +204,11 @@ def main():
print("Reading input Image '{}' ...".format(image_name))
t0 = time.clock()
im = skio.imread(image_name)
# JP2K .... not working yet ....
#jp2 = glymur.Jp2k(image_name)
#im = jp2[:]
print(" ... this took {} seconds".format(time.clock()-t0))
image_base = op.basename(image_name)
......@@ -204,7 +221,7 @@ def main():
plt.title('Click and drag to draw zones\n Close the window when done.')
biv = BigImageViewer(fig, ax, im)
zs = ZoneSelector(ax,op.join(outfolder,image_base),im)
zs = ZoneSelector(ax,op.join(outfolder,image_base),im)
plt.show()
......
#!/usr/bin/env python
# Generate models from archiectures
# Generate models from architectures
# Saad, 03/2019
# import modules
......
......@@ -108,7 +108,7 @@ def forward_prediction(patches, model, args):
return model_out
# save_results(density, cells, imnp, patches.shape, args)
def save_results(density, cells, image, patch_shape, args):
basename = args.out
......@@ -120,9 +120,15 @@ def save_results(density, cells, image, patch_shape, args):
print('* Saving predictions: {}'.format(outfile))
# Estimate number of cells in image
ncells = density.mean()*image.size/patch_shape[1]/patch_shape[2]
correction_factor = .5
ncells = density.sum()/patch_shape[1]/patch_shape[2]*correction_factor
print('* Detected {} Cells'.format(ncells))
# Save in text file
with open(basename+"_cells.txt",'w') as f:
f.write("{}".format(np.round(ncells)))
# Save density+cell number in same file
np.savez_compressed(outfile, density=density, cells=cells, ncells=ncells)
#visualise results
......
#!/usr/bin/env python3
import argparse
from skimage import io as skio
import os.path as op
def main():
p = argparse.ArgumentParser('Split big image into tiles')
p.add_argument("-i","--input",required=True,help="Input image")
p.add_argument("-o","--output",required=True,help="Output basename")
p.add_argument("-t","--tilesize",required=True,type=int,nargs=2,help="Tile size")
args = p.parse_args()
print('Reading input image {}'.format(args.input))
im = skio.imread(args.input)
print('Tiling')
(w,h,d) = im.shape
dw = args.tilesize[0]
dh = args.tilesize[1]
for ih in range(0,h,dh):
for iw in range(0,w,dw):
tile = im[iw:(w,iw+dw)[iw+dw<w],ih:(h,ih+dh)[ih+dh<h],:]
outfile = op.join(args.output,'tile_w_{}_h_{}.tif'.format(iw,ih))
skio.imsave(outfile,tile)
print('Done.')
if __name__ == '__main__':
main()
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment