使用 pytorch 应用简单的转换来获取二值图像

问题描述

我想在将图像传递给数据加载器之前对其进行二值化,我已经创建了一个运行良好的数据集类。但在 __getitem__() 方法中,我想对图像设置阈值:

    def __getitem__(self,idx):
        # Open image,apply transforms and return with label
        img_path = os.path.join(self.dir,self.filelist[filename"])
        image = Image.open(img_path)
        label = self.x_data.iloc[idx]["label"]

        # Applying transformation to the image
        if self.transforms is not None:
           image = self.transforms(image)

        # applying threshold here:
        my_threshold = 240
        image = image.point(lambda p: p < my_threshold and 255)
        image = torch.tensor(image)

        return image,label

然后我尝试调用数据集:

    data_transformer = transforms.Compose([
        transforms.Resize((10,10)),transforms.Grayscale()
        //transforms.ToTensor()
    ])

train_set = MyNewDataset(data_path,data_transformer,rows_train)

因为我已经在 PIL 对象上应用了阈值,所以我需要在转换后应用到张量对象,但由于某种原因它崩溃了。有人可以帮我吗?

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)