mirror of
https://github.com/balajsra/EECS-442-WN19-DIC-Project.git
synced 2025-09-03 01:13:15 +00:00
Working on gradient to calculate strain
In process of figuring out how to get strain calculation with gradient.
This commit is contained in:
216
src/__main__.py
216
src/__main__.py
@@ -3,200 +3,88 @@ import cv2
|
|||||||
from matplotlib import pyplot as plt
|
from matplotlib import pyplot as plt
|
||||||
import os
|
import os
|
||||||
import file_data
|
import file_data
|
||||||
from image_data import ImageData
|
import image_data
|
||||||
|
|
||||||
|
|
||||||
def main():
|
|
||||||
# Read in all images
|
|
||||||
images = read_images()
|
|
||||||
|
|
||||||
# Read in data from Section001_Data.txt
|
|
||||||
specimen, load_disp_data = file_data.read_file("../Section001_Data.txt")
|
|
||||||
|
|
||||||
# Keep track of Stress and Strains
|
|
||||||
stresses = []
|
|
||||||
strains = []
|
|
||||||
|
|
||||||
# Get distances using sift
|
|
||||||
distances = get_sift_distance(images[8], images[9])
|
|
||||||
|
|
||||||
# These distances are coming out as zero for some reason.
|
|
||||||
# Still trying to figure out if it's a bug in the code I
|
|
||||||
# wrote, or if SIFT won't work for our case.
|
|
||||||
print("DISTANCES:")
|
|
||||||
print(distances)
|
|
||||||
print("MAX DISTANCE:")
|
|
||||||
print(max(distances))
|
|
||||||
print("MIN DISTANCE:")
|
|
||||||
print(min(distances))
|
|
||||||
|
|
||||||
# Eventually we'll find the distances, stress, strain for all images
|
|
||||||
"""
|
|
||||||
for idx in range(0, len(images)-1):
|
|
||||||
distances = getSiftDistance(images[idx], images[idx+1])
|
|
||||||
strains.append(getStrain(specimen.ol, load_disp_data[idx].disp))
|
|
||||||
stresses.append(load_disp_data[idx].stress)
|
|
||||||
youngs_mod = getYoungsModulus(strains[idx] / stress[idx])
|
|
||||||
"""
|
|
||||||
|
|
||||||
|
|
||||||
def read_images():
|
def read_images():
|
||||||
image_dir = '../Images/'
|
image_dir = '../Images/'
|
||||||
filenames = os.listdir(image_dir)
|
filenames = os.listdir(image_dir)
|
||||||
|
|
||||||
images = []
|
images = []
|
||||||
|
|
||||||
images.append(None)
|
images.append(None)
|
||||||
|
|
||||||
for file in filenames:
|
for file in filenames:
|
||||||
images.append(cv2.imread(os.path.join(image_dir, file), 0))
|
images.append(cv2.imread(os.path.join(image_dir, file), 0))
|
||||||
|
|
||||||
return images
|
return images
|
||||||
|
|
||||||
|
|
||||||
def get_strain(length, displacement):
|
|
||||||
return displacement / length
|
|
||||||
|
|
||||||
|
|
||||||
def get_youngs_modulus(strain, stress):
|
|
||||||
return stress / strain
|
|
||||||
|
|
||||||
|
|
||||||
def get_sift_distance(img1, img2):
|
|
||||||
""" Gets distance between matching pts in
|
|
||||||
img1 and img2.
|
|
||||||
"""
|
|
||||||
sift = cv2.xfeatures2d.SIFT_create()
|
|
||||||
original_kp, original_des = sift.detectAndCompute(img1, None)
|
|
||||||
new_kp, new_des = sift.detectAndCompute(img2, None)
|
|
||||||
|
|
||||||
bf = cv2.BFMatcher()
|
|
||||||
matches = bf.knnMatch(original_des, new_des, k=2)
|
|
||||||
|
|
||||||
# Ratio test
|
|
||||||
good = []
|
|
||||||
|
|
||||||
for m, n in matches:
|
|
||||||
if m.distance < 0.3 * n.distance:
|
|
||||||
good.append(m)
|
|
||||||
|
|
||||||
# Draw matches
|
|
||||||
"""
|
|
||||||
# Uncomment to print matches between img1 and img2.
|
|
||||||
# SIFT may not be the best method based off the matched
|
|
||||||
# images.
|
|
||||||
matches = cv2.drawMatchesKnn(img1, original_kp, img2, new_kp, good, None, flags=2)
|
|
||||||
plt.imshow(matches)
|
|
||||||
plt.show()
|
|
||||||
"""
|
|
||||||
|
|
||||||
# Featured matched keypoints from images 1 and 2
|
|
||||||
pts1 = np.float32([original_kp[m.queryIdx].pt for m in good])
|
|
||||||
pts2 = np.float32([new_kp[m.trainIdx].pt for m in good])
|
|
||||||
|
|
||||||
# convert to complex number
|
|
||||||
z1 = np.array([[complex(c[0],c[1]) for c in pts1]])
|
|
||||||
z2 = np.array([[complex(c[0],c[1]) for c in pts2]])
|
|
||||||
|
|
||||||
# Distance between featured matched keypoints
|
|
||||||
distances = abs(z2 - z1)
|
|
||||||
|
|
||||||
return distances
|
|
||||||
|
|
||||||
|
|
||||||
def find_displacement(match_method):
|
def find_displacement(match_method):
|
||||||
images = read_images()
|
images = read_images()
|
||||||
|
|
||||||
specimen, load_disp_data = file_data.read_file("../Section001_Data.txt")
|
specimen, load_disp_data = file_data.read_file("../Section001_Data.txt")
|
||||||
|
|
||||||
plt.figure(1)
|
plt.figure(1)
|
||||||
|
|
||||||
reference = images[560]
|
reference = images[8]
|
||||||
compare_img = images[561]
|
compare_img = images[96]
|
||||||
|
|
||||||
plt.imshow(reference, cmap="gray", vmin=0, vmax=255)
|
plt.imshow(reference, cmap="gray", vmin=0, vmax=255)
|
||||||
|
|
||||||
subset_size = 5
|
subset_size = 5
|
||||||
subset_spacing = 30
|
subset_spacing = 20
|
||||||
search_size = 3
|
search_size = 5
|
||||||
|
|
||||||
x_range = range(650, 2080, subset_spacing)
|
x_range = range(650, 2080, subset_spacing)
|
||||||
y_range = range(120, 500, subset_spacing)
|
y_range = range(120, 500, subset_spacing)
|
||||||
|
|
||||||
im_data = ImageData(len(y_range), len(x_range))
|
im_data = ImageData(len(y_range), len(x_range))
|
||||||
|
|
||||||
for i in range(len(x_range)):
|
disp_mag = np.zeros((len(y_range), len(x_range)))
|
||||||
for j in range(len(y_range)):
|
|
||||||
x = x_range[i]
|
|
||||||
y = y_range[j]
|
|
||||||
|
|
||||||
x_displacements = np.zeros((round(380/subset_spacing), round(1430/subset_spacing)))
|
for i in range(0, len(y_range)):
|
||||||
y_displacements = np.zeros((round(380/subset_spacing), round(1430/subset_spacing)))
|
for j in range(0, len(x_range)):
|
||||||
x_disp_idx = 0
|
x = x_range[j]
|
||||||
y_disp_idx = 0
|
y = y_range[i]
|
||||||
|
|
||||||
for x in range(650, 2080, subset_spacing):
|
im_data.location[i, j, :] = np.array([x, y])
|
||||||
y_disp_idx = 0
|
|
||||||
print(x_disp_idx)
|
|
||||||
for y in range(120, 500, subset_spacing):
|
|
||||||
up_bound = (subset_size + 1) // 2
|
|
||||||
low_bound = subset_size // 2
|
|
||||||
|
|
||||||
subset = reference[y-low_bound:y+up_bound, x-low_bound:x+up_bound]
|
up_bound = (subset_size + 1) // 2
|
||||||
search = compare_img[y-search_size:y+search_size+1, x-search_size:x+search_size+1]
|
low_bound = subset_size // 2
|
||||||
|
|
||||||
res = cv2.matchTemplate(image=search, templ=subset, method=match_method)
|
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]
|
||||||
|
|
||||||
minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(src=res)
|
res = cv2.matchTemplate(image=search, templ=subset, method=match_method)
|
||||||
|
|
||||||
dx = None
|
minVal, maxVal, minLoc, maxLoc = cv2.minMaxLoc(src=res)
|
||||||
dy = None
|
|
||||||
|
|
||||||
if match_method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
|
dx = None
|
||||||
dx = (minLoc[0] + (subset_size // 2)) - search_size
|
dy = None
|
||||||
dy = (minLoc[1] + (subset_size // 2)) - search_size
|
|
||||||
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
|
|
||||||
|
|
||||||
im_data.dx[j, i] = dx
|
if match_method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED]:
|
||||||
im_data.dy[j, i] = dy
|
dx = (minLoc[0] + (subset_size // 2)) - search_size
|
||||||
im_data.disp_mag[j, i] = np.sqrt((dx ** 2) + (dy ** 2))
|
dy = (minLoc[1] + (subset_size // 2)) - search_size
|
||||||
plt.arrow(x=x, y=y, dx=dx, dy=dy, color="yellow", length_includes_head=True, shape="full")
|
elif match_method in [cv2.TM_CCORR, cv2.TM_CCORR_NORMED,
|
||||||
x_displacements[y_disp_idx, x_disp_idx] = dx
|
cv2.TM_CCOEFF, cv2.TM_CCOEFF_NORMED]:
|
||||||
y_displacements[y_disp_idx, x_disp_idx] = dy
|
dx = (maxLoc[0] + (subset_size // 2)) - search_size
|
||||||
y_disp_idx += 1
|
dy = (maxLoc[1] + (subset_size // 2)) - search_size
|
||||||
x_disp_idx += 1
|
|
||||||
|
|
||||||
plt.quiver(x_range, y_range, im_data.dx, im_data.dy, im_data.disp_mag, cmap=plt.cm.jet)
|
im_data.displacement[i, j, :] = np.array([dx, dy])
|
||||||
|
disp_mag[i, j] = np.sqrt((dx ** 2) + (dy ** 2))
|
||||||
|
|
||||||
x_average = findAverageDisplacement(x_displacements, 30, 42, 0, 19)
|
plt.quiver(X=x_range, Y=y_range,
|
||||||
y_average = findAverageDisplacement(y_displacements, 30, 42, 0, 19)
|
U=im_data.displacement[:, :, 0], V=im_data.displacement[:, :, 1],
|
||||||
print("X DISPLACEMENT AVERAGE: ", x_average)
|
C=im_data.disp_mag, cmap=plt.cm.jet)
|
||||||
print("Y DISPLACEMENT AVERAGE: ", y_average)
|
|
||||||
print("X MAX: ", np.amax(x_displacements))
|
|
||||||
print("Y MAX: ", np.amax(y_displacements))
|
|
||||||
#useless comment here
|
|
||||||
plt.show()
|
|
||||||
|
|
||||||
def findAverageDisplacement(displacement_field, x1, x2, y1, y2):
|
|
||||||
""" x1,x2,y1,y2 defines the window
|
|
||||||
that we want to find the average
|
|
||||||
for. Currently using magnitudes,
|
|
||||||
don't care about signs.
|
|
||||||
"""
|
|
||||||
absolute = np.absolute(displacement_field[y1:y2, x1:x2])
|
|
||||||
return np.average(absolute)
|
|
||||||
|
|
||||||
|
plt.show()
|
||||||
|
|
||||||
|
|
||||||
if __name__ == '__main__':
|
if __name__ == '__main__':
|
||||||
# main()
|
find_displacement(cv2.TM_SQDIFF_NORMED)
|
||||||
|
|
||||||
find_displacement(cv2.TM_SQDIFF_NORMED)
|
# for match_method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED,
|
||||||
|
# cv2.TM_CCORR, cv2.TM_CCORR_NORMED,
|
||||||
# for match_method in [cv2.TM_SQDIFF, cv2.TM_SQDIFF_NORMED,
|
# cv2.TM_CCOEFF, cv2.TM_CCOEFF_NORMED]:
|
||||||
# cv2.TM_CCORR, cv2.TM_CCORR_NORMED,
|
# find_displacement(match_method)
|
||||||
# cv2.TM_CCOEFF, cv2.TM_CCOEFF_NORMED]:
|
|
||||||
# find_displacement(match_method)
|
|
||||||
|
@@ -11,23 +11,13 @@ import numpy as np
|
|||||||
|
|
||||||
|
|
||||||
class ImageData:
|
class ImageData:
|
||||||
# Displacement Data
|
location = None
|
||||||
dx = None
|
displacement = None
|
||||||
dy = None
|
strain = None
|
||||||
disp_mag = None
|
|
||||||
|
|
||||||
# Strain Data
|
|
||||||
eps_x = None
|
|
||||||
eps_y = None
|
|
||||||
eps_mag = None
|
|
||||||
|
|
||||||
def __init__(self, num_rows, num_cols):
|
def __init__(self, num_rows, num_cols):
|
||||||
matrix_shape = (num_rows, num_cols)
|
matrix_shape = (num_rows, num_cols, 2)
|
||||||
|
|
||||||
self.dx = np.zeros(matrix_shape)
|
self.location = np.zeros(matrix_shape)
|
||||||
self.dy = np.zeros(matrix_shape)
|
self.displacement = np.zeros(matrix_shape)
|
||||||
self.disp_mag = np.zeros(matrix_shape)
|
self.strain = np.zeros(matrix_shape)
|
||||||
|
|
||||||
self.eps_x = np.zeros(matrix_shape)
|
|
||||||
self.eps_y = np.zeros(matrix_shape)
|
|
||||||
self.eps_mag = np.zeros(matrix_shape)
|
|
||||||
|
Reference in New Issue
Block a user