Merge branch 'master' of github.com:JamesEMcClure/LBPM-WIA

This commit is contained in:
James McClure 2016-10-10 11:14:05 -04:00
commit 1512df8fff
2 changed files with 47 additions and 42 deletions

View File

@ -4,50 +4,53 @@ import numpy as np
import matplotlib.pylab as plt
from glob import glob
from PIL import Image # import Python Imaging Library (PIL)
import os
import sys
basename=sys.argv[1]
plt.rcParams["savefig.directory"] = os.getcwd() # change the default pylab 'savefig' path (i.e. ~/) to cwd
from SegmentMicromodelTiff_utils import convert_image_to_array,read_input_parameters
# Load a group of *.in files
# e.g. 'parameters_1.in' etc.
file_input_group = glob('Micromodel.in')
#output_result = np.empty((0,),dtype=float)
# Check if there is a proper command line argument
if len(sys.argv) !=2:
sys.stderr.write('Usage: ' + sys.argv[0] + ' <Input parameter file>\n')
sys.exit()
# end if
input_parameter_file = sys.argv[1] # Give the name of the input parameter file
base_name_output = '_segmented.raw'# The string will be appended at the end of the
# name of the input image as an output file name
image_format = '.tiff' # format of experimental images
# Read other input parameters
Para = read_input_parameters(input_parameter_file)
imin = Para['imin']
imax = Para['imax']
imin_b = Para['imin_b'] # greater than 'imin'
imax_b = Para['imax_b'] # smaller than 'imax'
top_layer = Para['top_layer']
bot_layer = Para['bot_layer']
DEPTH = Para['DEPTH']
NY = imax - imin
NZ = NY
NX = DEPTH+top_layer+bot_layer
# parameters for segmentation
threshold_nw = Para['threshold_nw'] # NW phase: RGB values > threshold_nw
threshold_s = Para['threshold_s'] # solid: RGB values < threshold_s
# W phase: threshold_s <= RGB values <= threshold_nw
# Load a group of image files with 'base_name' in the names of files
# e.g. 'Micromodel_1.tiff' etc.
file_input_group = glob('*'+Para['base_name']+'*'+image_format) # need to match the image format
# Process all imported experimental images
if not file_input_group:
print 'Error: Input files cannot be found ! '
else:
for ii in range(len(file_input_group)):
file_input_single = file_input_group[ii]
Para = read_input_parameters(file_input_single)
# set your output file names
input_file_idx = file_input_single[file_input_single.find('parameters_')+len('parameters_'):file_input_single.find('.in')]
output_file_name = 'Segmented_image_test_'+input_file_idx+'.raw'
imin = Para['imin']
imax = Para['imax']
imin_b = Para['imin_b'] # greater than 'imin'
imax_b = Para['imax_b'] # smaller than 'imax'
top_layer = Para['top_layer']
bot_layer = Para['bot_layer']
DEPTH = Para['DEPTH']
NY = imax - imin
NZ = NY
NX = DEPTH+top_layer+bot_layer
print "Processing "+Para['image_file_name']+" now..."
print "Processing image "+file_input_single+" now..."
print "------ Micromodel dimensions (NX, NY, NZ) are (%i, %i, %i) ------" % (NX,NY,NZ)
# parameters for segmentation
threshold_nw = Para['threshold_nw'] # NW phase: RGB values > threshold_nw
threshold_s = Para['threshold_s'] # solid: RGB values < threshold_s
# W phase: threshold_s <= RGB values <= threshold_nw
# Get an array from the input .tiff image
im_array = convert_image_to_array(Para['image_file_name'],imin,imax)
im_array = convert_image_to_array(file_input_single,imin,imax)
# Initialize the segmented image 'ID'
# NOTE: 'ID' has the dimension (height, width, DEPTH)
@ -66,7 +69,9 @@ else:
POROSITY = 1.0 - 1.0*ID[ID==0].size/ID.size
print "Porosity of the micromodel is: "+str(POROSITY)
# Write the segmeted data to the output
# set the output file names (need to chop off say the '.tiff' part)
output_file_name = file_input_single[:file_input_single.find('.')]+base_name_output
# Write out the segmented data (in binary format)
print "The segmented image is written to "+output_file_name+" now..."
ID.tofile(output_file_name)
@ -79,20 +84,20 @@ else:
# In case you want to test the segmentation
plt.figure(1)
#plt.figure(1)
#plt.subplot(1,2,1)
#plt.title('original RGB figure')
#plt.pcolormesh(im_array);
#plt.axis('equal')
#plt.colorbar()
#plt.subplot(1,2,2)
plt.title('Segmented image')
#plt.title('Segmented image')
# This will show the last-processed segmented image
cmap = plt.cm.get_cmap('hot',3) #Plot 3 discrete colors for NW, W and Solids
cax=plt.pcolormesh(ID[:,:,NX/2],cmap=cmap,vmin=-0.5,vmax=2.5);
cbar = plt.colorbar(cax,ticks=[0,1,2])
plt.axis('equal')
plt.show()
#cmap = plt.cm.get_cmap('hot',3) #Plot 3 discrete colors for NW, W and Solids
#cax=plt.pcolormesh(ID[:,:,NX/2],cmap=cmap,vmin=-0.5,vmax=2.5);
#cbar = plt.colorbar(cax,ticks=[0,1,2])
#plt.axis('equal')
#plt.show()

View File

@ -21,7 +21,7 @@ def convert_image_to_array(file_name,imin,imax):
def read_input_parameters(input_file_name):
# input_file_name is a string
# The *.in file has the following lines
# line 0: the name of the image file e.g. *.tiff
# line 0: the base name of the experimental images
# line 1: imin imax
# line 2: imin_b imax_b
# line 3: top_layer bottom_layer DEPTH
@ -30,7 +30,7 @@ def read_input_parameters(input_file_name):
# 'threshold_s' means: solid: RGB values < threshold_s
f = open(input_file_name,'r') # read-only
lines = f.readlines()
output_file = {'image_file_name':lines[0].splitlines()[0]}
output_file = {'base_name':lines[0].splitlines()[0]}
line1_array = np.fromstring(lines[1].splitlines()[0],dtype=np.int32,sep=' ')
line2_array = np.fromstring(lines[2].splitlines()[0],dtype=np.int32,sep=' ')
line3_array = np.fromstring(lines[3].splitlines()[0],dtype=np.int32,sep=' ')