方法一:
点击查看代码
import scipy.io
import numpy as np
import cv2
import os
input_folder = 'D:\daima\CrackForest-dataset-master\CrackForest-dataset-master\groundTruth'
#输出文件夹路径
output_folder = 'D:\daima\CrackForest-dataset-master\CrackForest-dataset-master\png'
# 遍历文件夹中的.mat文件
for filename in os.listdir(input_folder):
if filename.endswith('.mat'):
print(filename)
# 构建完整的文件路径
mat_filepath = os.path.join(input_folder, filename)
# 从.mat文件中加载数据
mat = scipy.io.loadmat(mat_filepath)
np_seg = mat['groundTruth'][0][0][0]
(y, x) = np.where(np_seg == 2)
np_seg[y, x] = 255
(y, x) = np.where(np_seg == 1)
np_seg[y, x] = 0
cv2.imwrite("{}\{}{}".format(output_folder,filename[:-4],'.png'), np_seg)
方法二中只需加入一个for循环即可达到与法一一样的效果
点击查看代码
import scipy.io
import numpy as np
import cv2
mat = scipy.io.loadmat(r'D:\daima\CrackForest-dataset-master\CrackForest-dataset-master\groundTruth\001.mat')
np_seg = mat['groundTruth'][0][0][0]
(y, x) = np.where(np_seg == 2)
np_seg[y, x] = 255
(y, x) = np.where(np_seg == 1)
np_seg[y, x] = 0
cv2.imwrite(r'D:\daima\CrackForest-dataset-master\CrackForest-dataset-master\png.png', np_seg)
print()
浙公网安备 33010602011771号