def _pasteImage(self, pasted_image, pasted_image_mask, image_background, x_offset, y_offset): pasted_image_info = ImageInfo(pasted_image) image_background_info = ImageInfo(image_background) image_background_bbox = BoundingBox( top=0, left=0, bottom=image_background_info.height, right=image_background_info.width) pasted_image_bbox = BoundingBox( top=y_offset, left=x_offset, bottom=y_offset + pasted_image_info.height, right=x_offset + pasted_image_info.width) intersection_bbox = pasted_image_bbox.intersect(image_background_bbox) pasted_image_y_offset_from, pasted_image_x_offset_from, pasted_image_y_offset_to, pasted_image_x_offset_to = 0, 0, 0, 0 if image_background_bbox.left > pasted_image_bbox.left: pasted_image_x_offset_from = -pasted_image_bbox.left if image_background_bbox.top > pasted_image_bbox.top: pasted_image_y_offset_from = -pasted_image_bbox.top pasted_image_x_offset_to = min( pasted_image_x_offset_from + intersection_bbox.width - 1, pasted_image_info.width) pasted_image_y_offset_to = min( pasted_image_y_offset_from + intersection_bbox.height - 1, pasted_image_info.height) image_background_y_offset_from, image_background_x_offset_from, image_background_y_offset_to, image_background_x_offset_to = 0, 0, 0, 0 if x_offset >= 0: image_background_x_offset_from = x_offset if y_offset >= 0: image_background_y_offset_from = y_offset image_background_x_offset_to = image_background_x_offset_from + pasted_image_x_offset_to - pasted_image_x_offset_from image_background_y_offset_to = image_background_y_offset_from + pasted_image_y_offset_to - pasted_image_y_offset_from for channel_index in range(3): image_background[image_background_y_offset_from : image_background_y_offset_to, image_background_x_offset_from : image_background_x_offset_to, channel_index] = \ (pasted_image[pasted_image_y_offset_from : pasted_image_y_offset_to, pasted_image_x_offset_from : pasted_image_x_offset_to, channel_index] * (pasted_image_mask[pasted_image_y_offset_from : pasted_image_y_offset_to, pasted_image_x_offset_from : pasted_image_x_offset_to] / 255)) + \ (image_background[image_background_y_offset_from : image_background_y_offset_to, image_background_x_offset_from : image_background_x_offset_to, channel_index] * (1 - (pasted_image_mask[pasted_image_y_offset_from : pasted_image_y_offset_to, pasted_image_x_offset_from : pasted_image_x_offset_to] / 255))) return image_background
def test_intersect_no_interesction(self): bounding_box = BoundingBox(5, 5, 15, 15) bounding_box_to_intersect = BoundingBox(-10, 0, 4, 4) self.assertEqual(bounding_box.intersect(bounding_box_to_intersect), None) bounding_box_to_intersect = BoundingBox(16, 16, 17, 17) self.assertEqual(bounding_box.intersect(bounding_box_to_intersect), None)
def extractConnectedComponentsInMask(self, batch_index, class_index, connected_components, mask): bounding_boxes, countour_areas = BoundingBox.fromOpenCVConnectedComponentsImage(mask, 25, 256) for bounding_box in bounding_boxes: if bounding_box.area >= 2: mask_cropped = mask[bounding_box.top:bounding_box.bottom+1,bounding_box.left:bounding_box.right+1] if self.visual_logging: cv2.imshow(f'Mask {self.config.classes[class_index]}', cv2.resize(mask_cropped, (mask_cropped.shape[1], mask_cropped.shape[0])) ) cv2.waitKey(0) if not batch_index in connected_components: connected_components[batch_index] = {} if not class_index in connected_components[batch_index]: connected_components[batch_index][class_index] = [] unique_bounding_box = True for connected_component_in_class in connected_components[batch_index][class_index]: previous_bounding_box = connected_component_in_class['bounding_box'] if previous_bounding_box.areBoundsAproximatelySimilar(bounding_box): unique_bounding_box = False break if unique_bounding_box: object = { 'mask': mask_cropped, 'bounding_box' : bounding_box } connected_components[batch_index][class_index].append(object)
def test_dimensions(self): bounding_box = BoundingBox(-10, 5, 4, 6) self.assertEqual(bounding_box.area, 30) self.assertEqual(bounding_box.width, 2) self.assertEqual(bounding_box.height, 15)
def test_intersect_diagonal_interesction(self): bounding_box = BoundingBox(5, 5, 15, 15) bounding_box_to_intersect = BoundingBox(-10, -15, 6, 6) bounding_box_expected_intersection = BoundingBox(5, 5, 6, 6) self.assertEqual(bounding_box.intersect(bounding_box_to_intersect), bounding_box_expected_intersection)
def test_intersect_right_interesction(self): bounding_box = BoundingBox(5, 5, 15, 15) bounding_box_to_intersect = BoundingBox(-10, 15, 20, 16) bounding_box_expected_intersection = BoundingBox(5, 15, 15, 15) self.assertEqual(bounding_box.intersect(bounding_box_to_intersect), bounding_box_expected_intersection)