Ask Your Question
0

How to take out certain 2D matrix from 3D Mat matrix?

asked 2017-01-18 10:09:05 -0500

navia gravatar image

I have created a 3D matrix like this: int sizes[] = { 3, vec.size().height, vec.size().width }; Mat *zeta = new Mat(3, sizes, CV_64FC1, cv::Scalar(0)); now i d like to get zeta[0,:,:] for example I searched online, but have not found any function to do this. Anyone has the idea? Thanks!

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2017-01-18 12:13:34 -0500

LBerger gravatar image

updated 2017-01-18 12:18:12 -0500

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;
}
edit flag offensive delete link more

Comments

ok! thanks~

navia gravatar imagenavia ( 2017-01-18 16:39:54 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower

Stats

Asked: 2017-01-18 10:09:05 -0500

Seen: 226 times

Last updated: Jan 18 '17