如何修改/分配矩阵元素值
How to modify/assign a Matrix element value?
这段代码给了我错误。程序"停止工作"。我可以知道出了什么问题吗?我做得对吗?
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]
相关文章:
- 访问动态分配列表中的元素
- 如何为 std::vector 分配内存,然后稍后为某些元素调用构造函数?
- 动态分配列表 - 创建一个函数,用于删除所有包含偶数值的元素
- 是否有具有外部元素分配的序列容器(在 STL 中)?
- C++ 将元素分配给映射值时访问错误
- 如何通过查找迭代器结果分配给一组对的元素
- C++通过别名指针以静默方式将错误的类型分配给数组元素
- c++ 使用动态分配运算符反向数组元素
- 为什么在 c++ 中分配 char 数组元素时,分配的字符会被销毁?
- 我正在丢失动态分配的数组中的元素
- 如何为包含最多N个元素的std::multiset调用最大数量分配
- 通过动态分配插入列表元素
- 在 CPP 中将元素分配给矢量
- 如何将 50 行和 50 列的二维数组编码为一个为元素随机分配星号的函数?
- 当我尝试将一个向量元素的值分配给另一个向量元素时,为什么我的应用程序会崩溃
- 如何将元素分配给向量?
- 不能直接分配元素 std::array,它说没有运算符"="匹配
- 为什么存储在 STL 向量中的堆栈分配元素C++即使超出范围也仍然处于活动状态
- 在数组中分配元素
- 如何初始化一个固定大小的数组,并在C++11中使用constexpr函数或借助boost来分配元素