diff --git a/Plots/Matching Method Comparison/CCOEFF.png b/Plots/Matching Method Comparison/CCOEFF.png deleted file mode 100644 index 0d336f5..0000000 Binary files a/Plots/Matching Method Comparison/CCOEFF.png and /dev/null differ diff --git a/Plots/Matching Method Comparison/CCOEFF_NORMED.png b/Plots/Matching Method Comparison/CCOEFF_NORMED.png deleted file mode 100644 index c472562..0000000 Binary files a/Plots/Matching Method Comparison/CCOEFF_NORMED.png and /dev/null differ diff --git a/Plots/Matching Method Comparison/CCORR.png b/Plots/Matching Method Comparison/CCORR.png deleted file mode 100644 index b069868..0000000 Binary files a/Plots/Matching Method Comparison/CCORR.png and /dev/null differ diff --git a/Plots/Matching Method Comparison/CCORR_NORMED.png b/Plots/Matching Method Comparison/CCORR_NORMED.png deleted file mode 100644 index 143acd4..0000000 Binary files a/Plots/Matching Method Comparison/CCORR_NORMED.png and /dev/null differ diff --git a/Plots/Matching Method Comparison/Matching_Method_Comparison_560-561.png b/Plots/Matching Method Comparison/Matching_Method_Comparison_560-561.png new file mode 100644 index 0000000..258a827 Binary files /dev/null and b/Plots/Matching Method Comparison/Matching_Method_Comparison_560-561.png differ diff --git a/Plots/Matching Method Comparison/Matching_Method_Comparison_8-9.png b/Plots/Matching Method Comparison/Matching_Method_Comparison_8-9.png new file mode 100644 index 0000000..eeaf67a Binary files /dev/null and b/Plots/Matching Method Comparison/Matching_Method_Comparison_8-9.png differ diff --git a/Plots/Matching Method Comparison/SQDIFF.png b/Plots/Matching Method Comparison/SQDIFF.png deleted file mode 100644 index 98f7a39..0000000 Binary files a/Plots/Matching Method Comparison/SQDIFF.png and /dev/null differ diff --git a/Plots/Matching Method Comparison/SQDIFF_NORMED.png b/Plots/Matching Method Comparison/SQDIFF_NORMED.png deleted file mode 100644 index c96b853..0000000 Binary files a/Plots/Matching Method Comparison/SQDIFF_NORMED.png and /dev/null differ diff --git a/src/__main__.py b/src/__main__.py index d56ec89..f7fa444 100644 --- a/src/__main__.py +++ b/src/__main__.py @@ -20,24 +20,18 @@ def read_images(): return images -def find_displacement(match_method): - images = read_images() +def find_displacement(images, ref_index, comp_index, match_method): + reference = images[ref_index] + compare_img = images[comp_index] - specimen, load_disp_data = file_data.read_file("../Section001_Data.txt") + ref_template_size = (5, 5) + search_window_size = (5, 3) + grid_spacing = 10 - reference = images[8] - compare_img = images[9] + x_range = range(200, 2200, grid_spacing) + y_range = range(100, 500, grid_spacing) - subset_size = 5 - subset_spacing = 10 - search_size = 5 - - x_range = range(650, 2080, subset_spacing) - y_range = range(120, 500, subset_spacing) - - im_data = image_data.ImageData(len(y_range), len(x_range)) - - disp_mag = np.zeros((len(y_range), len(x_range))) + im_data = image_data.ImageData(len(y_range), len(x_range), ref_index, comp_index) for i in range(len(y_range)): for j in range(len(x_range)): @@ -46,90 +40,156 @@ def find_displacement(match_method): im_data.location[i, j, :] = np.array([x, y]) - up_bound = (subset_size + 1) // 2 - low_bound = subset_size // 2 + template_x = [x - (ref_template_size[0] // 2), x + ((ref_template_size[0] + 1) // 2)] + template_y = [y - (ref_template_size[1] // 2), y + ((ref_template_size[1] + 1) // 2)] + search_x = [template_x[0] - search_window_size[0], template_x[1] + search_window_size[0]] + search_y = [template_y[0] - search_window_size[1], template_y[1] + search_window_size[1]] - subset = reference[y-low_bound:y+up_bound, x-low_bound:x+up_bound] - search = compare_img[y-search_size:y+search_size+1, x-search_size:x+search_size+1] + ref_template = reference[template_y[0]:template_y[1], template_x[0]:template_x[1]] + search_window = compare_img[search_y[0]:search_y[1], search_x[0]:search_x[1]] - res = cv2.matchTemplate(image=search, templ=subset, method=match_method) + res = cv2.matchTemplate( + image=search_window, # Search window in compare image + templ=ref_template, # Template from reference image + method=match_method # Matching Method + ) - minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(src=res) + min_val, max_val, min_loc, max_loc = cv2.minMaxLoc(src=res) dx = None dy = None if match_method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]: - dx = (minLoc[0] + (subset_size // 2)) - search_size - dy = (minLoc[1] + (subset_size // 2)) - search_size + dx = min_loc[0] - search_window_size[0] + dy = min_loc[1] - search_window_size[1] elif match_method in [cv2.TM_CCORR, cv2.TM_CCORR_NORMED, cv2.TM_CCOEFF, cv2.TM_CCOEFF_NORMED]: - dx = (maxLoc[0] + (subset_size // 2)) - search_size - dy = (maxLoc[1] + (subset_size // 2)) - search_size + dx = max_loc[0] - search_window_size[0] + dy = max_loc[1] - search_window_size[1] im_data.displacement[i, j, :] = np.array([dx, dy]) - disp_mag[i, j] = np.sqrt((dx ** 2) + (dy ** 2)) # Strain in x direction im_data.strain[:, :, 0] = np.gradient( im_data.displacement[:, :, 0], # x displacements im_data.location[:, 0, 1], # y (row) positions - im_data.location[0, :, 0])[1] # x (col) positions + im_data.location[0, :, 0] # x (col) positions + )[1] # Gradient in cols direction # Strain in y direction im_data.strain[:, :, 1] = np.gradient( im_data.displacement[:, :, 1], # y displacements im_data.location[:, 0, 1], # y (row) positions - im_data.location[0, :, 0])[0] # x (col) positions + im_data.location[0, :, 0] # x (col) positions + )[0] # Gradient in rows direction - ########################## - # Figure 1: Displacement # - ########################## - plt.figure(1) - plt.subplot(2, 1, 1) + return reference, im_data - plt.imshow(reference, # Show reference image - cmap="gray", # Grayscale - vmin=0, # Minimum pixel value - vmax=255, # Maximum pixel value + +def plot_disp_and_strain(ref_img, img_data): + plt.figure() + + # Subplot 1: Displacement + ax = plt.subplot(2, 1, 1) + ax.title.set_text("Displacements between image " + str(img_data.reference_index) + + " and image " + str(img_data.comparison_index)) + + plt.imshow(ref_img, # Show reference image + cmap="gray", # Grayscale + vmin=0, # Minimum pixel value + vmax=255, # Maximum pixel value origin="lower") # Flip image so increasing row corresponds to increasing y - plt.quiver(im_data.location[:, :, 0], # x coordinates of arrow locations - im_data.location[:, :, 1], # y coordinates of arrow locations - im_data.displacement[:, :, 0], # x components of arrow vectors - im_data.displacement[:, :, 1], # y components of arrow vectors - disp_mag, # arrow color (vector magnitude) - cmap=plt.cm.jet, # color map (jet) - units="dots") # units of arrow dimensions + disp_mag = np.sqrt(img_data.displacement[:, :, 0] ** 2 + img_data.displacement[:, :, 1] ** 2) - #################### - # Figure 1: Strain # - #################### - plt.subplot(2, 1, 2) + plt.quiver(img_data.location[:, :, 0], # x coordinates of arrow locations + img_data.location[:, :, 1], # y coordinates of arrow locations + img_data.displacement[:, :, 0], # x components of arrow vectors + img_data.displacement[:, :, 1], # y components of arrow vectors + disp_mag, # arrow color (vector magnitude) + cmap=plt.cm.jet, # color map (jet) + units="dots", # units of arrow dimensions + angles="xy") # arrows point from (x, y) to (x + u, y + v) - plt.imshow(reference, # Show reference image - cmap="gray", # Grayscale - vmin=0, # Minimum pixel value - vmax=255, # Maximum pixel value + # Subplot 2: Strain + ax = plt.subplot(2, 1, 2) + ax.title.set_text("Strain between image " + str(img_data.reference_index) + + " and image " + str(img_data.comparison_index)) + + plt.imshow(ref_img, # Show reference image + cmap="gray", # Grayscale + vmin=0, # Minimum pixel value + vmax=255, # Maximum pixel value origin="lower") # Flip image so increasing row corresponds to increasing y - strain_mag = np.sqrt(im_data.strain[:, :, 0] ** 2 + im_data.strain[:, :, 1] ** 2) + strain_mag = np.sqrt(img_data.strain[:, :, 0] ** 2 + img_data.strain[:, :, 1] ** 2) - plt.quiver(im_data.location[0, :, 0], - im_data.location[:, 0, 1], - im_data.strain[:, :, 0], - im_data.strain[:, :, 1], + plt.quiver(img_data.location[0, :, 0], + img_data.location[:, 0, 1], + img_data.strain[:, :, 0], + img_data.strain[:, :, 1], strain_mag, cmap=plt.cm.jet, - units="dots") + units="dots", + angles="xy") + + plt.show() + + +def compare_matching_methods(images, ref_idx, comp_idx): + plt.figure() + plt.suptitle("Displacements between image " + str(ref_idx) + " and image " + str(comp_idx)) + + i = 1 + for match_method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED, + cv2.TM_CCORR, cv2.TM_CCORR_NORMED, + cv2.TM_CCOEFF, cv2.TM_CCOEFF_NORMED]: + reference_img, img_data = find_displacement(images, ref_idx, comp_idx, match_method) + + ax = plt.subplot(3, 2, i) + + title = None + + if match_method == cv2.TM_SQDIFF: + title = "SQDIFF" + elif match_method == cv2.TM_SQDIFF_NORMED: + title = "SQDIFF_NORMED" + elif match_method == cv2.TM_CCORR: + title = "CCORR" + elif match_method == cv2.TM_CCORR_NORMED: + title = "CCORR_NORMED" + elif match_method == cv2.TM_CCOEFF: + title = "CCOEFF" + elif match_method == cv2.TM_CCOEFF_NORMED: + title = "CCOEFF_NORMED" + + ax.title.set_text(title) + + plt.imshow(reference_img, cmap="gray", vmin=0, vmax=255, origin="lower") + + disp_mag = np.sqrt(img_data.displacement[:, :, 0] ** 2 + img_data.displacement[:, :, 1] ** 2) + + plt.quiver(img_data.location[:, :, 0], img_data.location[:, :, 1], img_data.displacement[:, :, 0], + img_data.displacement[:, :, 1], disp_mag, cmap=plt.cm.jet, units="dots", angles="xy") + + i += 1 plt.show() if __name__ == '__main__': - find_displacement(cv2.TM_SQDIFF_NORMED) + images = read_images() - # for match_method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED, - # cv2.TM_CCORR, cv2.TM_CCORR_NORMED, - # cv2.TM_CCOEFF, cv2.TM_CCOEFF_NORMED]: - # find_displacement(match_method) + specimen, load_disp_data = file_data.read_file("../Section001_Data.txt") + + compare_matching_methods(images, 560, 561) + + # Matching Methods + # cv2.TM_SQDIFF + # cv2.TM_SQDIFF_NORMED + # cv2.TM_CCORR + # cv2.TM_CCORR_NORMED + # cv2.TM_CCOEFF + # cv2.TM_CCOEFF_NORMED + reference_img, img_data = find_displacement(images, 560, 561, cv2.TM_CCORR_NORMED) + plot_disp_and_strain(reference_img, img_data) diff --git a/src/image_data.py b/src/image_data.py index f4e1a39..709734e 100644 --- a/src/image_data.py +++ b/src/image_data.py @@ -11,11 +11,16 @@ import numpy as np class ImageData: + reference_index = None + comparison_index = None location = None displacement = None strain = None - def __init__(self, num_rows, num_cols): + def __init__(self, num_rows, num_cols, ref_index, comp_index): + self.reference_index = ref_index + self.comparison_index = comp_index + matrix_shape = (num_rows, num_cols, 2) self.location = np.zeros(matrix_shape)