本文来自opencv2.x的教程中,可以学到如下基础知识
- 访问像素值
- 用0初始化矩阵
- saturate_cast 是做什么用的,以及它为什么有用
- 一些有关像素变换的精彩内容
图像处理
- 一般来说,图像处理算子是带有一幅或多幅输入图像、产生一幅输出图像的函数。
- 图像变换可分为以下两种:
- 点算子(像素变换)
- 邻域(基于区域的)算子
像素变换
- 在这一类图像处理变换中,仅仅根据输入像素值(有时可加上某些全局信息或参数)计算相应的输出像素值。
- 这类算子包括 亮度和对比度调整 ,以及颜色校正和变换。
亮度和对比度调整
#include <opencv2/core/core.hpp> #include <opencv2/highgui/highgui.hpp> #include <iostream> using namespace std; using namespace cv; double alpha; /**< 控制对比度 */ int beta; /**< 控制亮度 */ int main( int argc, char** argv ) { /// 读入用户提供的图像 Mat image = imread( argv[1] ); Mat new_image = Mat::zeros( image.size(), image.type() ); /// 初始化 cout << " Basic Linear Transforms " << endl; cout << "-------------------------" << endl; cout << "* Enter the alpha value [1.0-3.0]: "; cin >> alpha; cout << "* Enter the beta value [0-100]: "; cin >> beta; /// 执行运算 new_image(i,j) = alpha*image(i,j) + beta for( int y = 0; y < image.rows; y++ ) { for( int x = 0; x < image.cols; x++ ) { for( int c = 0; c < 3; c++ ) { new_image.at<Vec3b>(y,x)[c] = saturate_cast<uchar>( alpha*( image.at<Vec3b>(y,x)[c] ) + beta ); } } } /// 创建窗口 namedWindow("Original Image", 1); namedWindow("New Image", 1); /// 显示图像 imshow("Original Image", image); imshow("New Image", new_image); /// 等待用户按键 waitKey(); return 0; }