一、at方法
获取单通道像素:int pv = image.at<uchar>(row, col);
获取三通道像素:Vec3b bgr = image.at<Vec3b>(row, col);
for (int row = 0; row < h; row++)
{
for (int col = 0; col < w; col++)
{
Vec3b& v = image.at<Vec3b>(row,col);
v[0] = 255 - v[0];
v[1] = 255 - v[1];
v[2] = 255 - v[2];
}
}
二、ptr指针
uchar* current_row = image.ptr<uchar>(row);
for (int row = 0; row < h; row++)
{
uchar* cur = image.ptr<uchar>(row);
for (int col = 0; col < w; col++)
{
if (dims == 1) {
*cur++ = 255 - *cur;
}
if (dims == 3) {
*cur++ = 255 - *cur;
*cur++ = 255 - *cur;
*cur++ = 255 - *cur;
}
}
}
三、迭代器
Mat_<Vec3b>::iterator it = image.begin<Vec3b>(); 不能使用 Mat_<uchar>::iterator it
for (Mat_<Vec3b>::iterator it = image.begin<Vec3b>(); it != image.end<Vec3b>(); ++it)
{
Vec3b& cc= (*it);
cc[0] = 255 - cc[0];//蓝色通道置反色;
cc[1] = 255 - cc[1];//蓝色通道置反色;
cc[2] = 255 - cc[2];//蓝色通道置反色;
}
从个人执行的时间分析,迭代器效率最低,指针效率最高,因为它少了Vec3b转换。