灰度图单通道实现:
for (int i = 0; i < srcImg.rows; i++)
{
uchar *srcData = srcImg.ptr<uchar>(i);
for (int j = 0; j < srcImg.cols; j++)
{
dstImg.at<uchar>(i, j) = srcData[j] * k + b;
}
}
彩色图的实现只需拓展到三通道即可:
for (int i = 0; i < RowsNum; i++)
{
for (int j = 0; j < ColsNum; j++)
{
//c为遍历图像的三个通道
for (int c = 0; c < 3; c++)
{
//使用at操作符,防止越界
dstImg.at<Vec3b>(i, j)[c] = saturate_cast<uchar>
(k* (srcImg.at<Vec3b>(i, j)[c]) + b);
}
}
}
注意:row == heigh == Point.y ; col == width == Point.x ; Mat::at(Point(x, y)) == Mat::at(y,x)