我想在将图像传递给数据加载器之前对其进行二值化,我创建了一个运行良好的数据集类。但在__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 对象上应用了阈值,因此我需要在之后应用转换为张量对象,但由于某种原因它崩溃了。有人可以帮助我吗?