【问题标题】:openCv and PyTorch inverser Transform not workingopenCv 和 PyTorch 逆变器不工作
【发布时间】:2021-03-06 11:37:05
【问题描述】:

我有一个只做的转换类:

        if transform is None:
            transform = transforms.Compose([
                transforms.Resize((256, 256)),
                transforms.ToTensor()
            ])
        root = os.path.join(PROJECT_ROOT_DIR, "data")
        super(AttributesDataset, self).__init__()
        self.data = torchvision.datasets.CelebA(
            root=root,
            split=split,
            target_type='attr',
            download=True,
            transform=transform
        )

从文档中,我了解到这仅意味着 0,1 范围内的值按比例缩小,即所有像素值应位于 [0,1] 之间(我也已对此进行了验证)。 我想可视化来自模型的一些输出。因此,我创建了一个简单的方法:-

    for img, label in dataloader:
        img.squeeze_(0)
        # permute the channels. cv2 expects image in format (h, w, c)
        unscaled_img = img.permute(1, 2, 0)
        # move images to cpu and convert to numpy as required by cv2 library
        unscaled_img = torch.round(unscaled_img * 255)
        unscaled_img = unscaled_img.to(torch.uint8)
        # unscaled_img = np.rint(unscaled_img * 255).astype(np.uint8) 
        unscaled_img = cv2.cvtColor(unscaled_img, cv2.COLOR_RGB2BGR)
        cv2.imshow(unscaled_img.numpy())

但是,创建的所有图像都有异常的蓝色阴影。例如,

谁能告诉我我到底做错了什么?非常感谢您的帮助

【问题讨论】:

  • 也许红色被蓝色取代,蓝色被红色取代unscaled_img = cv2.cvtColor(unscaled_img, cv2.COLOR_RGB2BGR)。原图有什么相似之处?
  • 是的,你是对的。我检查了它并注释掉该行解决了这个问题。谢谢
  • 我很高兴问题已得到解决。

标签: numpy pytorch opencv-python


【解决方案1】:

由@LajosArpad 评论解决。罪魁祸首是

unscaled_img = cv2.cvtColor(unscaled_img, cv2.COLOR_RGB2BGR)

删除它会产生正确的值。

【讨论】:

    猜你喜欢
    • 2021-10-26
    • 1970-01-01
    • 2023-04-01
    • 1970-01-01
    • 2017-06-25
    • 1970-01-01
    • 1970-01-01
    • 2021-03-20
    • 1970-01-01
    相关资源
    最近更新 更多