//读取已有的npy文件
NDArray masks = np.load("I:\\软件\\1.npy");
List<NDArray> ndList = new List<NDArray>();
if (masks==null)
{
}
else
{
//读取npy文件已有的数据
for (int i = 0; i < masks.shape[2]; i++)
{
var roi = masks[$":,:,{i}"];
ndList.Add(roi);
}
}
//roiMatList 是新产生的数据
foreach (Mat mat in roiMatList)
{
//转一维数组
mat.GetArray(out byte[] data);
//将大于1的都变成1
var dataInt = Array.ConvertAll<byte,int>(data, x => x>0?(byte)1:(byte)0);
NDArray nd = new NDArray(dataInt);
//按行和列拆分成二维数组
nd = nd.reshape(ndHeight, ndWidth);
ndList.Add(nd);
}
//将所有的数组合并成新的数组
NDArray appendArray = np.stack(ndList.ToArray());
//转换维度,默认是0 ,1,2,所以要把0放在最后
int[] trans = new int[] { 1,2,0 };
NDArray resultArray = np.transpose(appendArray, trans);
np.save("I:\\软件\\2.npy", resultArray);
1. NDArray 要引入 NumSharp 类库
2. int[] trans = new int[] { 1,2,0 };
NDArray resultArray = np.transpose(appendArray, trans);
transpose是维度转换函数,默认是0 ,1,2,所以要把0放在最后