Пример #1
0
 def __getitem__(self, index):
     img_path = self.filelist[index]
     img = convert_to_rgb(io.imread(img_path))
     img = resize_max_length(img, 240)
     img = img[:, :, 0].astype(np.float32)
     img /= 255.0
     return img
Пример #2
0
    def __getitem__(self, index):
        img_path = self.img_paths[index]
        img = io.imread(img_path)
        img = convert_to_rgb(img)

        img0 = img
        # h, w = (240, 240)
        # img0 = random_crop(img, size=(h, w))
        img1, H, scale = homography_adaption(img0)

        if np.random.uniform(0, 1) > 0.5:
            # swap two images
            img = img0
            img0 = img1
            img1 = img
            H = np.linalg.inv(H)
            h, w = img1.shape[:2]
            scale = compute_scale(np.linalg.inv(H), h, w)
            scale = 1. / scale

        # the pixel scale change of img0 to img1 for each pixel in img0
        h, w = img0.shape[:2]
        left_scale = compute_scale(H, h, w)
        left_scale = 1. / left_scale

        pix_pos0, pix_pos1 = sample_ground_truth(img0, H)

        # img = draw_corspd_region(img0, img1, H)
        # import matplotlib.pyplot as plt
        # plt.imshow(np.concatenate([np.concatenate([img, draw_scale(scale)], axis=1), draw_scale(left_scale)], axis=1))
        # plt.show()

        # img = draw_kps(img0, pix_pos0, img1, pix_pos1)
        # import matplotlib.pyplot as plt
        # _, (ax1, ax2) = plt.subplots(1, 2)
        # ax1.imshow(img)
        # ax2.imshow(msk)
        # plt.show()

        if self.transforms is not None:
            img0, pix_pos0 = self.transforms(img0, pix_pos0)
            img1, pix_pos1 = self.transforms(img1, pix_pos1)

        pix_pos2 = sample_negative(img1, pix_pos1)

        img0 = torch.tensor(img0).permute(2, 0, 1).float()
        img1 = torch.tensor(img1).permute(2, 0, 1).float()

        pix_pos0 = torch.tensor(pix_pos0).float()
        pix_pos1 = torch.tensor(pix_pos1).float()
        target = dict(kps0=pix_pos0,
                      kps1=pix_pos1,
                      kps2=pix_pos2,
                      H=H,
                      scale=scale,
                      left_scale=left_scale)

        return img0, img1, target, index
Пример #3
0
    def __getitem__(self, index):
        img_path = self.img_paths[index]
        # H_path = self.H_paths[index]
        H_path = np.random.choice(self.H_paths)
        img = io.imread(img_path)
        img = convert_to_rgb(img)

        # img0 = img
        h, w = (240, 240)
        img0 = cv2.resize(img.copy(), (w, h), interpolation=cv2.INTER_LINEAR)
        # img0 = random_crop(img, size=(240, 240))
        H = read_H(H_path, img0)
        img1, scale = homography_adaption(img0, H)

        # the pixel scale change of img0 to img1 for each pixel in img0
        h, w = img0.shape[:2]
        left_scale = compute_scale(H, h, w)
        left_scale = 1. / left_scale

        pix_pos0, pix_pos1 = sample_ground_truth(img0, H)
        _, msk = get_homography_correspondence(h, w, np.linalg.inv(H))
        msk = msk.astype(np.float32)

        # img = draw_corspd_region(img0, img1, H)
        # import matplotlib.pyplot as plt
        # plt.imshow(np.concatenate([img, draw_scale(scale)], axis=1))
        # plt.show()

        # img = draw_kps(img0, pix_pos0, img1, pix_pos1)
        # import matplotlib.pyplot as plt
        # plt.imshow(np.concatenate([img0, img], axis=1))
        # plt.show()

        if self.transforms is not None:
            img0, pix_pos0 = self.transforms(img0, pix_pos0)
            img1, pix_pos1 = self.transforms(img1, pix_pos1)

        pix_pos2 = sample_negative(img1, pix_pos1)

        img0 = torch.tensor(img0).permute(2, 0, 1).float()
        img1 = torch.tensor(img1).permute(2, 0, 1).float()

        pix_pos0 = torch.tensor(pix_pos0).float()
        pix_pos1 = torch.tensor(pix_pos1).float()
        scale = torch.tensor(scale).unsqueeze(0)
        left_scale = torch.tensor(left_scale).unsqueeze(0)
        msk = torch.tensor(msk).unsqueeze(0)
        target = dict(kps0=pix_pos0,
                      kps1=pix_pos1,
                      kps2=pix_pos2,
                      H=H,
                      scale=scale,
                      msk=msk,
                      left_scale=left_scale)

        return img0, img1, target, index
Пример #4
0
    def __getitem__(self, index):
        img_path = self.img_paths[index]
        img = io.imread(img_path)
        img = convert_to_rgb(img)

        h, w = (240, 240)
        img0 = cv2.resize(img.copy(), (w, h), interpolation=cv2.INTER_LINEAR)
        # img0 = random_crop(img, size=(h, w))
        img1, H, scale = homography_adaption(img0)

        if np.random.uniform(0, 1) > 0.5:
            # swap two images
            img = img0
            img0 = img1
            img1 = img
            H = np.linalg.inv(H)
            h, w = img1.shape[:2]
            scale = compute_scale(np.linalg.inv(H), h, w)
            scale = 1. / scale

        pix_pos0, pix_pos1 = sample_ground_truth(img0, H)
        _, msk = get_homography_correspondence(h, w, np.linalg.inv(H))
        msk = msk.astype(np.float32)

        # img = draw_corspd_region(img0, img1, H)
        # import matplotlib.pyplot as plt
        # print(scale)
        # plt.imshow(np.concatenate([img, draw_scale(scale)], axis=1))
        # plt.show()

        # img = draw_kps(img0, pix_pos0, img1, pix_pos1)
        # import matplotlib.pyplot as plt
        # _, (ax1, ax2) = plt.subplots(1, 2)
        # ax1.imshow(img)
        # ax2.imshow(msk)
        # plt.show()

        if self.transforms is not None:
            img0, pix_pos0 = self.transforms(img0, pix_pos0)
            img1, pix_pos1 = self.transforms(img1, pix_pos1)

        pix_pos2 = sample_negative(img1, pix_pos1)

        img0 = torch.tensor(img0).permute(2, 0, 1).float()
        img1 = torch.tensor(img1).permute(2, 0, 1).float()

        pix_pos0 = torch.tensor(pix_pos0).float()
        pix_pos1 = torch.tensor(pix_pos1).float()
        target = dict(kps0=pix_pos0,
                      kps1=pix_pos1,
                      kps2=pix_pos2,
                      H=H,
                      scale=scale,
                      msk=msk)

        return img0, img1, target, index
Пример #5
0
def read_img(img_path, scale):
    img = convert_to_rgb(io.imread(img_path))
    oh, ow = img.shape[:2]
    if isinstance(scale, int):
        img = resize_max_length(img, scale)
    elif isinstance(scale, tuple):
        img = cv2.resize(img, scale)
    h, w = img.shape[:2]
    scale_h, scale_w = h / oh, w / ow
    return img, scale_h, scale_w
Пример #6
0
    def __getitem__(self, index):
        img_path = self.img_paths[index]
        img = io.imread(img_path)
        img = convert_to_rgb(img)

        img0 = img
        # img0 = random_crop(img, size=(240, 320))
        img1, H, patch_ratio = homography_adaption(img0, angle=self.angle)
        pix_pos0, pix_pos1 = sample_ground_truth(img0, H)

        # img = draw_corspd_region(img0, img1, H)
        # import matplotlib.pyplot as plt
        # plt.imshow(img)
        # plt.show()

        if self.transforms is not None:
            img0, pix_pos0 = self.transforms(img0, pix_pos0)
            img1, pix_pos1 = self.transforms(img1, pix_pos1)

        pix_pos2 = sample_negative(img1, pix_pos1)

        img0 = torch.tensor(img0).permute(2, 0, 1).float()
        img1 = torch.tensor(img1).permute(2, 0, 1).float()

        # img = draw_triplet(img0, pix_pos0, img1, pix_pos1, img1, pix_pos2)
        # import matplotlib.pyplot as plt
        # plt.imshow(img)
        # plt.show()
        # return

        if patch_ratio > 1. / np.sqrt(2):
            scale = 1
        else:
            scale = 2

        pix_pos0 = torch.tensor(pix_pos0).float()
        pix_pos1 = torch.tensor(pix_pos1).float()
        target = dict(kps0=pix_pos0,
                      kps1=pix_pos1,
                      kps2=pix_pos2,
                      H=H,
                      scale=scale)

        return img0, img1, target, index
Пример #7
0
    def __getitem__(self, index):
        img_path = self.img_paths[index]
        img = io.imread(img_path)
        img = convert_to_rgb(img)

        img0 = img
        # let p, p' be corresponding points in the two images, then
        # area[p'] / area[p] = scale[p']
        # img0 = random_crop(img, size=(240, 320))
        img1, H, scale = homography_adaption(img0)
        pix_pos0, pix_pos1 = sample_ground_truth(img0, H)

        if self.transforms is not None:
            img0, pix_pos0 = self.transforms(img0, pix_pos0)
            img1, pix_pos1 = self.transforms(img1, pix_pos1)

        pix_pos2 = sample_negative(img1, pix_pos1)

        img0 = torch.tensor(img0).permute(2, 0, 1).float()
        img1 = torch.tensor(img1).permute(2, 0, 1).float()

        # img = draw_triplet(img0, pix_pos0, img1, pix_pos1, img1, pix_pos2)
        # import matplotlib.pyplot as plt
        # plt.imshow(img)
        # plt.show()
        # return

        pix_pos0 = torch.tensor(pix_pos0).float()
        pix_pos1 = torch.tensor(pix_pos1).float()

        # switch image so that the second is smaller
        h, w = img1.size()[1:]
        img0, img1 = img1, img0
        pix_pos0, pix_pos1 = pix_pos1, pix_pos0
        H = np.linalg.inv(H)
        scale = 1.0 / compute_scale(np.linalg.inv(H), h, w)
        target = dict(kps0=pix_pos0,
                      kps1=pix_pos1,
                      kps2=pix_pos2,
                      H=H,
                      scale=scale)

        return img0, img1, target, index
    def __getitem__(self, index):
        img_path = self.img_paths[index]
        img = io.imread(img_path)
        img = convert_to_rgb(img)

        # img0 = img
        img0 = resize_max_length(img, 640)
        img1, H = homography_adaption(img0, angle=self.angle)
        pix_pos0, pix_pos1 = sample_ground_truth(img0, H)

        # img = draw_corspd_region(img0, img1, H)
        # import matplotlib.pyplot as plt
        # plt.imshow(img)
        # plt.show()

        if self.transforms is not None:
            img0, pix_pos0 = self.transforms(img0, pix_pos0)
            img1, pix_pos1 = self.transforms(img1, pix_pos1)

        pix_pos2 = sample_negative(img1, pix_pos1)

        img0 = torch.tensor(img0).permute(2, 0, 1).float()
        img1 = torch.tensor(img1).permute(2, 0, 1).float()

        # img = draw_triplet(img0, pix_pos0, img1, pix_pos1, img1, pix_pos2)
        # import matplotlib.pyplot as plt
        # plt.imshow(img)
        # plt.show()
        # return

        pix_pos0 = torch.tensor(pix_pos0).float()
        pix_pos1 = torch.tensor(pix_pos1).float()
        target = dict(
            kps0=pix_pos0,
            kps1=pix_pos1,
            kps2=pix_pos2,
            H=H,
        )

        return img0, img1, target, index