Skip to content
Snippets Groups Projects
Commit c86e9580 authored by Sebastian Böttger's avatar Sebastian Böttger
Browse files

Further improve of the image analyser

parent e25437b1
Branches
No related tags found
No related merge requests found
......@@ -161,11 +161,68 @@ class ImageAnalyser():
self.index_corners = (self.index_corners + shift) % self.cart_path.shape[0]
def split_sites(self):
self.sides_cart_path = np.array([[],[],[],[]],dtype=np.ndarray)
self.sides_cart_path = [None, None, None, None]
for i in range(3):
self.sides_cart_path[i] = self.cart_path[self.index_corners[i]:self.index_corners[i+1]]
self.sides_cart_path[3] = self.cart_path[self.index_corners[3]:]
def normalize_sides(self):
for i in range(4):
self.sides_cart_path[i] = self.rotate_side(self.sides_cart_path[i], (-(np.pi/2)*i))
angle = self.calculate_straight_angle(self.sides_cart_path[i])[0]
self.sides_cart_path[i] = self.rotate_side(self.sides_cart_path[i], angle)
#angle = self.calculate_straight_angle(self.sides_cart_path[i])
def calculate_straight_angle(self, side):
if side[0,0] == side[-1,0]:
return 0
points = np.zeros((1,3,2))
points[0,0] = np.array((side[0,0], side[-1,1]))
points[0,1] = side[0]
points[0,2] = side[-1]
if side[0,0] > side[-1,0]:
return self.calculate_gamma(points)
else:
return -self.calculate_gamma(points)
def rotate_side(self, side, angle):
center = side[0]
rotation_matrix = np.array([
[np.cos(angle), -np.sin(angle)],
[np.sin(angle), np.cos(angle)]
])
shifted_side = side - center
rotated_side = np.dot(shifted_side, rotation_matrix)
rotated_side += center
rotated_side[:,0] -= rotated_side[:,0].min()
rotated_side[:,1] -= rotated_side[:,1].min()
return rotated_side.copy().astype(int)
def polish_side_paths(self):
for side_index in range(len(self.sides_cart_path)):
side = self.sides_cart_path[side_index]
index = 0
while index < side.shape[0]-1:
if (np.abs(side[index] - side[index + 1]).max()) > 1:
side = np.insert(side, index+1, side[index]-((side[index]-side[index+1])//2),axis=0)
else:
index+=1
self.sides_cart_path[side_index] = side
def convert_to_binary_array(self, side):
bin_array = np.zeros(side.max(axis=0)+1, dtype=np.uint8)
bin_array[side[:,0], side[:,1]] = 1
return bin_array
#%% test functions
def print_outline(cart_path, image_shape):
image = np.zeros(image_shape)
......@@ -179,7 +236,7 @@ def print_outline(cart_path, image_shape):
cv.destroyWindow("outline sorted")
cv.imshow("outline sorted", image)
cv.waitKey(0)
cv.destroyWindow("outline sorted")
#cv.destroyWindow("outline sorted")
def show_pol_path(pol_path):
pol_path_edit = pol_path.copy()
......@@ -193,13 +250,13 @@ if __name__ == "__main__":
ia = ImageAnalyser(image_path)
cv.imshow("binary image", ia.image)
cv.waitKey(0)
cv.destroyAllWindows()
#cv.destroyAllWindows()
print("find outline image")
ia.find_outline_image()
cv.imshow("outline image", ia.outline_image)
cv.waitKey(0)
cv.destroyAllWindows()
#cv.destroyAllWindows()
print("convert to cartesian coordinates")
ia.convert_to_cart_path(ia.outline_image)
......@@ -208,6 +265,7 @@ if __name__ == "__main__":
print("convert to polar coordianates")
ia.convert_to_pol_path(ia.cart_path)
show_pol_path(ia.pol_path)
cv.waitKey(0)
print("find high spots")
ia.find_high_spots_index(ia.pol_path)
......@@ -221,7 +279,19 @@ if __name__ == "__main__":
ia.extracted_corners()
print(ia.corners)
print("split sides")
ia.split_sites()
print("normalize sides")
ia.normalize_sides()
print("polish side paths")
ia.polish_side_paths()
for i in range(5):
temp = ia.convert_to_binary_array(ia.sides_cart_path[i])
cv.imshow(f"Side {i}", temp*255)
cv.waitKey(0)
#%% playground
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment