如何修改/分配Matrix元素值?

时间:2022-10-28 16:11:13

This code gave me error. The program "stopped working". May I know what's wrong? Am i doing it right?

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

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++;
}

1 个解决方案

#1


1  

This is how you can access/modify matrix elements. You can build further on this base program to fill the matrix with the values you want:

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

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;

this gives:

这给了:

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

#1


1  

This is how you can access/modify matrix elements. You can build further on this base program to fill the matrix with the values you want:

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

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;

this gives:

这给了:

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