diff --git a/main.py b/main.py new file mode 100755 index 0000000..f633012 --- /dev/null +++ b/main.py @@ -0,0 +1,38 @@ +##!/usr/bin/env python3 +import numpy as np +import cv2 +from pathlib import Path +import math + + +def create_slope_image(image_width: int, image_height: int, p1_angle: float, p1_radius: float, bg_color: float = 0.0, fg_color: float = 1.0): + """ + creates an image containing 2 regions separated by a line defined as the tangent of a point p1 on a centered circle + + the semi-plane the contains the origin of the circle is colored with fg_color, while the other semi plane is assigned pixel values bg_color + """ + slope_image = np.ndarray(shape=(image_height, image_width), dtype=float) + # work out the equation of the tangent in the form a.x + b.y + c = 0 + c = np.array([image_height * 0.5, image_width * 0.5]) + p1 = c + np.array([math.cos(p1_angle), math.sin(p1_angle)]) * p1_radius + cp1 = p1 - c + a = cp1[0] + b = cp1[1] + c = - p1[0] * cp1[0] - p1[1] * cp1[1] + x = np.tile(np.arange(image_width).reshape((1, image_width)), (image_height, 1)) + y = np.tile(np.arange(image_height).reshape((image_height, 1)), (1, image_width)) + # print('x=', x) + # print('y=', y) + retval, slope_image = cv2.threshold(x * a + y * b + c, 0.0, fg_color, type=cv2.THRESH_BINARY_INV) + # print('slope_image=', slope_image) + cv2.imwrite('slope.tif', slope_image) + return slope_image + + +def main(): + slope_image = create_slope_image(image_width=128, image_height=256, p1_angle=0.7, p1_radius=3.0) + cv2.imwrite(str(Path('toto.tif')), slope_image) + + +if __name__ == '__main__': + main()