对比度是指更改每个图像像素的值。可以通过将图像的像素值乘以或除以任何常数来完成此更改。本文深入介绍了如何使用OpenCV更改图像对比度。
Input :
Original Image
Output :
-> Original Image
-> Image with contrast increased by 4
-> Image with contrast increased by 2
-> Image with contrast decreased by .5
-> Image with contrast decreased by .25
代码:CPP代码,用于增加或减少图像的对比度
CPP
// c++ code explaining how to
// increase or decrease the
// contrast of an image
// loading library files
#include
#include
#include
using namespace cv;
using namespace std;
int main(int argc, char** argv)
{
// Loading the Image File under testing
Mat image = imread("C:\\Users\\dell\\Desktop\\abc.jpg");
// Check whether the image is present or not
if (image.empty()) {
cout << "Could not open or find the image" << endl;
// waiting for any key to be pressed
return -1;
}
// Declaring the Contrast instances
// increasing the contrast level by 100%
Mat imageContrastHigh2;
image.convertTo(imageContrastHigh2, -1, 2, 0);
// increasing the contrast level by 200%
Mat imageContrastHigh4;
image.convertTo(imageContrastHigh4, -1, 4, 0);
// decreasing the contrast level by 50%
Mat imageContrastLow0_5;
image.convertTo(imageContrastLow0_5, -1, 0.5, 0);
// decreasing the contrast level by 75%
Mat imageContrastLow0_25;
image.convertTo(imageContrastLow0_25, -1, 0.25, 0);
// Declaring the windows
// for images belonging to different contrast level
String windowNameOriginalImage = "Original Image";
String windowNameContrastHigh2 = "Contrast Increased by 2";
String windowNameContrastHigh4 = "Contrast Increased by 4";
String windowNameContrastLow0_5 = "Contrast Decreased by 0.5";
String windowNameContrastLow0_25 = "Contrast Decreased by 0.25";
// Running the window instance
// and opening it
namedWindow(windowNameOriginalImage, WINDOW_NORMAL);
namedWindow(windowNameContrastHigh2, WINDOW_NORMAL);
namedWindow(windowNameContrastHigh4, WINDOW_NORMAL);
namedWindow(windowNameContrastLow0_5, WINDOW_NORMAL);
namedWindow(windowNameContrastLow0_25, WINDOW_NORMAL);
// Loading images inside the above created Windows
imshow(windowNameOriginalImage, image);
imshow(windowNameContrastHigh2, imageContrastHigh2);
imshow(windowNameContrastHigh4, imageContrastHigh4);
imshow(windowNameContrastLow0_5, imageContrastLow0_5);
imshow(windowNameContrastLow0_25, imageContrastLow0_25);
// waiting for any key to be pressed
waitKey(0);
// closing all the windows instances
// when any key is pressed.
destroyAllWindows();
return 0;
}
CPP
// Declaring the Contrast instances
// increasing the contrast level by 100%
Mat imageContrastHigh2;
image.convertTo(imageContrastHigh2, -1, 2, 0);
// increasing the contrast level by 200%
Mat imageContrastHigh4;
image.convertTo(imageContrastHigh4, -1, 4, 0);
// decreasing the contrast level by 50%
Mat imageContrastLow0_5;
image.convertTo(imageContrastLow0_5, -1, 0.5, 0);
// decreasing the contrast level by 75%
Mat imageContrastLow0_25;
image.convertTo(imageContrastLow0_25, -1, 0.25, 0);
输入 :
输出 :
对比度提高了4的图像
对比度提高2倍的图像
对比度降低了0.5的图像
对比度降低了.25的图像
解释 :
CPP
// Declaring the Contrast instances
// increasing the contrast level by 100%
Mat imageContrastHigh2;
image.convertTo(imageContrastHigh2, -1, 2, 0);
// increasing the contrast level by 200%
Mat imageContrastHigh4;
image.convertTo(imageContrastHigh4, -1, 4, 0);
// decreasing the contrast level by 50%
Mat imageContrastLow0_5;
image.convertTo(imageContrastLow0_5, -1, 0.5, 0);
// decreasing the contrast level by 75%
Mat imageContrastLow0_25;
image.convertTo(imageContrastLow0_25, -1, 0.25, 0);
这些代码行会将图像像素值更改指定的数量。然后将最终更改的图像存储到给定的输出图像。如果指定的数量因子大于1,则对比度级别将增加;否则,如果指定的数量因子小于1,则对比度级别将降低。
MAT函数:
“ MAT”函数将每个图像像素值更改为目标数据类型,并且值会随着倍数的倍增而变化。
Parameters :
m : Output Image
rtype : Output Image type, Output Image type is same
as input if it is set to -ve value
alpha : Input image pixel are multiplied with this
number prior to its assignment to output image
beta : adding this value to each input image pixel
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程” 。