Beispiel #1
0
    def make_chip(self, sample, imgset):
        image = cv2.imread(sample['image'])
        height, width = sample['height'], sample['width']
        img_id = osp.splitext(osp.basename(sample['image']))[0]

        mask_path = osp.join(self.segmentation_dir, '{}.hdf5'.format(img_id))
        with h5py.File(mask_path, 'r') as hf:
            mask = np.array(hf['label'])
        mask_h, mask_w = mask.shape[:2]

        # make chip
        region_box, contours = utils.generate_box_from_mask(mask)
        region_box = utils.region_postprocess(region_box, contours,
                                              (mask_w, mask_h))
        region_box = utils.resize_box(region_box, (mask_w, mask_h),
                                      (width, height))
        region_box = utils.generate_crop_region(region_box, (width, height))

        if args.show:
            utils.show_image(image, region_box)
        # if imgset == 'train':
        #     region_box = np.vstack((region_box, np.array([0, 0, width-1, height-1])))

        gt_bboxes, gt_cls = sample['bboxes'], sample['cls']

        chip_gt_list, chip_label_list, neglect_list = self.generate_region_gt(
            region_box, gt_bboxes, gt_cls)
        chip_loc = self.write_chip_and_anno(image, img_id, imgset, region_box,
                                            chip_gt_list, chip_label_list,
                                            neglect_list)

        return chip_loc
Beispiel #2
0
    def make_chip(self, img_name):
        image = cv2.imread(osp.join(self.img_dir, img_name))
        height, width = image.shape[:2]
        img_id = osp.splitext(osp.basename(img_name))[0]
        # mask_path = ""
        mask_path = osp.join(self.mask_dir, '{}.hdf5'.format(img_id))
        with h5py.File(mask_path, 'r') as hf:
            mask = np.array(hf['label'])
        mask_h, mask_w = mask.shape[:2]

        # make chip
        region_box, contours = utils.generate_box_from_mask(mask)
        region_box = utils.region_postprocess(region_box, contours,
                                              (mask_w, mask_h))
        region_box = utils.resize_box(region_box, (mask_w, mask_h),
                                      (width, height))
        region_box = utils.generate_crop_region(region_box, (width, height))
        try:
            region_box = np.vstack(
                (region_box, np.array([0, 0, width - 1, height - 1])))
        except:
            print("empty box")

        if args.show:
            utils.show_image(image, region_box)

        chip_loc = self.write_chip_and_anno(image, img_id, region_box)

        return len(region_box), chip_loc
Beispiel #3
0
    def make_chip(self, img_name):
        image = cv2.imread(osp.join(self.img_dir, img_name))
        height, width = image.shape[:2]
        img_id = osp.splitext(osp.basename(img_name))[0]
        # mask_path = ""
        mask_path = osp.join(self.mask_dir, '{}.hdf5'.format(img_id))
        with h5py.File(mask_path, 'r') as hf:
            mask = np.array(hf['label'])
        mask_h, mask_w = mask.shape[:2]

        # make chip
        region_box, contours = utils.generate_box_from_mask(mask)
        # # utils.show_image(mask, np.array(region_box))
        region_box = utils.generate_crop_region(region_box, mask, (mask_w, mask_h), (width, height), self.gbm, args.aim)
        # # utils.show_image(mask, np.array(region_box))
        region_box = utils.resize_box(region_box, (mask_w, mask_h), (width, height))

        # if len(region_box) == 0:
        #     region_box = np.array([[0, 0, width, height]])
        # else:
        #     region_box = np.vstack((region_box, [0, 0, width, height]))

        if args.show:
            utils.show_image(image[..., ::-1], np.array(region_box))

        chip_loc = self.write_chip_and_anno(image, img_id, region_box)

        return chip_loc
Beispiel #4
0
    def make_chip(self, sample, imgset):
        # get image and mask informations
        image = cv2.imread(sample['image'])
        height, width = sample['height'], sample['width']
        img_id = osp.splitext(osp.basename(sample['image']))[0]
        mask_path = osp.join(self.segmentation_dir, '{}.hdf5'.format(img_id))
        with h5py.File(mask_path, 'r') as hf:
            mask = np.array(hf['label'])
        mask_h, mask_w = mask.shape[:2]

        # make chip
        region_box, contours = utils.generate_box_from_mask(mask)
        region_box = utils.generate_crop_region(region_box, mask,
                                                (mask_w, mask_h),
                                                (width, height), self.gbm,
                                                args.aim)
        region_box = utils.resize_box(region_box, (mask_w, mask_h),
                                      (width, height))

        # make tiling
        if args.tiling and imgset != "val":
            tiling = utils.add_tiling((width, height))
            for pattern in tiling:
                if utils.iou_calc1(pattern, region_box).max() < 0.85:
                    region_box = np.vstack((region_box, tiling))

        if args.show:
            utils.show_image(image[..., ::-1], np.array(region_box))

        # get box and class
        gt_bboxes, gt_cls = sample['bboxes'], sample['cls']

        # generate chip annotations and writer chip image
        chip_gt_list, chip_label_list, neglect_list = self.generate_region_gt(
            region_box, gt_bboxes, gt_cls)
        chip_loc = self.write_chip_and_anno(image, img_id, region_box,
                                            chip_gt_list, chip_label_list,
                                            neglect_list, imgset)

        return chip_loc
def _worker(img_path, dataset):
    imgid = os.path.basename(img_path)[:-4]
    image = cv2.imread(img_path)
    height, width = image.shape[:2]

    mask_path = os.path.join(segmentation_dir, imgid + '_region.png')
    mask_img = cv2.imread(mask_path, cv2.IMREAD_GRAYSCALE)
    mask_h, mask_w = mask_img.shape[:2]
    region_box, contours = utils.generate_box_from_mask(mask_img)
    region_box = utils.region_postprocess(region_box, contours,
                                          (mask_w, mask_h))
    region_box = utils.resize_box(region_box, (mask_w, mask_h),
                                  (width, height))
    region_box = utils.generate_crop_region(region_box, (width, height))
    region_box = np.vstack((region_box, np.array([0, 0, width - 1,
                                                  height - 1])))

    gt_boxes, labels = dataset.get_gtbox(img_path)

    chip_list, chip_gt_list, chip_label_list = generate_region_gt(
        (width, height), region_box, gt_boxes, labels)
    chip_loc = write_chip_and_anno(image, imgid, chip_list, chip_gt_list,
                                   chip_label_list)
    return len(chip_list), chip_loc
Beispiel #6
0
def _vis(img_path, dataset):
    img = cv2.imread(img_path)
    height, width = img.shape[:2]

    img_id = os.path.basename(img_path)[:-4]
    pred_mask_path = os.path.join(pred_mask_dir, img_id + '.png')
    label_mask_path = os.path.join(segmentation_dir, img_id + '_region.png')
    pred_mask = cv2.imread(pred_mask_path) * 255
    label_mask = cv2.imread(label_mask_path) * 255

    # bounding box
    img1 = img.copy()
    gt_box_list, _ = dataset.get_gtbox(img_path)
    for box in gt_box_list:
        cv2.rectangle(img1, (box[0], box[1]), (box[2], box[3]), (255, 0, 0), 4)
        cv2.putText(img1, str((box[2], box[3])), (box[0], box[3]),
                    cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 4)
    cv2.putText(img1, str((width, height)), (100, 100),
                cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 0), 4)
    label_region_box, _ = utils.generate_box_from_mask(label_mask[:, :, 0])
    print('box', gt_box_list)
    print('region', label_region_box)
    label_region_box = utils.resize_box(label_region_box, (40, 30),
                                        (width, height))
    for box in label_region_box:
        cv2.rectangle(img1, (box[0], box[1]), (box[2], box[3]), (255, 0, 0), 5)

    # region box
    img2 = img.copy()
    mask_h, mask_w = pred_mask.shape[:2]
    region_box, contours = utils.generate_box_from_mask(pred_mask[:, :, 0])
    resize_region_box = utils.resize_box(region_box, (mask_w, mask_h),
                                         (width, height))
    for box in resize_region_box:
        cv2.rectangle(img2, (box[0], box[1]), (box[2], box[3]), (255, 0, 0), 5)

    # region postprocess
    img3 = img.copy()
    new_regions = utils.region_postprocess(region_box, contours,
                                           (mask_w, mask_h))
    resize_region_box = utils.resize_box(new_regions, (mask_w, mask_h),
                                         (width, height))
    # new_regions = utils.generate_crop_region(resize_region_box, (width, height))
    for box in resize_region_box:
        cv2.rectangle(img3, (box[0], box[1]), (box[2], box[3]), (255, 0, 0), 5)

    img4 = img.copy()
    # resize_region_box = utils.resize_box(temp, (mask_w, mask_h), (width, height))
    new_regions = utils.generate_crop_region(resize_region_box,
                                             (width, height))
    for box in new_regions:
        cv2.rectangle(img4, (box[0], box[1]), (box[2], box[3]), (255, 0, 0),
                      10)

    plt.subplot(1, 1, 1)
    plt.imshow(img1[:, :, [2, 1, 0]])
    # plt.subplot(2, 3, 2); plt.imshow(img2[:, :, [2,1,0]])
    # plt.subplot(2, 3, 3); plt.imshow(img3[:, :, [2,1,0]])
    # plt.subplot(2, 3, 4); plt.imshow(label_mask[:, :, [2,1,0]])
    # plt.subplot(2, 3, 5); plt.imshow(pred_mask[:, :, [2,1,0]])
    # plt.subplot(2, 3, 6); plt.imshow(img4[:, :, [2,1,0]])

    plt.show()
    cv2.waitKey(0)
Beispiel #7
0
    pixel_num = []
    for img_path in tqdm(val_list, ncols=80):
        img_name = os.path.basename(img_path)
        raw_file = os.path.join(mask_path, img_name[:-4]+'.png')
        
        img = cv2.imread(img_path)
        height, width = img.shape[:2]
        mask_img = cv2.imread(raw_file, cv2.IMREAD_GRAYSCALE)
        mask_h, mask_w = mask_img.shape[:2]
        
        pixel_num.append(np.sum(mask_img))

        label_box, _ = dataset.get_gtbox(img_path)
        region_box, contours = utils.generate_box_from_mask(mask_img)
        region_box = utils.region_postprocess(region_box, contours, (mask_w, mask_h))
        region_box = utils.resize_box(region_box, (mask_w, mask_h), (width, height))
        region_box = utils.generate_crop_region(region_box, (width, height))

        count = 0
        for box1 in label_box:
            for box2 in region_box:
                if utils.overlap(box2, box1):
                    count += 1
                    break

        label_object.append(len(label_box))
        detect_object.append(count)
        mask_object.append(len(region_box))
        if len(label_box) != count:
            undetected_img.append(img_name)
Beispiel #8
0
def _vis(img_path, dataset):
    img = cv2.imread(img_path)
    height, width = img.shape[:2]

    img_id = os.path.basename(img_path)[:-4]
    pred_mask_path = os.path.join(pred_mask_dir, img_id+'.png')
    label_mask_path = os.path.join(segmentation_dir, img_id+'_region.png')
    pred_mask = cv2.imread(pred_mask_path) * 255
    label_mask = cv2.imread(label_mask_path) * 255

    # bounding box
    img1 = img.copy()
    gt_box_list, _ = dataset.get_gtbox(img_path)
    for box in gt_box_list:
        cv2.rectangle(img1, (box[0], box[1]), (box[2], box[3]), (255, 127, 0), 3)
    #     cv2.putText(img1, str((box[2], box[3])), (box[0], box[3]), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,0), 4)
    # cv2.putText(img1, str((width, height)), (100, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,0,0), 4)
    # label_region_box, _ = utils.generate_box_from_mask(label_mask[:, :, 0])
    # label_region_box = utils.resize_box(label_region_box, (40, 30), (width, height))
    # for box in label_region_box:
    #     cv2.rectangle(img1, (box[0], box[1]), (box[2], box[3]), (0, 127, 255), 5)

    # region box
    img2 = img.copy()
    mask_h, mask_w = pred_mask.shape[:2]
    region_box, contours = utils.generate_box_from_mask(pred_mask[:, :, 0])
    resize_region_box = utils.resize_box(region_box, (mask_w, mask_h), (width, height))
    for box in resize_region_box:
        cv2.rectangle(img2, (box[0], box[1]), (box[2], box[3]), (0, 127, 255), 4)

    # region postprocess
    img3 = img.copy()
    new_regions = utils.region_postprocess(region_box, contours, (mask_w, mask_h))
    resize_region_box = utils.resize_box(new_regions, (mask_w, mask_h), (width, height))
    # new_regions = utils.generate_crop_region(resize_region_box, (width, height))
    for box in resize_region_box:
        cv2.rectangle(img3, (box[0], box[1]), (box[2], box[3]), (0, 127, 255), 4)

    img4 = img.copy()
    # resize_region_box = utils.resize_box(temp, (mask_w, mask_h), (width, height))
    new_regions = utils.generate_crop_region(resize_region_box, (width, height))
    chip_list = []
    for box in new_regions:
        cv2.rectangle(img4, (box[0], box[1]), (box[2], box[3]), (0, 127, 255), 7)
        chip_list.append(img[box[1]:box[3], box[0]:box[2], :].copy())

    # heat map
    img5 = img.copy().astype(np.float64)
    pred_mask_path = os.path.join(pred_mask_dir, img_id+'_heat.png')
    heat_mask = cv2.imread(pred_mask_path, cv2.IMREAD_GRAYSCALE)
    heat_mask = cv2.resize(heat_mask, (width, height), interpolation=cv2.INTER_NEAREST)
    img5[:, :, 2] += heat_mask / 255 * 150
    

    plt.subplot(2, 3, 1); plt.imshow(img1[:, :, [2,1,0]])
    plt.subplot(2, 3, 2); plt.imshow(img2[:, :, [2,1,0]])
    plt.subplot(2, 3, 3); plt.imshow(img3[:, :, [2,1,0]])
    plt.subplot(2, 3, 4); plt.imshow(label_mask[:, :, [2,1,0]])
    plt.subplot(2, 3, 5); plt.imshow(pred_mask[:, :, [2,1,0]])
    plt.subplot(2, 3, 6); plt.imshow(img4[:, :, [2,1,0]])

    dirname = os.path.join(pred_mask_dir, os.path.pardir)
    cv2.imwrite(os.path.join(dirname, 'virtualization', 'image.jpg'), img)
    cv2.imwrite(os.path.join(dirname, 'virtualization', 'bbox.jpg'), img1)
    cv2.imwrite(os.path.join(dirname, 'virtualization', 'brec.jpg'), img2)
    cv2.imwrite(os.path.join(dirname, 'virtualization', 'post_process.jpg'), img3)
    cv2.imwrite(os.path.join(dirname, 'virtualization', 'label_mask.jpg'), label_mask)
    cv2.imwrite(os.path.join(dirname, 'virtualization', 'pred_mask.jpg'), pred_mask)
    cv2.imwrite(os.path.join(dirname, 'virtualization', 'result.jpg'), img4)
    cv2.imwrite(os.path.join(dirname, 'virtualization', 'heat.jpg'), img5)
    for i, chip in enumerate(chip_list):
        cv2.imwrite(os.path.join(dirname, 'virtualization', 'chip_%d.jpg' % i), chip)

    
    plt.show()
    cv2.waitKey(0)