将[0,255]的二值图像转成可以训练的[0,1]二值图像 您所在的位置:网站首页 黑白相机的像素深度为0~255 将[0,255]的二值图像转成可以训练的[0,1]二值图像

将[0,255]的二值图像转成可以训练的[0,1]二值图像

2024-01-24 08:24| 来源: 网络整理| 查看: 265

前言

在深度学习中的语义分割任务中,像素值大小代表种类,例如0为背景, 1 , 2 , 3 , . . . n 1,2,3,...n 1,2,3,...n代表了各个种类。因此如果下载的二分类语义分割数据集图像是 0 , 255 0,255 0,255的二值图像的话,就需要存成 0 , 1 0,1 0,1图像进行训练

文章目录 前言代码cpp参考结果显示

代码 from PIL import Image import numpy as np import os if __name__ == '__main__': work_dir = "Test_GroundTruth" # 图像所处文件夹 file_names = os.listdir(work_dir) for file_name in file_names: # print(file_name) # ISIC_0000000_Segmentation.png file_path = os.path.join(work_dir,file_name) image = Image.open(file_path) img = np.array(image) img[img==255] = 1 # 重新保存 image = Image.fromarray(img,'L') new_name = file_name[:-4] new_name = new_name.strip("_Segmentation") # 文件名处理成和图像一样的名字 image.save(f'{new_name}.png') cpp参考 #include using namespace std; using namespace cv; int main() { Mat img = imread( "E:/download/custom_dataset/labels_0-255/ISIC_0000000_segmentation_0.jpg",0); cout if (int(p[i]) != 0) { p[i] = 1; } } } imshow("w", img); waitKey(); imwrite("re.png", img); return 0; } 结果

在这里插入图片描述 在这里插入图片描述

显示

因为opencv的显示不能自动放缩,所以看起来是全黑的,这个时候我们用python的matplotlib包来显示保存下来的文件看看

import matplotlib.pyplot as plt from PIL import Image if __name__ == '__main__': re = Image.open('re.png') plt.imshow(re,cmap='gray') plt.show()

在这里插入图片描述



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有