Opencv処理画像のコントラストと輝度
2663 ワード
式:g(x)=af(x)+b、f(x)は元の画像画素値、g(x)は処理後の画像画素値を表す.a>0,a,bはそれぞれ画像のコントラストと輝度を制御する.具体的なコードは以下の通りです.
説明:saturate_castはデータのオーバーフローを防止する、値が0未満であると結果は0、値が255より大きいと結果は255となる.
参考:docs.opencv.org/2.4.13/doc/tutorials/tutorials.html learning opencv
void ChangeContrastAndBrightness(Mat& myImage) {
Mat new_image = Mat::zeros(myImage.size(), myImage.type());
//initialize values
std::cout<<" Basic Linear Transforms "<::endl;
std::cout<<"-------------------------"<::endl;
std::cout<<"* Enter the alpha value [1.0-3.0]: ";
std::cin>>alpha;
std::cout<<"* Enter the beta value [0-100]: ";
std::cin>>beta;
for (int y = 0;y < myImage.rows;y++) {
for (int x = 0;x < myImage.cols;x++) {
for (int c = 0; c < 3;c++) {
new_image.at<Vec3b>(y, x)[c] = saturate_cast(alpha*(myImage.at<Vec3b>(y, x)[c]) + beta); //
}
}
}
namedWindow("original image", 1);
namedWindow("new image", 1);
imshow("original image", myImage);
imshow("new image", new_image);
waitKey(0);
}
説明:saturate_castはデータのオーバーフローを防止する、値が0未満であると結果は0、値が255より大きいと結果は255となる.
参考:docs.opencv.org/2.4.13/doc/tutorials/tutorials.html learning opencv