Exemplo n.º 1
0
 def prepare_dataset(self, all_images=False):
     dataset = imantics.Dataset('INbreast')
     categories_names = list(self.annotations_df.category.unique())
     colors = {name: imantics.Color.random() for name in categories_names}
     categories = {
         name: imantics.Category(name, id=i, color=colors[name])
         for i, name in enumerate(categories_names)
     }
     total = len(self.annotations_df)
     prepared = 0
     for case_id, annotations_data in self.annotations_df.groupby(
             'case_id'):
         width = annotations_data['width'].unique()[0]
         height = annotations_data['height'].unique()[0]
         image = imantics.Image(id=case_id, width=width, height=height)
         image.file_name = '%d.png' % case_id
         annotations = []
         for _, annotation_data in annotations_data.iterrows():
             annotation_id = annotation_data['annotation_id']
             category = categories[annotation_data['category']]
             color = colors[annotation_data['category']]
             points = annotation_data['points']
             annotation = imantics.Annotation(id=annotation_id,
                                              polygons=[points],
                                              category=category,
                                              color=color)
             annotations.append(annotation)
         image.add(annotations)
         dataset.add(image)
         prepared += 1
         print('\r%d %d/%d' % (case_id, prepared, total), end="")
     print('\rprepared %d images' % prepared)
     print()
     self.dataset = dataset
Exemplo n.º 2
0
    def __call__(self):

        category = CategoryModel.objects(id=self.category_id).first()
        if category:
            category = category()

        data = {
            'image': None,
            'category': category,
            'color': self.color,
            'polygons': self.segmentation,
            'width': self.width,
            'height': self.height,
            'metadata': self.metadata
        }

        return im.Annotation(**data)
Exemplo n.º 3
0
    def imageCallback(self, img_msg, info_msg):
        try:
            img = self.bridge.imgmsg_to_cv2(img_msg, "bgr8")
            header = img_msg.header
        except CvBridgeError as err:
            rospy.logerr(err)
            return

        board_mask = np.zeros_like(img[:, :, 0])
        if (self.image_roi is None):
            board_mask[:, :] = 1
        else:
            board_mask[self.image_roi[0]:self.image_roi[1],
                       self.image_roi[2]:self.image_roi[3]] = 1
        board_size = board_mask.sum()
        all_markers = segmentImage(img, self.filter_size, self.filter_const)
        markers_masked = (all_markers + 1) * board_mask

        filtered_idxs, filtered_counts = filterMarkers(markers_masked,
                                                       board_size / 250,
                                                       board_size / 3)
        markers_masked[np.isin(markers_masked, filtered_idxs, invert=True)] = 0
        ann_img = imantics.Image(image_array=img)
        colors = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (0, 255, 255),
                  (255, 0, 255), (255, 255, 0)]
        for j, idx in enumerate(filtered_idxs):
            mask = imantics.Mask((markers_masked == idx).astype(np.uint8))
            ann = imantics.Annotation(image=ann_img,
                                      category=imantics.Category(
                                          'obj_{}'.format(j),
                                          color=imantics.Color(rgb=colors[j])),
                                      mask=mask,
                                      bbox=imantics.BBox.from_mask(mask))
            ann_img.add(ann)
        display_img = ann_img.draw(thickness=1, color_by_category=True)

        try:
            display_msg = self.bridge.cv2_to_imgmsg(display_img.astype(
                np.uint8),
                                                    encoding="bgr8")
        except CvBridgeError as err:
            rospy.logerr(err)
            return

        display_msg.header = img_msg.header
        self.image_pub.publish(display_msg)
Exemplo n.º 4
0
                              '%d_pixels%d.png' % (case_id, index_breast))
 pm_mask_fn = osp.join(out_annotations_dir,
                       '%d_pixels%d.png' % (case_id, index_pectoral_muscle))
 img_rescaled = cv2.resize(img, scaled_dim)
 cv2.imwrite(img_rescaled_fn, img_rescaled)
 cv2.imwrite(img_breat_mask_fn, breast_mask_gray)
 cv2.imwrite(pm_mask_fn, pm_mask_gray)
 # add to coco
 coco_image = imantics.Image(id=case_id,
                             width=scaled_dim[1],
                             height=scaled_dim[0])
 coco_image.file_name = '%d.png' % case_id
 breast_mask_mask = imantics.Mask.create(breast_mask_gray)
 pm_mask_mask = imantics.Mask.create(pm_mask_gray)
 breast_annotation = imantics.Annotation(mask=breast_mask_mask,
                                         category=category_breast,
                                         color=category_breast.color)
 pectoral_muscle_annotation = imantics.Annotation(
     mask=pm_mask_mask,
     category=category_pectoral_muscle,
     color=category_pectoral_muscle.color)
 coco_image.add(breast_annotation)
 coco_image.add(pectoral_muscle_annotation)
 dataset.add(coco_image)
 # show debug
 if debug:
     # resize image
     img_debug = np.copy(img)
     for pt in pts:
         pt_tuple = (pt[0], pt[1])
         img_debug = cv2.circle(img_debug, pt_tuple, 10, (0, 255, 255), -1)