Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug/#111 #120

Merged
merged 2 commits into from
Apr 17, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
67,397 changes: 0 additions & 67,397 deletions src/template_matching_scaling/notebooks/cache_flat.pkl

This file was deleted.

67,397 changes: 0 additions & 67,397 deletions src/template_matching_scaling/notebooks/cache_wiltshire_inside_10cm.pkl

This file was deleted.

56,174 changes: 0 additions & 56,174 deletions src/template_matching_scaling/notebooks/cache_wiltshire_inside_15cm.pkl

This file was deleted.

33,728 changes: 0 additions & 33,728 deletions src/template_matching_scaling/notebooks/cache_wiltshire_inside_15cm_obstacle.pkl

This file was deleted.

112,289 changes: 0 additions & 112,289 deletions src/template_matching_scaling/notebooks/cache_wiltshire_outside.pkl

This file was deleted.

11,428 changes: 17 additions & 11,411 deletions src/template_matching_scaling/notebooks/template_match_scaled.ipynb

Large diffs are not rendered by default.

Large diffs are not rendered by default.

70 changes: 21 additions & 49 deletions src/template_matching_scaling/template_matching_scaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import math
import matplotlib.pyplot as plt
import argparse
import numpy as np

from tse.tse_fileio import TSEFileIO
from tse.tse_datautils import TSEDataUtils
Expand All @@ -18,8 +17,6 @@
from collections import OrderedDict
from pprint import pprint

import json

__author__ = 'connorgoddard'


Expand Down Expand Up @@ -49,7 +46,7 @@ def load_calibration_data(self, file_path):
raw_data = TSEFileIO.read_file(file_path, split_delimiter=",", start_position=1)
return dict(TSEDataUtils.string_2d_list_to_int_2d_list(raw_data))

def search_image(self, patch_height, match_method, use_scaling=False, force_cont_search=False, plot_results=False):
def search_image(self, patch_height, match_method, use_scaling=False, exhaustive_search=False, plot_results=False):

run_results = []

Expand All @@ -72,11 +69,14 @@ def search_image(self, patch_height, match_method, use_scaling=False, force_cont

if use_scaling is True:

run_results.append(TSEResult(i, self.scan_search_window_scaling(template_patch, template_patch_origin_point, match_method, force_cont_search)))
run_results.append(TSEResult(i, self.scan_search_window_scaling(template_patch,
template_patch_origin_point,
match_method, exhaustive_search)))

else:

run_results.append(TSEResult(i, self.scan_search_window(template_patch, template_patch_origin_point, match_method, force_cont_search)))
run_results.append(TSEResult(i, self.scan_search_window(template_patch, template_patch_origin_point,
match_method, exhaustive_search)))

if plot_results:
# self._plot_axis.set_xlabel('Row Number (px)')
Expand All @@ -86,7 +86,7 @@ def search_image(self, patch_height, match_method, use_scaling=False, force_cont

return run_results

def scan_search_window(self, template_patch, template_patch_origin, match_method, force_cont_search=False):
def scan_search_window(self, template_patch, template_patch_origin, match_method, exhaustive_search=False):

image_height, image_width = self._hsv_img2.shape[:2]

Expand Down Expand Up @@ -135,13 +135,13 @@ def scan_search_window(self, template_patch, template_patch_origin, match_method
else:
stop = True

if (force_cont_search is False) and (stop is True):
if (exhaustive_search is False) and (stop is True):
break

# We need to return the 'Y' with the best score (i.e. the displacement)
return best_position

def scan_search_window_scaling(self, template_patch, template_patch_origin, match_method, force_cont_search=False):
def scan_search_window_scaling(self, template_patch, template_patch_origin, match_method, exhaustive_search=False):

image_height, image_width = self._hsv_img2.shape[:2]

Expand Down Expand Up @@ -219,7 +219,7 @@ def scan_search_window_scaling(self, template_patch, template_patch_origin, matc
else:
stop = True

if (force_cont_search is False) and (stop is True):
if (exhaustive_search is False) and (stop is True):
break

# We need to return the 'Y' with the best score (i.e. the displacement)
Expand All @@ -236,7 +236,7 @@ def plot_results(self, results, match_method):
x.append(val.row)
y.append(val.displacement)

y_moving_average = TSEDataUtils.calc_moving_average_array(y, 10)
y_moving_average = TSEDataUtils.calc_moving_average(y, 10)

self.plot(x, y, "{0}.".format(plot_format_color), 100, match_method.match_name)
self.plot(x[len(x) - len(y_moving_average):], y_moving_average, "{0}-".format(plot_format_color), 100, "MVAV_{0}".format(match_method.match_name))
Expand All @@ -253,7 +253,8 @@ def plot(self, data_x, data_y, plot_format, max_boundary_offset, plot_name):
self._plot_axis.legend(loc='upper left', shadow=True)


def start_tests(image_pairs, patch_sizes, match_methods, config_file, use_scaling=False, force_cont_search=False, plot_results=False):
def start_tests(image_pairs, patch_sizes, match_methods, config_file, use_scaling=False, exhaustive_search=False,
plot_results=False):

# Create a dictionary to store the results of all image pairs -> patch sizes -> match methods for a given pair of images.
image_dict = {}
Expand All @@ -271,7 +272,8 @@ def start_tests(image_pairs, patch_sizes, match_methods, config_file, use_scalin
match_dict = {}

for match_method in match_methods:
match_dict[match_method.match_name] = match.search_image(patch_size, match_method, use_scaling, force_cont_search, plot_results)
match_dict[match_method.match_name] = match.search_image(patch_size, match_method, use_scaling,
exhaustive_search, plot_results)

patch_dict[patch_size] = match_dict

Expand Down Expand Up @@ -317,7 +319,8 @@ def start_tests(image_pairs, patch_sizes, match_methods, config_file, use_scalin
for match_method in match_methods:

# Store the results for a given match method.
match_dict[match_method.match_name] = match.search_image(patch_size, match_method, use_scaling, force_cont_search, plot_results)
match_dict[match_method.match_name] = match.search_image(patch_size, match_method, use_scaling,
exhaustive_search, plot_results)

if column_count == (column_max - 1):
row_count += 1
Expand Down Expand Up @@ -364,7 +367,7 @@ def main():
parser.add_argument('-m', '--methods', nargs='+', dest="match_methods", type=str, required=True)
parser.add_argument('-s', '--scaling', dest='scaling', action='store_true')
parser.add_argument('-d', '--drawplot', dest='plot_results', action='store_true')
parser.add_argument('-f', '--forcecontsearch', dest='force_cont_search', action='store_true')
parser.add_argument('-es', '--exhaustivesearch', dest='exhaustive_search', action='store_true')

args = vars(parser.parse_args())

Expand All @@ -389,43 +392,12 @@ def main():
parser.error("Error: \"{0}\" is not a valid matching method option.\nSupported Methods: \'DistanceEuclidean\', \'DistanceCorr\', \'HistCorrel\', \'HistChiSqr\' ".format(method))

# Start the tests using settings passed in as command-line arguments.
results_dict = start_tests(args['image_pairs'], list(OrderedDict.fromkeys(args['patch_sizes'])), match_methods, args['calib_file'], use_scaling=args['scaling'], force_cont_search=args['force_cont_search'], plot_results=args['plot_results'])

pprint(results_dict)

results_pair1_100 = results_dict['IMG1.JPG_IMG2.JPG'][100]

raw_results_pair1_100 = []
image_rows = []

for key in results_pair1_100:
raw_results_pair1_100.append([o.displacement for o in results_pair1_100[key]])
image_rows = [o.row for o in results_pair1_100[key]]

print raw_results_pair1_100

averaged_results_pair1_100 = TSEDataUtils.calc_element_wise_average(raw_results_pair1_100)

print averaged_results_pair1_100

print len(raw_results_pair1_100[0])
print len(averaged_results_pair1_100)

# filtered_results_pair1_100 = TSEDataUtils.filter_outliers_ab_dist_median(averaged_results_pair1_100)
#
# image_rows = np.array(image_rows)[TSEDataUtils.filter_outliers_ab_dist_median_indices(averaged_results_pair1_100)]
#
# plt.plot(image_rows, np.array(filtered_results_pair1_100), "b.")
#
# y_moving_average = TSEDataUtils.calc_moving_average_array(np.array(filtered_results_pair1_100), 20)
#
# plt.plot(image_rows[len(image_rows) - len(y_moving_average):], y_moving_average, "g-")
#
# plt.show()
results_dict = start_tests(args['image_pairs'], list(OrderedDict.fromkeys(args['patch_sizes'])), match_methods,
args['calib_file'], use_scaling=args['scaling'],
exhaustive_search=args['exhaustive_search'], plot_results=args['plot_results'])

if args['plot_results']:
plt.show()


if __name__ == '__main__':
main()
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ def test_calc_moving_average_array(self):
expected_result = np.array([float((2+1) / 2.0), float((3+2)/2.0), float((4+3)/2.0), float((5+4)/2.0), float((6+5)/2.0)])

# Using both Numpy test asserts and nosetest asserts.
np.testing.assert_equal(TSEDataUtils.calc_moving_average_array(test_data, points_to_average), expected_result)
assert_true(np.array_equal(TSEDataUtils.calc_moving_average_array(test_data, points_to_average), expected_result))
np.testing.assert_equal(TSEDataUtils.calc_moving_average(test_data, points_to_average), expected_result)
assert_true(np.array_equal(TSEDataUtils.calc_moving_average(test_data, points_to_average), expected_result))

def test_convert_array_to_numpy_array(self):
test_data = [1, 2, 3, 4, 5, 6]
Expand Down
14 changes: 12 additions & 2 deletions src/template_matching_scaling/tse/tse_datautils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,21 @@ def convert_list_to_int(list_to_convert):
return map(int, list_to_convert)

@staticmethod
def calc_moving_average_array(values, window, mode='valid'):
def calc_centered_moving_average(values, window):

# Using 'same' mode for the 'numpy.convolve' function will centre the convolution window at each point of the array overlap.
# WARNING: Boundary effects will be observed at the ends as a result of the arrays not fully overlapping.
return TSEDataUtils.calc_moving_average(values, window, 'same')


@staticmethod
def calc_moving_average(values, window, mode='valid'):

weights = np.repeat(1.0, window)/window

# Including 'valid' MODE will REQUIRE there to be enough data points.
# Including 'valid' MODE will REQUIRE there to be enough data points before beginning convolution.
# 'valid' mode only convolve where points overlap exactly. This is equivalent to a Simple Moving Average.
# See: http://docs.scipy.org/doc/numpy/reference/generated/numpy.convolve.html
return np.convolve(values, weights, mode)

@staticmethod
Expand Down