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(TSEDataUtils.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 search_image(self,
                     patch_height,
                     match_method,
                     use_scaling=False,
                     force_cont_search=False,
                     plot_results=False):

        run_results = []

        smallest_key = TSEDataUtils.get_smallest_key_dict(
            self._calibration_lookup)

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

        image_centre_x = math.floor(image_width / 2)

        for i in range(smallest_key + 1, image_height - patch_height):

            calibrated_patch_width = self._calibration_lookup[i]
            patch_half_width = math.floor(calibrated_patch_width / 2)

            # Create points for the current template patch origin, end and centre.
            template_patch_origin_point = TSEPoint(
                (image_centre_x - patch_half_width), i)
            template_patch_end_point = TSEPoint(
                (image_centre_x + patch_half_width), (i + patch_height))

            template_patch = self._hsv_img1[
                template_patch_origin_point.y:template_patch_end_point.y,
                template_patch_origin_point.x:template_patch_end_point.x]

            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)))

            else:

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

        if plot_results:
            # self._plot_axis.set_xlabel('Row Number (px)')
            # self._plot_axis.set_ylabel('Vertical Displacement (px)')
            # self._plot_axis.set_title('Patch: {0}px - Images: {1}, {2}'.format(patch_height, self._image_one_file_name, self._image_two_file_name))
            self.plot_results(run_results, match_method)

        return run_results
    def plot_results(self, results, match_method):

        x = []
        y = []

        plot_format_color = match_method.format_string

        for val in results:
            x.append(val.row)
            y.append(val.displacement)

        y_moving_average = TSEDataUtils.calc_moving_average_array(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))
    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 main():

    parser = argparse.ArgumentParser()

    parser.add_argument('-c',
                        '--calibfile',
                        help='Datafile containing the calibration data',
                        dest="calib_file",
                        required=True)
    parser.add_argument('-i',
                        '--images',
                        help="Images",
                        dest="image_pairs",
                        type=InputImagePairArgument,
                        nargs='+',
                        required=True)
    parser.add_argument('-p',
                        '--patches',
                        nargs='+',
                        dest="patch_sizes",
                        type=int,
                        required=True)
    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')

    args = vars(parser.parse_args())

    match_methods = []

    # OrderedDict is used to remove any duplicates.
    for method in list(OrderedDict.fromkeys(args['match_methods'])):

        if method == "DistanceEuclidean":
            match_methods.append(
                TSEMatchType("DistanceEuclidean",
                             tse_match_methods.DISTANCE_ED,
                             None,
                             "r",
                             reverse_score=True))

        elif method == "DistanceCorr":
            match_methods.append(
                TSEMatchType("DistanceCorr", tse_match_methods.DISTANCE,
                             cv2.cv.CV_TM_CCORR_NORMED, "b"))

        elif method == "HistCorrel":
            match_methods.append(
                TSEMatchType("HistCorrel", tse_match_methods.HIST,
                             cv2.cv.CV_COMP_CORREL, "b"))

        elif method == "HistChiSqr":
            match_methods.append(
                TSEMatchType("HistChiSqr",
                             tse_match_methods.HIST,
                             cv2.cv.CV_COMP_CHISQR,
                             "g",
                             reverse_score=True))

        else:
            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()

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