import time start_time = datetime.datetime.now() print("start preprocessing..................") n = 10000 (X_train, train_labels) = rm.load_mnist('E:\design project\wenquxing22a-master\sw\swSim-mnist\scripts', kind='train') otsu_imgs = [] images = [] for (k, j) in zip(range(n),range(1, n + 1)): o = sf.OTSU(X_train[k].reshape([28,28])) otsu_imgs.append(o.Otsu()) sf.processBar(j, n, "Otsuing") print("") # image normalization for (img, i) in zip(otsu_imgs, range(1, n + 1)): images.append(sf.deskew(img / 255.0)) sf.processBar(i, n, "Normalization") print("") images = np.asarray(images) np.save("E:/design project/wenquxing22a-master/sw/swSim-mnist/scripts/result date/nomalizedData_after_OTSU.npy", images) np.save("E:/design project/wenquxing22a-master/sw/swSim-mnist/scripts/result date/label.npy", train_labels) otsu_imgs_test = [] X_test, test_label = rm.load_mnist(r'E:\design project\wenquxing22a-master\sw\swSim-mnist\scripts',kind='t10k') testimages = [] for (k, j) in zip(range(n),range(1, n + 1)): o = sf.OTSU(X_test[k].reshape([28,28])) otsu_imgs_test.append(o.Otsu()) sf.processBar(j, n, "Otsuing") print("") # image normalization for img in otsu_imgs_test: testimages.append(sf.deskew(img / 255.0)) #soft_test = sf.imgSoft(testimages, soft_test, 0.80) #soft_test = np.asarray(soft_test) np.save("E:/design project/wenquxing22a-master/sw/swSim-mnist/scripts/result date/nomalizedDataTest.npy", testimages) np.save("E:/design project/wenquxing22a-master/sw/swSim-mnist/scripts/result date//label_test.npy",test_label) end_time = datetime.datetime.now() print("done!") print("running time: ", end_time - start_time)
时间: 2024-04-03 07:36:57 浏览: 97
这段代码是用来对 MNIST 数据集进行预处理的。首先,通过调用 `rm.load_mnist()` 函数加载 MNIST 数据集,然后使用 Otsu 算法进行图像二值化处理,接着对图像进行归一化处理(即去除图像中的旋转和偏移)。最后,将处理好的图像数据和对应的标签保存到 Numpy 数组中。其中,函数的输入参数包括数据集的路径和数据集的类型(训练集或测试集),预处理后的图像数据和标签数据会分别保存到不同的文件中。这段代码可以用于对 MNIST 数据集进行预处理,以便在训练和测试模型时使用。
阅读全文