def test_paste_when_location_is_outside_image_negative_coordinates_3_channels( self): image_target = Image.blank(10, 10) image_src = Image(img=np.full((2, 2, 3), 2, np.uint8)) image_target.paste(image_src, -10, 0) a = np.sum(image_target.img) self.assertEquals(a, 0)
def test_draw_circle_thickness_equals_one(self): img = Image.blank(40, 40) circle = Circle(Point(20, 20), 10) img.draw_circle(circle, Color(200, 200, 200), 1) self.assertEquals(img.img[10][20][1], 200) self.assertEquals(img.img[9][20][1], 0) self.assertEquals(img.img[11][20][1], 0)
def TRIANGLE_DEMO(): """ Draw a set of axes and a triangle. Perform a series of random transformations on the triangle and display the results. """ A = Point(143, 52) B = Point(17, 96.5) C = Point(0, 0) for i in range(10): # Create random transformation angle = random.random() * 2 * math.pi scale = random.random() * 3 delX = (random.random() - 0.5) * 200 delY = (random.random() - 0.5) * 200 translate = Point(delX, delY) transform = Transform(translate, angle, scale) # Transform the triangle A_ = transform.transform(A) B_ = transform.transform(B) C_ = transform.transform(C) # From the line A-B and the transformed line A'-B', determine what the transformation was # This should be the same as the original transformation trans_calc = Transform.line_mapping(A, B, A_, B_) print("Angle: {:.2f}; {:.2f}".format(rad_to_deg(angle), rad_to_deg(trans_calc.rot))) print("Trans: ({}); ({})".format(translate, trans_calc.trans)) print("Zoom: {:.2f}; {:.2f}".format(scale, trans_calc.zoom)) # Display on image image = Image.blank(1000, 800) image.draw_offset = IMG_CENTER draw_axes(image, 300) # Draw original triangle image.draw_line(A, B, Color.Red(), 5) image.draw_line(C, B, Color.Red(), 5) image.draw_line(A, C, Color.Red(), 5) #Draw transformed triangle image.draw_line(A_, B_, Color.Green()) image.draw_line(A_, C_, Color.Green()) image.draw_line(C_, B_, Color.Green()) # Check that the reverse transformation works properly A__ = transform.reverse(A_) B__ = transform.reverse(B_) C__ = transform.reverse(C_) # Draw the reverse transformation - this should overlap the origianl triangle image.draw_line(A__, B__, Color.Green(), 1) image.draw_line(A__, C__, Color.Green(), 1) image.draw_line(C__, B__, Color.Green(), 1) # Write the transformation on the image image.draw_text(transform.__str__(), Point(-450, 350), Color.White(), centered=False, scale=0.5, thickness=1) # Show the image image.popup()
def test_paste_when_location_is_inside_image_3_channels(self): image_target = Image.blank(10, 10) image_src = Image(img=np.full((1, 1, 3), 2, np.uint8)) image_target.paste(image_src, 0, 0) a = np.sum(image_target.img) self.assertEquals(a, 6) self.assertEquals(image_target.img[0, 0, 1], 2) self.assertEquals(image_target.img[0, 1, 1], 0)
def edges_image(edge_sets): blank = Image.blank(image_original.width, image_original.height, 3, 255) for shape in edge_sets: for edge in shape: print(edge[1]) blank.draw_line(Point.from_array(edge[0]), Point.from_array(edge[1]), Color.Green(), 1) return blank
def test_sub_image_has_size_of_input_image_if_2xradius_covers_the_whole_image( self): image = Image.blank(5, 6, 3, 0) x_center = 2 y_center = 2 radius = 7 sub_image, roi = image.sub_image(Point(x_center, y_center), radius) height = sub_image.height width = sub_image.width self.assertEquals(width, image.width) self.assertEquals(height, image.height)
def test_sub_image_has_size_0x0_if_center_and_radius_outside_the_input_image( self): image = Image.blank(5, 6, 3, 0) x_center = 10 y_center = 10 radius = 2 sub_image, roi = image.sub_image(Point(x_center, y_center), radius) height = sub_image.height width = sub_image.width self.assertEquals(width, 0) self.assertEquals(height, 0)
def test_sub_image_is_created_if_the_center_is_outside_but_the_radius_ovelaps_with_input_image( self): image = Image.blank(9, 9, 3, 0) x_center = 10 y_center = 10 radius = 2 sub_image, roi = image.sub_image(Point(x_center, y_center), radius) height = sub_image.height width = sub_image.width self.assertEquals(width, 1) self.assertEquals(height, 1)
def test_sub_image_is_not_square_if_center_x_is_too_close_to_the_edge( self): image = Image.blank(10, 10, 3, 0) x_center = 9 y_center = 5 radius = 2 sub_image, roi = image.sub_image(Point(x_center, y_center), radius) height = sub_image.height width = sub_image.width self.assertEquals(width, 3) self.assertEquals(height, 2 * radius) self.assertNotEquals(width, height)
def test_draw_circle_thickness_equals_four(self): img = Image.blank(40, 40) circle = Circle(Point(20, 20), 10) img.draw_circle(circle, Color(200, 200, 200), 4) self.assertEquals(img.img[6][20][1], 0) self.assertEquals(img.img[7][20][1], 0) #!! self.assertEquals(img.img[8][20][1], 200) self.assertEquals(img.img[10][20][1], 200) self.assertEquals(img.img[9][20][1], 200) self.assertEquals(img.img[11][20][1], 200) self.assertEquals(img.img[12][20][1], 200) self.assertEquals(img.img[13][20][1], 0) #!! self.assertEquals(img.img[14][20][1], 0)
def test_sub_image_is_square_with_side_length_2_radius_if_enough_space_to_cut_from( self): image = Image.blank(10, 10, 3, 0) x_center = 5 y_center = 5 radius = 2 sub_image, roi = image.sub_image(Point( x_center, y_center), radius) #sub_image returns a raw cv image height = sub_image.height width = sub_image.width self.assertEquals(width, 2 * radius) self.assertEquals(height, 2 * radius) self.assertEquals(width, height)
def test_draw_circle_centre_is_kept(self): img = Image.blank(40, 40) circle = Circle(Point(20, 20), 1) img.draw_circle(circle, Color(200, 200, 200), 1) self.assertEquals(img.img[18][20][1], 0) self.assertEquals(img.img[19][20][1], 200) self.assertEquals(img.img[20][20][1], 0) self.assertEquals(img.img[21][20][1], 200) self.assertEquals(img.img[22][20][1], 0) self.assertEquals(img.img[20][18][1], 0) self.assertEquals(img.img[20][19][1], 200) self.assertEquals(img.img[20][20][1], 0) self.assertEquals(img.img[20][21][1], 200) self.assertEquals(img.img[20][22][1], 0)
def CIRCLES_DEMO(): """ Draw a set of axes and a random set of circles. Perform a series of random transformations on the circles. """ # Create a set of random circles points = [] for i in range(10): X = (random.random()) * 200 Y = (random.random()) * 200 points.append(Point(X, Y)) for i in range(10): # Create random transformation angle = random.random() * 2 * math.pi scale = random.random() * 3 delX = (random.random() - 0.5) * 200 delY = (random.random() - 0.5) * 200 translate = Point(delX, delY) trs = Transform(translate, angle, scale) # Display on image image = Image.blank(1000, 800) image.draw_offset = IMG_CENTER draw_axes(image, 300) # Draw the circles and transformed circles on the image radius = 10 for p in points: circle = Circle(p, radius) trans_circle = Circle(trs.transform(p), radius * trs.zoom) image.draw_circle(circle, Color.Red()) image.draw_circle(trans_circle, Color.Blue()) # Write the transformation on the image image.draw_text(trs.__str__(), Point(-450, 350), Color.White(), centered=False, scale=0.5, thickness=1) # Show the image image.popup()
def test_circle_is_correctly_detected_when_there_is_one_circle_in_the_image( self): img = Image.blank(40, 40) circle = Circle(Point(20, 20), 10) img.draw_circle(circle, Color(200, 200, 200), 2) grey = img.to_grayscale() #parameters of the detector very important - a bit dubious test decorator = CircleDetector() decorator.set_maximum_radius(20) decorator.set_minimum_radius(5) decorator.set_accumulator_threshold(20) decorator.set_canny_threshold(20) list = decorator.find_circles(grey) self.assertEqual(list.__len__(), 1) # self.assertEqual(list[0].radius(), circle.radius()) #self.assertEqual(list[0].center().x, circle.center().x + 1) #self.assertEqual(list[0].center().y, circle.center().y + 1) self.assertEqual(list[0].area(), circle.area())
def test_circle_is_correctly_detected_when_there_there_are_two_circles_in_the_image_not_intersecting( self): img = Image.blank(100, 100) circle_a = Circle(Point(20, 20), 10) circle_b = Circle(Point(50, 50), 10) img.draw_circle(circle_a, Color(10, 50, 100), 2) img.draw_circle(circle_b, Color(10, 50, 100), 2) grey = img.to_grayscale() decorator = CircleDetector() decorator.set_maximum_radius(20) decorator.set_minimum_radius(5) decorator.set_accumulator_threshold(30) decorator.set_canny_threshold(30) decorator.set_minimum_separation(10) list = decorator.find_circles(grey) self.assertEqual(list.__len__(), 2)
def test_paste_when_location_is_outside_image_x_off(self): image_target = Image.blank(10, 10) image_src = Image(img=np.full((5, 5, 3), 2, np.uint8)) image_target.paste(image_src, 20, 0) a = np.sum(image_target.img) self.assertEquals(a, 0)
for shape in edge_sets: for edge in shape: print(edge[1]) blank.draw_line(Point.from_array(edge[0]), Point.from_array(edge[1]), Color.Green(), 1) return blank # Make images image_contours = polygon_image(contours) image_polygons = polygon_image(polygons) image_filter1 = edges_image(edge_sets2) image_filter2 = edges_image(edge_sets5) # Make finder pattern image image_fp = Image.blank(image_original.width, image_original.height, 3, 255) fps[0].draw_to_image(image_fp) # Popups image_original.rescale(2).popup() image_mono.rescale(2).popup() image_threshold.rescale(2).popup() image_morphed.rescale(2).popup() image_contours.rescale(2).popup() image_polygons.rescale(2).popup() image_filter1.rescale(2).popup() image_filter2.rescale(2).popup() image_fp.rescale(2).popup() # Save images
def polygon_image(lines): blank = Image.blank(image_original.width, image_original.height, 3, 255) img = cv2.drawContours(blank.img, lines, -1, (0, 255, 0), 1) return Image(img)
def test_no_circle_detected_in_blan_image(self): img = Image.blank(100, 100, 1, 0) decorator = CircleDetector() list_of_circle = decorator.find_circles(img) self.assertEqual(list_of_circle.__len__(), 0)