Skip to content

Commit

Permalink
check all
Browse files Browse the repository at this point in the history
  • Loading branch information
yuichiroaoki committed Aug 4, 2023
1 parent 12b21a6 commit 9548ac4
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 98 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -159,4 +159,5 @@ cython_debug/
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/

output
output
lines
10 changes: 3 additions & 7 deletions cnceye/cmm/all.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,13 @@


class AllImages:
def __init__(self, start: Coordinate, camera: Camera, move: Coordinate) -> None:
self.center = start
def __init__(self, camera: Camera) -> None:
self.camera = camera
self.move = move
self.previous_lines = []

def add_image(self, image, distance: float) -> None:
single = SingleImage(image, self.center, self.camera)
def add_image(self, image, distance: float, center: Coordinate) -> None:
single = SingleImage(image, center, self.camera)
lines = single.lines(distance)
print(lines)
self.center += self.move
if lines is None:
return None

Expand Down
2 changes: 1 addition & 1 deletion cnceye/cmm/single.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def from_pixel_length(self, distance: float, pixel_length) -> float:
return pixel_length / pixel_per_mm

def lines(self, distance: float) -> List[Line] or None:
line_array = get_lines(self.image)
line_array = get_lines(self.image, max_line_gap=50)
if line_array is None:
return None

Expand Down
41 changes: 37 additions & 4 deletions cnceye/line.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,25 @@ def __init__(self, start: Coordinate, end: Coordinate) -> None:
self.end = end

def get_slope(self) -> float:
if self.end.x == self.start.x:
if abs(self.start.x - self.end.x) < 0.1:
return np.inf
return (self.end.y - self.start.y) / (self.end.x - self.start.x)

def get_slope_diff(self, other: "Line") -> float:
if self.get_slope() == other.get_slope():
return 0
return abs(self.get_slope() - other.get_slope())

def get_intercept(self) -> float:
if self.get_slope() == np.inf:
return np.inf
return self.start.y - self.get_slope() * self.start.x

def get_intercept_diff(self, other: "Line") -> float:
if self.get_intercept() == other.get_intercept():
return 0
return abs(self.get_intercept() - other.get_intercept())

def get_x(self, y: float) -> float:
return (y - self.get_intercept()) / self.get_slope()

Expand All @@ -40,13 +52,34 @@ def get_intersection(self, other) -> tuple:
return (x, y)

def is_same_straight_line(self, other: "Line") -> bool:
slope_diff = self.get_slope_diff(other)
intercept_diff = self.get_intercept_diff(other)
if self.get_slope() == np.inf and other.get_slope() == np.inf:
return abs(self.start.x - other.start.x) < 0.1
return slope_diff < 0.1 and intercept_diff < 0.1

def is_x_overlapping(self, other: "Line") -> bool:
return (
self.start.x <= other.start.x <= self.end.x
or self.start.x <= other.end.x <= self.end.x
or other.start.x <= self.start.x <= other.end.x
or other.start.x <= self.end.x <= other.end.x
)

def is_y_overlapping(self, other: "Line") -> bool:
return (
self.get_slope() == other.get_slope()
and self.get_intercept() == other.get_intercept()
self.start.y <= other.start.y <= self.end.y
or self.end.y <= other.start.y <= self.start.y
or self.start.y <= other.end.y <= self.end.y
or self.end.y <= other.end.y <= self.start.y
or other.start.y <= self.start.y <= other.end.y
or other.end.y <= self.start.y <= other.start.y
or other.start.y <= self.end.y <= other.end.y
or other.end.y <= self.start.y <= other.start.y
)

def is_overlapping(self, other: "Line") -> bool:
return self.end.x >= other.start.x or self.start.x <= other.end.x
return self.is_x_overlapping(other) and self.is_y_overlapping(other)

def connect_lines(self, other: "Line") -> "Line" or None:
if self.is_same_straight_line(other) and self.is_overlapping(other):
Expand Down
49 changes: 49 additions & 0 deletions scripts/test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
import bpy
import os
from time import sleep


def render_image(output_path, camera_position, camera_rotation, light_position):
# Set camera position and rotation
camera = bpy.data.objects["Camera"]
camera.location = camera_position
camera.rotation_euler = camera_rotation

# Set light position
lamp = bpy.data.objects["Light"]
lamp.location = light_position

# Set rendering settings
bpy.context.scene.render.image_settings.file_format = "PNG"
bpy.context.scene.render.filepath = output_path

# Render the image
bpy.ops.render.render(write_still=True)


# Example usage
if __name__ == "__main__":
# Define camera and light positions
camera_position_start = (-0.05, 0.025, 0.06)
camera_rotation = (0.0, 0.0, 0.0)
light_position = (0, 0, 0.1)

# Create a folder to save the rendered images
output_folder = "/home/runner/work/cnceye/cnceye/output"
os.makedirs(output_folder, exist_ok=True)

# Render images with different camera and light positions
index = 0
for i in range(1):
for j in range(1):
camera_position = (
camera_position_start[0] + j * 0.02,
camera_position_start[1] - i * 0.01,
camera_position_start[2],
)
output_path = os.path.join(output_folder, f"image_{index}.png")
render_image(output_path, camera_position, camera_rotation, light_position)
index += 1
sleep(0.1)

print("Rendering completed!")
88 changes: 81 additions & 7 deletions tests/test_all.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,87 @@
camera = Camera(focal_length, sensor_width)


def test_add_image():
start = Coordinate(-50.0, 25.0, 60.0)
move = Coordinate(10.0, 0.0, 0.0)
all_images = AllImages(start, camera, move)
for i in range(1, 11):
image = cv2.imread(f"tests/fixtures/output_images/image_{i}.png")
all_images.add_image(image, distance)
def test_add_image_one_row():
x_move = Coordinate(20.0, 0.0, 0.0)
all_images = AllImages(camera)
initial_center = Coordinate(-50.0, 25.0, 60.0)
index = 0
for i in range(0, 6):
image = cv2.imread(f"tests/fixtures/output_images/image_{index}.png")
center = initial_center + x_move * i
all_images.add_image(image, distance, center)
index += 1

print(f"lines: {len(all_images.previous_lines)}")
print(all_images.previous_lines)

all_images.save_image("output/one_row.png")

first_line = all_images.previous_lines[0]
expected_first_line_length = 106.0
first_line_diff_in_micron = diff_in_micron(
expected_first_line_length, first_line.get_length()
)
print(f"first line length: {first_line_diff_in_micron:.2f} μm")
assert first_line_diff_in_micron < 100.0

third_line = all_images.previous_lines[2]
expected_third_line_length = 75.0
third_line_diff_in_micron = diff_in_micron(
expected_third_line_length, third_line.get_length()
)
print(f"third line length: {third_line_diff_in_micron:.2f} μm")
assert third_line_diff_in_micron < 100.0


def test_add_image_two_rows():
x_move = Coordinate(20.0, 0.0, 0.0)
y_move = Coordinate(0.0, -10.0, 0.0)
all_images = AllImages(camera)
index = 0
initial_center = Coordinate(-50.0, 25.0, 60.0)
for i in range(0, 2):
for j in range(0, 6):
image = cv2.imread(f"tests/fixtures/output_images/image_{index}.png")
center = initial_center + x_move * j + y_move * i
all_images.add_image(image, distance, center)
index += 1

print(f"lines: {len(all_images.previous_lines)}")
for line in all_images.previous_lines:
print(line)

all_images.save_image("output/two.png")

first_line = all_images.previous_lines[0]
expected_first_line_length = 106.0
first_line_diff_in_micron = diff_in_micron(
expected_first_line_length, first_line.get_length()
)
print(f"first line length: {first_line_diff_in_micron:.2f} μm")
assert first_line_diff_in_micron < 100.0

third_line = all_images.previous_lines[2]
expected_third_line_length = 75.0
third_line_diff_in_micron = diff_in_micron(
expected_third_line_length, third_line.get_length()
)
print(f"third line length: {third_line_diff_in_micron:.2f} μm")
assert third_line_diff_in_micron < 100.0


def test_add_image_all_rows():
x_move = Coordinate(20.0, 0.0, 0.0)
y_move = Coordinate(0.0, -10.0, 0.0)
all_images = AllImages(camera)
index = 0
initial_center = Coordinate(-50.0, 25.0, 60.0)
for i in range(0, 6):
for j in range(0, 6):
image = cv2.imread(f"tests/fixtures/output_images/image_{index}.png")
center = initial_center + x_move * j + y_move * i
all_images.add_image(image, distance, center)
index += 1

print(f"lines: {len(all_images.previous_lines)}")
print(all_images.previous_lines)
Expand Down
28 changes: 28 additions & 0 deletions tests/test_line.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,31 @@ def test_get_intersection():
end = Coordinate(1, 0, 0)
other = Line(start, end)
assert line.get_intersection(other) == (0.5, 0.5)


def test_is_same_straight_line():
line0 = Line(
Coordinate(-53.01875, 14.89375, -100), Coordinate(-53.01875, 28.0, -100)
)
line1 = Line(
Coordinate(-53.01875, 4.89375, -100), Coordinate(-53.01875, 25.125, -100)
)
assert line0.is_same_straight_line(line1)


def test_is_overlapping():
line0 = Line(
Coordinate(-53.01875, 14.89375, -100), Coordinate(-53.01875, 28.0, -100)
)
line1 = Line(
Coordinate(-53.01875, 4.89375, -100), Coordinate(-53.01875, 25.125, -100)
)
assert line0.is_overlapping(line1)

line0 = Line(
Coordinate(-53.01875, 14.89375, -100), Coordinate(-53.01875, 28.0, -100)
)
line1 = Line(
Coordinate(-53.01875, 4.89375, -100), Coordinate(-53.01875, 11.125, -100)
)
assert not line0.is_overlapping(line1)
80 changes: 2 additions & 78 deletions tests/test_single.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
camera = Camera(focal_length, sensor_width)


def test_opencv_coord_1():
image = cv2.imread("tests/fixtures/output_images/image_1.png")
def test_opencv_coord_0():
image = cv2.imread("tests/fixtures/output_images/image_0.png")
center_coordinates = Coordinate(-50.0, 25.0, 60.0)

cmm = SingleImage(image, center_coordinates, camera)
Expand Down Expand Up @@ -54,79 +54,3 @@ def test_opencv_coord_1():
y_diff_in_micron = diff_in_micron(expected_corner.y, corner.y)
print(f"y: {y_diff_in_micron:.2f} μm")
assert y_diff_in_micron < 100.0


def test_opencv_coord_2():
image = cv2.imread("tests/fixtures/output_images/image_2.png")
center_coordinates = Coordinate(-40.0, 25.0, 60.0)

cmm = SingleImage(image, center_coordinates, camera)
circles = get_circles(image, 100, 50, 100, 200)
(x_pixel, y_pixel, r_pixel) = circles[0][0]

# check radius is close to expected radius
expected_radius = 3.0 # mm
radius_from_img_in_mm = cmm.from_pixel_length(distance, r_pixel)
radius_diff_in_micron = diff_in_micron(expected_radius, radius_from_img_in_mm)
print(f"radius: {radius_diff_in_micron:.2f} μm")
assert radius_diff_in_micron < 100.0

# check center is close to expected center
expected_circle_center = Coordinate(-48.0, 23.0, 10.0)
circle_center_from_img_in_mm = cmm.from_opencv_coord(distance, (x_pixel, y_pixel))
x_from_img_in_mm = circle_center_from_img_in_mm.x
y_from_img_in_mm = circle_center_from_img_in_mm.y

x_diff_in_micro = diff_in_micron(expected_circle_center.x, x_from_img_in_mm)
print(f"x: {x_diff_in_micro:.2f} μm")
assert x_diff_in_micro < 100.0

y_diff_in_micron = diff_in_micron(expected_circle_center.y, y_from_img_in_mm)
print(f"y: {y_diff_in_micron:.2f} μm")
assert y_diff_in_micron < 100.0

lines = cmm.lines(distance)
assert len(lines) > 0
expected_corner = Coordinate(-53.0, 28.0, 10.0)
corner = lines[0].start

x_diff_in_micro = diff_in_micron(expected_corner.x, corner.x)
print(f"x: {x_diff_in_micro:.2f} μm")
assert x_diff_in_micro < 100.0

y_diff_in_micron = diff_in_micron(expected_corner.y, corner.y)
print(f"y: {y_diff_in_micron:.2f} μm")
assert y_diff_in_micron < 100.0


def test_opencv_coord_3():
image = cv2.imread("tests/fixtures/output_images/image_3.png")
center_coordinates = Coordinate(-30.0, 25.0, 60.0)

cmm = SingleImage(image, center_coordinates, camera)
circles = get_circles(image, 100, 50, 100, 200)
(x_pixel, y_pixel, r_pixel) = circles[0][0]

# check radius is close to expected radius
expected_radius = 3.0 # mm
radius_from_img_in_mm = cmm.from_pixel_length(distance, r_pixel)
radius_diff_in_micron = diff_in_micron(expected_radius, radius_from_img_in_mm)
print(f"radius: {radius_diff_in_micron:.2f} μm")
# assert radius_diff_in_micron < 100.0

# check center is close to expected center
expected_circle_center = Coordinate(-48.0, 23.0, 10.0)
circle_center_from_img_in_mm = cmm.from_opencv_coord(distance, (x_pixel, y_pixel))
x_from_img_in_mm = circle_center_from_img_in_mm.x
y_from_img_in_mm = circle_center_from_img_in_mm.y

x_diff_in_micro = diff_in_micron(expected_circle_center.x, x_from_img_in_mm)
print(f"x: {x_diff_in_micro:.2f} μm")
# assert x_diff_in_micro < 100.0

y_diff_in_micron = diff_in_micron(expected_circle_center.y, y_from_img_in_mm)
print(f"y: {y_diff_in_micron:.2f} μm")
# assert y_diff_in_micron < 100.0

lines = cmm.lines(distance)
assert len(lines) > 0

0 comments on commit 9548ac4

Please sign in to comment.