gpt4 book ai didi

c++ - 如何修改/分配矩阵元素值?

转载 作者:行者123 更新时间:2023-11-28 06:57:50 26 4
gpt4 key购买 nike

这段代码给我错误。该程序“停止工作”。我可以知道出了什么问题吗?我做对了吗?

short* pixelX = grad_x.ptr<short>(0);
short* pixelY = grad_y.ptr<short>(0);
cv::Mat grad = cv::Mat::zeros(original_Mat.size(), CV_64F);

cv::Mat grad_x = cv::Mat::zeros(original_Mat.size(), CV_64F);
cv::Mat grad_y = cv::Mat::zeros(original_Mat.size(), CV_64F);
int a=0,b=0;

for(int i = 0; i < grad_x.rows * grad_x.cols; i++)
{
double directionRAD = atan2(pixelY[i], pixelX[i]);
int directionDEG = (int)(180 + directionRAD / CV_PI * 180);
grad.at<double>(a,b)=directionDEG;// this is the one giving me error
if(a%=319)
{
a=0;
b++;
}
else
a++;
}

最佳答案

这是您访问/修改矩阵元素的方式。您可以在此基础程序的基础上进一步构建,以用您想要的值填充矩阵:

cv::Mat grad = cv::Mat::zeros(4, 5, CV_64F);

int a = 0, b = 0;

for (int i = 0; i < grad.rows * grad.cols; i++)
{
grad.at<double>(b, a) = i; // this is the one giving me error
if (a == grad.cols - 1)
{
a = 0;
b++;
}
else
a++;
}
cout << grad << endl;

这给出:

[0, 1, 2, 3, 4;
5, 6, 7, 8, 9;
10, 11, 12, 13, 14;
15, 16, 17, 18, 19]

关于c++ - 如何修改/分配矩阵元素值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22932593/

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