简体   繁体   中英

How to assign values to a 3 dimensional array in opencv

I want to assign value to a 3 dimensional array in opencv but don't know how to do it.

here is the code in matlab that I want to write in opencv

vv = zeros(800,600,2);

for j1=1:m1 
  for j2=1:m2
        w=[-k;vv(j1,j2,1);vv(j1,j2,2)];
        w=w/norm(w);
  end
end

and this is what I did in opencv, but did not work

int dim2[3] = {800,600,2};
    Mat vv(3,dim2,CV_32F,Scalar::all(0));

    for(int j1 = 0; j1 < 800; j1++)
         { for(int j2 = 0; j2 < 600; j2++)
             {

                   Mat w(3,dim2,CV_32F, Scalar(1,vv(j1,j2,1),vv(j1,j2,2)));

              }
          }

use the following syntax:

//initizlizes a matrix zeros, of size 800x600x2
cv::Mat vv = cv::Mat::zeros(cv::Size(600, 800), CV_32FC2);

//do some calculations on vv

//opencv version of the for loop
for (int y = 0; y < vv.rows; y++)
{
    for (int x = 0; x < vv.cols; x++)
    {

        //access indices (y,x,1) and (y,x,2)
        cv::Vec2f wVec = vv.at<cv::Vec2f>(cv::Point(x, y));
        //calculates the norm
        cv::Point3f w(3, wVec[0], wVec[1]);
        double normW = cv::norm(w);
        //divides w by it's norm. don't forget to verify that normW is not 0
        w = w / normW;

        //do something with the calculated w vector

    }

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM