成员rows
并cols
告诉您矩阵的行数和列数。要访问它们,您可以使用at
:
// let's assume your image is CV_8U, so that it is accessed with unsigned char
cv::Mat m = ...;
for(int r = 0; r < m.rows; ++r)
{
for(int c = 0; c < m.cols; ++c)
{
char byte = m.at<unsigned char>(r, c);
...
}
}
您还可以at
通过指针为每一行保存一些调用和访问数据:
// let's assume your image is CV_8U, so that it is accessed with unsigned char
cv::Mat m = ...;
for(int r = 0; r < m.rows; ++r)
{
const unsigned char *p = m.ptr<unsigned char>();
for(int c = 0; c < m.cols; ++c, ++p)
{
char byte = *p;
...
}
}
如果矩阵在内存中是连续的,则可以只获取一次指针:
// let's assume your image is CV_8U, so that it is accessed with unsigned char
cv::Mat m = ...;
assert(m.isContinuous());
const unsigned char *p = m.ptr<unsigned char>();
for(int r = 0; r < m.rows; ++r)
{
for(int c = 0; c < m.cols; ++c, ++p)
{
char byte = *p;
...
}
}