Ejemplo n.º 1
0
    def __call__(self, batch):
        nir_image_batch, rgb_image_batch, theta_batch, image_name = batch[
            'nir_image'], batch['rgb_image'], batch['theta'], batch['name']
        if self.use_cuda:
            nir_image_batch = nir_image_batch.cuda()
            rgb_image_batch = rgb_image_batch.cuda()
            theta_batch = theta_batch.cuda()

        b, c, h, w = nir_image_batch.size()

        # 为较大的采样区域生成对称填充图像
        rgb_image_batch_pad = symmetricImagePad(rgb_image_batch,
                                                self.padding_factor,
                                                use_cuda=self.use_cuda)
        nir_image_batch_pad = symmetricImagePad(nir_image_batch,
                                                self.padding_factor,
                                                use_cuda=self.use_cuda)

        # convert to variables 其中Tensor是原始数据,并不知道梯度计算等问题,
        # Variable里面有data,grad和grad_fn,其中data就是Tensor
        # image_batch = Variable(image_batch, requires_grad=False)
        # theta_batch = Variable(theta_batch, requires_grad=False)

        # indices_R = torch.tensor([choice(self.channel_choicelist)])
        # indices_G = torch.tensor([choice(self.channel_choicelist)])

        # indices_R = torch.tensor([1])
        # indices_G = torch.tensor([0])
        #
        # if self.use_cuda:
        #     indices_R = indices_R.cuda()
        #     indices_G = indices_G.cuda()
        #
        # rgb_image_batch_pad = torch.index_select(rgb_image_batch_pad, 1, indices_R)
        # nir_image_batch_pad = torch.index_select(nir_image_batch_pad, 1, indices_G)

        # 获取裁剪的图像
        cropped_image_batch = self.rescalingTnf(
            rgb_image_batch_pad, None, self.padding_factor,
            self.crop_factor)  # Identity is used as no theta given
        # 获取裁剪变换的图像
        warped_image_batch = self.geometricTnf(
            nir_image_batch_pad, theta_batch, self.padding_factor,
            self.crop_factor)  # Identity is used as no theta given

        return {
            'source_image': cropped_image_batch,
            'target_image': warped_image_batch,
            'theta_GT': theta_batch,
            'name': image_name
        }
def save_matlab_pic(image_data, theta_aff):
    image_batch = torch.from_numpy(image_data).transpose(1, 2).transpose(
        0, 1).unsqueeze(1).float()
    vis.showImageBatch(image_batch,
                       win='image_batch',
                       title='raw_image_batch',
                       start_index=16)

    crop_factor = 9 / 16
    padding_factor = 0.6
    padding_image_batch = symmetricImagePad(image_batch,
                                            padding_factor=padding_factor)
    affTnf = AffineTnf(240, 240, use_cuda=False)
    # 变换以后超出范围自动变为0
    source_image_batch = affTnf(padding_image_batch, None, padding_factor,
                                crop_factor)
    target_image_batch = affTnf(padding_image_batch, theta_aff, padding_factor,
                                crop_factor)

    vis.showImageBatch(source_image_batch,
                       win='source_image_batch',
                       title='source_image_batch',
                       start_index=16)
    vis.showImageBatch(target_image_batch,
                       win='target_image_batch',
                       title='target_image_batch',
                       start_index=16)

    save_image_tensor(source_image_batch[16], 'mul_1s_s.png')
    save_image_tensor(target_image_batch[16], 'mul_1t_s.png')
def get_image_information(image_dir, image_name, label_path, vis):
    image_path = os.path.join(image_dir, image_name)

    image_np = io.imread(image_path)
    csv_data = read_csv_file(label_path)

    label_row_param = csv_data.loc[csv_data['image'] == image_name].values
    label_row_param = np.squeeze(label_row_param)

    if image_name != label_row_param[0]:
        raise ValueError("图片文件名和label图片文件名不匹配")

    theta_aff = torch.from_numpy(label_row_param[1:].reshape(2, 3).astype(
        np.float32)).unsqueeze(0)

    image_batch = torch.from_numpy(image_np).transpose(1, 2).transpose(
        0, 1).unsqueeze(0).float()

    vis.showImageBatch(image_batch, win='image_batch', title='raw_image_batch')

    crop_factor = 9 / 16
    padding_factor = 0.6
    # crop_factor = 3
    # padding_factor = 0.9

    padding_image_batch = symmetricImagePad(image_batch,
                                            padding_factor=padding_factor)

    affTnf = AffineTnf(446, 640, use_cuda=False)

    # 变换以后超出范围自动变为0
    source_image_batch = affTnf(padding_image_batch, theta_aff, padding_factor,
                                crop_factor)
    target_image_batch = affTnf(padding_image_batch, None, padding_factor,
                                crop_factor)

    # inverse_theta_aff = inverse_theta(theta_aff,use_cuda=False)
    warped_image_batch = affTnf(target_image_batch,
                                theta_aff,
                                crop_factor=1,
                                padding_factor=1)

    vis.showImageBatch(source_image_batch,
                       win='source_image_batch',
                       title='source_image_batch')
    vis.showImageBatch(target_image_batch,
                       win='target_image_batch',
                       title='target_image_batch')
    vis.showImageBatch(warped_image_batch,
                       win='warped_image_batch',
                       title='warped_image_batch')

    # save_image_tensor(image_batch,'raw.jpg')
    save_image_tensor(source_image_batch, 'source.jpg')
    save_image_tensor(target_image_batch, 'target.jpg')
    save_image_tensor(warped_image_batch, 'warped2.jpg')
    def __call__(self, batch):
        image_batch, theta_batch, image_name = batch['image'], batch[
            'theta'], batch['name']
        indices_R = torch.tensor([0])
        indices_G = torch.tensor([2])
        if self.use_cuda:
            image_batch = image_batch.cuda()
            theta_batch = theta_batch.cuda()
            indices_R = indices_R.cuda()
            indices_G = indices_G.cuda()

        # 对图像边缘进行镜像填充
        image_batch = symmetricImagePad(image_batch,
                                        self.padding_factor,
                                        use_cuda=self.use_cuda)

        # 获得单通道图片,沿着指定维度对输入进行切片,取index中指定的相应项(index为一个LongTensor),
        # 然后返回到一个新的张量, 返回的张量与原始张量_Tensor_有相同的维度(在指定轴上)。

        image_batch_R = torch.index_select(image_batch, 1, indices_R)
        image_batch_G = torch.index_select(image_batch, 1, indices_G)

        # 原始图像R通道缩放
        original_image_batch = self.affineTnf(image_batch_R, None,
                                              self.padding_factor,
                                              self.crop_factor)
        wraped_image_batch = self.affineTnf(image_batch_G, theta_batch,
                                            self.padding_factor,
                                            self.crop_factor)

        pair_result = {
            'source_image': original_image_batch,
            'target_image': wraped_image_batch,
            'theta_GT': theta_batch,
            'name': image_name
        }

        return pair_result
Ejemplo n.º 5
0
    def __call__(self, batch):
        image_batch, theta_batch,image_name = batch['image'], batch['theta'],batch['name']
        if self.use_cuda:
            image_batch = image_batch.cuda()
            theta_batch = theta_batch.cuda()

        b, c, h, w = image_batch.size()

        # 为较大的采样区域生成对称填充图像
        image_batch = symmetricImagePad(image_batch, self.padding_factor,use_cuda=self.use_cuda)

        # indices_R = torch.tensor([choice(self.channel_choicelist)])
        # indices_G = torch.tensor([choice(self.channel_choicelist)])

        indices_R = torch.tensor([0])
        indices_G = torch.tensor([2])
        #
        if self.use_cuda:
            indices_R = indices_R.cuda()
            indices_G = indices_G.cuda()

        image_batch_R = torch.index_select(image_batch, 1, indices_R)
        image_batch_G = torch.index_select(image_batch, 1, indices_G)

        image_batch_R = torch.cat((image_batch_R,image_batch_R,image_batch_R),1)
        image_batch_G = torch.cat((image_batch_G,image_batch_G,image_batch_G),1)

        # 获取裁剪的图像
        cropped_image_batch = self.rescalingTnf(image_batch_R, None, self.padding_factor,
                                                self.crop_factor)  # Identity is used as no theta given
        # 获取裁剪变换的图像
        warped_image_batch = self.geometricTnf(image_batch_G, theta_batch,
                                               self.padding_factor,
                                               self.crop_factor)  # Identity is used as no theta given

        return {'source_image': cropped_image_batch, 'target_image': warped_image_batch, 'theta_GT': theta_batch,
                'name':image_name}