I think something like this could work. You should check carrefully this answer...
#include "opencv2/opencv.hpp"
#include <iostream>
using namespace cv;
using namespace std;
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();
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;
}
}
}
You can create that class