Opencv is not ready for 3D Mat. You can use this first :
Mat y=Mat::zeros(5,5,CV_32FC(n));
I think it is easyest answer to 3D Mat
You can try this code too:
int main(int argc, char* argv[])
{
int planSize[] = { 50,100,200 };
Mat voxel(3,planSize,CV_8UC1);
cout << "Size 0 = " << voxel.size[0] << "\n";
cout << "Size 1 = " << voxel.size[1] << "\n";
cout << "Size 2 = " << voxel.size[2] << "\n";
for (int i = 0; i<voxel.size[0]; i ++)
for (int j = 0; j<voxel.size[1]; j ++)
for (int k = 0; k<voxel.size[2]; k ++)
voxel.at<uchar>(i,j,k)=saturate_cast<uchar>(abs(i-voxel.size[0]/2)+abs(j-voxel.size[1]/2)+abs(k-voxel.size[2]/2));
bool fin=false;
int indPlan=0;
Range rangesVoxel[] = { Range(0,voxel.size[0]), Range(0,voxel.size[1]),Range(0,voxel.size[2]) };
for (;!fin;)
{
rangesVoxel[0] = cv::Range(indPlan,indPlan + 1);
Mat sliceTmp;
sliceTmp= voxel(rangesVoxel).clone();
Mat finalSlice(2, &(voxel.size[1]), voxel.type());
sliceTmp.copySize(finalSlice);
putText(sliceTmp, format("Plane %d", indPlan),Point(10, 10), FONT_HERSHEY_SIMPLEX,1,Scalar(255));
imshow("Plane Histo", sliceTmp);
cout << mean(sliceTmp) << "\n";
char c=waitKey();
switch (c) {
case 27:
fin=true;
break;
case '+':
indPlan = (indPlan++) % (voxel.size[0]-1);
break;
case '-':
indPlan = (indPlan--) % (voxel.size[0]-1);
break;
}
}
return 0;
}