예시1)
Mat이 1채널인 경우
int main(int argc, const char** argv)
{
Mat A(3, 3, CV_32F);
for (int i = 0; i < A.rows; i++)
{
float* ptrA = A.ptr<float>(i); //ptr는 선언하면서 행렬의 row의 시작주소를 할당한다.
for (int j = 0; j < A.cols ; j++)
{
ptrA[j] = i * A.cols + j; // row안에 원소에 접근하는 법은 행렬의 원소에 접근하는 법과 동일
}
}
cout << A << endl;
}
Mat이 3채널(이미지)인 경우
Mat B(4, 4, CV_32FC3);
for (int i = 0; i < B.rows; i++)
{
Vec3f* ptrB = B.ptr<Vec3f>(i);
for (int j = 0; j < B.cols; j++)
{
ptrB[j] = Vec3f(255, 0, i* B.cols + j);
}
}
cout << B << endl;