您的位置:首页 > 运维架构

[OpenCV]访问图像中的像素调整对比度亮度

2018-03-08 19:37 295 查看
访问图像中的像素的方法:
1.指针访问:
#为了简化指针运算,Mat类提供了ptr函数可以得到图像任意行的首地址
Mat outputImage = img.clone();
int row = outputImage.rows;
int col = outputImage.cols*outputImage.channels();
for (int i = 0; i < row; i++)
{
uchar* data = outputImage.ptr<uchar>(i);
for (int j = 0; j < col; j++)
{
data[j] = data[j] / 2 +2;
}
} 2.用迭代器操作像素Mat outputImage = img.clone();
Mat_<Vec3b>::iterator it = outputImage.begin<Vec3b>();
Mat_<Vec3b>::iterator itend = outputImage.end<Vec3b>();
for (; it != itend; it++)
{
(*it)[0] = (*it)[0] / 2 + 2;
(*it)[1] = (*it)[1] / 2 + 2;
(*it)[2] = (*it)[2] / 2 + 2;
}3.图像对比度、亮度调整
基本知识:
灰度变换方程:g(x,y) = af(x,y) + b
当a>1时,输出图像的对比度将增大
当a<1时,输出图像的对比度将减小
当a不变,b>0时输出图像的亮度增大
当a不变,b<0时输出图像的亮度减小Mat outputImage = img.clone();
int row = outputImage.rows;
int col = outputImage.cols;
for (int i = 0; i < row; i++)
{
for (int j = 0; j < col; j++)
{
for (int c = 0; c < 3; c++)
{
outputImage.at<Vec3b>(j, i)[c] = 2 * outputImage.at<Vec3b>(j, i)[c] + 2;
}
}
}4、通道分离与合并:
split()函数用来表示通道的分离与合并:split(分离的图像,输出到的容器)
merge()函数用来合并通道:merge(需要被合并的容器,合并到的新图像)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: