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

前言

在深度学习中的语义分割任务中,像素值大小代表种类,例如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图像进行训练

代码

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<opencv2/opencv.hpp>

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 << img.size << endl;
	cout << img.channels() << endl; // [256,256,1]
	/*imshow("w", img);
	waitKey();*/

	for (int j = 0; j < img.rows; j++) {
		uchar* p = img.ptr<uchar>(j);
		for (int i = 0; i < img.cols; i++) {
				if (int(p[i]) != 0) {
					p[i] = 1;
			}
		}
	}

	imshow("w", img);
	waitKey();
	imwrite("re.png", img);

	return 0;
}

结果

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

显示

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

import matplotlib.pyplot as plt
from PIL import Image

if __name__ == '__main__':
    re = Image.open('re.png')
    plt.imshow(re,cmap='gray')
    plt.show()

在这里插入图片描述

  • 8
    点赞
  • 51
    收藏
    觉得还不错? 一键收藏
  • 23
    评论
以下是一个简单的二值图像利用bp神经网络进行图像边缘提取的Python代码示例: ``` import numpy as np import matplotlib.pyplot as plt # 生成二值图像 img = np.zeros((100, 100)) img[20:80, 20:80] = 1 # 构建训练数据 X = [] Y = [] for i in range(1, 99): for j in range(1, 99): X.append([img[i-1,j-1], img[i-1,j], img[i-1,j+1], img[i,j-1], img[i,j], img[i,j+1], img[i+1,j-1], img[i+1,j], img[i+1,j+1]]) Y.append(img[i,j]) X = np.array(X) Y = np.array(Y) # 构建神经网络 class NeuralNetwork: def __init__(self): self.inputSize = 9 self.outputSize = 1 self.hiddenSize = 10 self.W1 = np.random.randn(self.inputSize, self.hiddenSize) self.W2 = np.random.randn(self.hiddenSize, self.outputSize) def forward(self, X): self.z = np.dot(X, self.W1) self.z2 = self.sigmoid(self.z) self.z3 = np.dot(self.z2, self.W2) o = self.sigmoid(self.z3) return o def sigmoid(self, s): return 1 / (1 + np.exp(-s)) def sigmoidPrime(self, s): return s * (1 - s) def backward(self, X, Y, o): self.o_error = Y - o self.o_delta = self.o_error * self.sigmoidPrime(o) self.z2_error = self.o_delta.dot(self.W2.T) self.z2_delta = self.z2_error * self.sigmoidPrime(self.z2) self.W1 += X.T.dot(self.z2_delta) self.W2 += self.z2.T.dot(self.o_delta) def train(self, X, Y): o = self.forward(X) self.backward(X, Y, o) # 训练神经网络 NN = NeuralNetwork() for i in range(100): NN.train(X, Y) # 对图像进行边缘提取 edgeImg = np.zeros((100, 100)) for i in range(1, 99): for j in range(1, 99): x = [img[i-1,j-1], img[i-1,j], img[i-1,j+1], img[i,j-1], img[i,j], img[i,j+1], img[i+1,j-1], img[i+1,j], img[i+1,j+1]] edgeImg[i,j] = NN.forward(np.array(x))[] # 显示结果 plt.subplot(1, 2, 1) plt.imshow(img, cmap='gray') plt.title('Original Image') plt.subplot(1, 2, 2) plt.imshow(edgeImg, cmap='gray') plt.title('Edge Image') plt.show() ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 23
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值