Skip to content

Commit

Permalink
Code for counting dilation error instances
Browse files Browse the repository at this point in the history
  • Loading branch information
KatrinaHoffert committed Mar 29, 2017
1 parent 19d0198 commit 65b4a0c
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 2 deletions.
54 changes: 54 additions & 0 deletions count_error_dilation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
'''
Counts how many images would differ from textbook_dilation.
'''

import os, re, sys, enum
from dilate import check_no_error_dilate
from common import Study

def error_count(study, strokes_loc, ground_truth_loc):
'''
Dilates all files.
study: The Study used, which determines naming scheme.
strokes_loc: Folder location for where the stroke files are.
ground_truth_loc: Folder location for where the ground truth files are.
output_loc: Folder location to save the dilated files in.
'''
num_errors = 0
i = 0
files = os.listdir(strokes_loc)
for file in files:
# Extract data from file name
if study == Study.Rau:
file_re = re.search('(\d+)-(\d+)', file)
else:
file_re = re.search('(\d+)-(\d+)-(\d+)', file)
if file_re == None: continue

participant_id = file_re.group(1)
file_id = file_re.group(2)
if study == Study.Yuanxia: time_pressure = file_re.group(3)

i += 1
print('\rProcessing file', i, 'of', len(files), end='')
sys.stdout.flush()

background_label = 149
foreground_label = 29
input_image = strokes_loc + '/' + file
ground_truth_image = ground_truth_loc + '/' + file_id + '-GT.png'

num_errors += int(check_no_error_dilate(input_image, ground_truth_image, foreground_label, background_label))

print('\nFound', num_errors, 'files with errors in', i, 'files total')

print('Processing Rau\'s strokes')
error_count(Study.Rau, './rau/strokes', './rau/ground_truth')

print('\nProcessing Rau\'s points')
error_count(Study.Rau, './rau/points', './rau/ground_truth')

print('\nProcessing Yuanxia\'s points')
error_count(Study.Yuanxia, './yuanxia/points', './yuanxia/ground_truth')
print()
26 changes: 24 additions & 2 deletions dilate.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,6 @@ def no_error_dilate(input_image, ground_truth_image, output_image, dilation_radi
'place' (foreground pixels won't be dilated if they're in the background
and vice versa).
All non-zero labels will be dilated.
input_image: The label image to dilate.
ground_truth_image: The binary ground truth image.
output_image: Where to save the result.
Expand Down Expand Up @@ -65,7 +63,31 @@ def no_error_dilate(input_image, ground_truth_image, output_image, dilation_radi
it.iternext()

Image.fromarray(output).save(output_image)

def check_no_error_dilate(input_image, ground_truth_image, foreground_label = 255, background_label = 128):
'''
Returns true if no_error_dilate would act differently than textbook_dilation.
input_image: The label image to dilate.
ground_truth_image: The binary ground truth image.
'''
image = scipy.ndimage.imread(input_image)
ground_truth = np.greater(scipy.ndimage.imread(ground_truth_image), 0)

# Iterate over all pixels for dilation
it = np.nditer(image, flags=['multi_index'])
while not it.finished:
pixel = int(it[0])

if pixel == foreground_label and not ground_truth[it.multi_index]:
return True
elif pixel == background_label and ground_truth[it.multi_index]:
return True

it.iternext()

return False

def textbook_dilation(input_image, output_image, dilation_radius):
'''
Dilates all pixels a given radius (which excludes the pixel in question, so
Expand Down

0 comments on commit 65b4a0c

Please sign in to comment.