Skip to content

Commit

Permalink
Added unit tests for 'TSE_Compiled' package modules. Closes #98.
Browse files Browse the repository at this point in the history
  • Loading branch information
cgddrd committed Apr 5, 2015
1 parent 74e6608 commit 912c2ca
Show file tree
Hide file tree
Showing 7 changed files with 8,007 additions and 38 deletions.
2 changes: 1 addition & 1 deletion src/template_matching_scaling/run_travisci_proj_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ python setup.py build_ext --inplace
python setup.py install

# Run the automated unti tests with coverage.
nosetests -w tests/ --with-coverage --cover-package=tse --cover-erase
nosetests -w tests/ --with-coverage --cover-package=tse --cover-package=tse_compiled --cover-erase
4 changes: 2 additions & 2 deletions src/template_matching_scaling/template_matching_scaling.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ def scan_search_window(self, template_patch, template_patch_origin, match_method
match_method.match_id)

elif match_method.match_type == tse_match_methods.HIST:
score = TSEImageUtils.calc_compare_histogram(template_patch, current_window, match_method.match_id)
score = TSEImageUtils.calc_compare_hsv_histogram(template_patch, current_window, match_method.match_id)

# If lower score means better match, then the method is a 'reverse' method.
if match_method.reverse_score:
Expand Down Expand Up @@ -190,7 +190,7 @@ def scan_search_window_scaling(self, template_patch, template_patch_origin, matc
elif match_method.match_type == tse_match_methods.HIST:
scaled_template_patch = TSEImageUtils.scale_image_interpolation_auto(template_patch, scaled_search_window)

score = TSEImageUtils.calc_compare_histogram(scaled_template_patch, scaled_search_window, match_method.match_id)
score = TSEImageUtils.calc_compare_hsv_histogram(scaled_template_patch, scaled_search_window, match_method.match_id)

# If lower score means better match, then the method is a 'reverse' method.
if match_method.reverse_score:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
from unittest import TestCase
from nose.tools import *

from tse_compiled.tse_c_imageutils import TSECImageUtils
from tse.tse_geometry import TSEGeometry
from tse.tse_utils import TSEUtils

import numpy as np

__author__ = 'connorgoddard'


class TestTSECImageUtils(TestCase):
def test_calc_ssd_slow(self):
# Create a sample test image that is empty.
original_image = np.full((400, 400, 3), [0, 200, 0], dtype=np.uint8)

# Calculate the scale factor (MAKING SURE TO SUBTRACT '1' from the max height/width to account for array index out of bounds issue)
scale_factor_width = TSEGeometry.calc_measure_scale_factor(200, (400 - 1))

# Calculate the scaled indices to identify the pixels in the larger image that we will want to make GREEN to provide evidence for the test succeeding.
original_image_scaled_indices = np.rint((np.arange(0, 200) * scale_factor_width)).astype(int)

rows_cols_cartesian_product = np.hsplit(TSEUtils.calc_cartesian_product([original_image_scaled_indices, original_image_scaled_indices]), 2)

rows_to_extract = rows_cols_cartesian_product[0].astype(int)
cols_to_extract = rows_cols_cartesian_product[1].astype(int)

# We now want to set each fo the pixels THAT WE EXPECT TO BE EXTRACTED BY THE TEST to GREEN to show that the test has passed.
original_image[rows_to_extract, cols_to_extract] = [0, 200, 0]

# Once we have performed the pixel extraction, we expect that all of the pixels returned will be GREEN (based ont he setup above)
matching_image = np.full((200, 200, 3), [0, 200, 0], dtype=np.uint8)

non_matching_image = np.full((200, 200, 3), [200, 0, 0], dtype=np.uint8)

# Check that for perfectly matching images, we get a score of exactly 0.
assert_equal(TSECImageUtils.calc_ssd_slow(matching_image, original_image, matching_image.shape[0], matching_image.shape[1], scale_factor_width, scale_factor_width), 0)

# Check that for non-matching images, we get a score > 0.
assert_true(TSECImageUtils.calc_ssd_slow(non_matching_image, original_image, matching_image.shape[0], matching_image.shape[1], scale_factor_width, scale_factor_width) > 0)

def test_extract_rows_cols_pixels_image(self):
required_rows = np.array([1, 100]).astype(float)
required_cols = np.array([10, 20]).astype(float)

image_target_shape = np.zeros((200, 200, 3), dtype=np.uint8)

image_target_shape[1, 10] = [0, 0, 200]
image_target_shape[1, 20] = [0, 0, 200]

image_target_shape[100, 10] = [0, 0, 200]
image_target_shape[100, 20] = [0, 0, 200]

returned_image = TSECImageUtils.extract_rows_cols_pixels_image(required_rows, required_cols, image_target_shape)

# '.all()' loops through every element in 'returned_image' and checks that they equal '[0, 0, 200]'
assert_true((returned_image == [0, 0, 200]).all())

def test_calc_cartesian_product(self):

# NOTE: As we are creating these arrays MANUALLY for the test, we have to cast them to NUMPY ARRAYS first.
test_data1 = np.array([1, 2])
test_data2 = np.array([3, 4, 5])

test_data3 = np.array([10, 20, 30])
test_data4 = np.array([40, 50])

expected_result = [[1, 3], [1, 4], [1, 5], [2, 3], [2, 4], [2, 5]]
expected_result2 = [[10, 40], [10, 50], [20, 40], [20, 50], [30, 40], [30, 50]]

assert_true(np.array_equal(TSECImageUtils.calc_cartesian_product([test_data1, test_data2]), expected_result))
assert_true(np.array_equal(TSECImageUtils.calc_cartesian_product([test_data3, test_data4]), expected_result2))

def test_reshape_match_images(self):

image_target_shape = np.zeros((200, 200, 3), dtype=np.uint8)

image_current_shape = np.zeros((120000, 1, 1), dtype=np.uint8)

assert_false(np.array_equal(image_target_shape, image_current_shape))

image_reshaped = TSECImageUtils.reshape_match_images(image_current_shape, image_target_shape)

assert_true(np.array_equal(image_target_shape, image_reshaped))

def test_reshape_match_images_same_shape(self):

image_target_shape = np.zeros((200, 200, 3), dtype=np.uint8)

image_current_shape = np.zeros((200, 200, 3), dtype=np.uint8)

image_reshaped = TSECImageUtils.reshape_match_images(image_current_shape, image_target_shape)

assert_true(np.array_equal(image_target_shape, image_reshaped))

0 comments on commit 912c2ca

Please sign in to comment.