gpt4 book ai didi

c++ - 如何在 opencv 中为 3 维数组赋值

转载 作者:行者123 更新时间:2023-11-28 02:08:11 25 4
gpt4 key购买 nike

我想在 opencv 中为 3 维数组赋值,但不知道该怎么做。

这是我想用opencv写的matlab代码

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

这就是我在 opencv 中所做的,但没有奏效

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)));

}
}

最佳答案

使用以下语法:

//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

}

}

关于c++ - 如何在 opencv 中为 3 维数组赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36688754/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com