//读取已有的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的都变成1var 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放在最后