SoFunction
Updated on 2024-11-16

OpenCV Image Contrast in Practice

This article mainly introduces the OpenCV image contrast, with some reference value, interested can learn about

Implementation Principle

Image contrast refers to the measurement of the different brightness levels between the brightest white and darkest black in the light and dark areas of an image, i.e., the size of the gray-scale contrast of an image. A larger range of differences means a larger contrast, and a smaller range of differences means a smaller contrast. Set a benchmark value thresh, when percent is greater than 0, you need to make the color contrast in the image stronger, i.e., the farther the value is from thresh, the greater the change; when percent is equal to 1, the contrast is strong to the extreme, there is only a distinction between 255 and 0; when percent is equal to 0, it is unchanged; when percent is less than 0, the contrast decreases, i.e., the value is made closer to the thresh value; when percent equals -1, there is no more contrast, it's all thresh values.

The implementation process of the contrast adjustment algorithm is as follows:

1. Set the adjustment parameter percent, take the value of -100 to 100, similar to the PS settings, normalized to -1 to 1.

2. Individual processing for all pixel points of the image. When percent is greater than or equal to 0, the contrast is enhanced and the adjusted RGB three-channel values are:

3. If the percent is less than 0, the contrast is reduced, at this time the adjusted image RGB three-channel value is:

4. If percent is equal to 1, it is equal to 255 if it is greater than thresh, and equal to 0 if it is less than that.

At this point, the image realizes the brightness adjustment, the algorithm logic referencexingyanxiaoThe C++ implementation code is as follows.

Function Code

// Contrast
cv::Mat Contrast(cv::Mat src, int percent)
{
	float alpha = percent / ;
	alpha = max(-, min(, alpha));
	cv::Mat temp = ();
	int row = ;
	int col = ;
	int thresh = 127;
	for (int i = 0; i < row; ++i)
	{
		uchar *t = <uchar>(i);
		uchar *s = <uchar>(i);
		for (int j = 0; j < col; ++j)
		{
			uchar b = s[3 * j];
			uchar g = s[3 * j + 1];
			uchar r = s[3 * j + 2];
			int newb, newg, newr;
			if (alpha == 1)
			{
				t[3 * j + 2] = r > thresh ? 255 : 0;
				t[3 * j + 1] = g > thresh ? 255 : 0;
				t[3 * j] = b > thresh ? 255 : 0;
				continue;
			}
			else if (alpha >= 0)
			{
				newr = static_cast<int>(thresh + (r - thresh) / (1 - alpha));
				newg = static_cast<int>(thresh + (g - thresh) / (1 - alpha));
				newb = static_cast<int>(thresh + (b - thresh) / (1 - alpha));
			}
			else {
				newr = static_cast<int>(thresh + (r - thresh) * (1 + alpha));
				newg = static_cast<int>(thresh + (g - thresh) * (1 + alpha));
				newb = static_cast<int>(thresh + (b - thresh) * (1 + alpha));
 
			}
			newr = max(0, min(255, newr));
			newg = max(0, min(255, newg));
			newb = max(0, min(255, newb));
			t[3 * j + 2] = static_cast<uchar>(newr);
			t[3 * j + 1] = static_cast<uchar>(newg);
			t[3 * j] = static_cast<uchar>(newb);
		}
	}
	return temp;
}

C++ Test Code

#include <opencv2/>
#include <iostream>
using namespace cv;
using namespace std;
 
cv::Mat Contrast(cv::Mat src, int percent);
 
int main()
{
	cv::Mat src = imread("");
	cv::Mat result = Contrast(src, );
	imshow("original", src);
	imshow("result", result);
	waitKey(0);
	return 0;
}
 
// Contrast
cv::Mat Contrast(cv::Mat src, int percent)
{
	float alpha = percent / ;
	alpha = max(-, min(, alpha));
	cv::Mat temp = ();
	int row = ;
	int col = ;
	int thresh = 127;
	for (int i = 0; i < row; ++i)
	{
		uchar *t = <uchar>(i);
		uchar *s = <uchar>(i);
		for (int j = 0; j < col; ++j)
		{
			uchar b = s[3 * j];
			uchar g = s[3 * j + 1];
			uchar r = s[3 * j + 2];
			int newb, newg, newr;
			if (alpha == 1)
			{
				t[3 * j + 2] = r > thresh ? 255 : 0;
				t[3 * j + 1] = g > thresh ? 255 : 0;
				t[3 * j] = b > thresh ? 255 : 0;
				continue;
			}
			else if (alpha >= 0)
			{
				newr = static_cast<int>(thresh + (r - thresh) / (1 - alpha));
				newg = static_cast<int>(thresh + (g - thresh) / (1 - alpha));
				newb = static_cast<int>(thresh + (b - thresh) / (1 - alpha));
			}
			else {
				newr = static_cast<int>(thresh + (r - thresh) * (1 + alpha));
				newg = static_cast<int>(thresh + (g - thresh) * (1 + alpha));
				newb = static_cast<int>(thresh + (b - thresh) * (1 + alpha));
 
			}
			newr = max(0, min(255, newr));
			newg = max(0, min(255, newg));
			newb = max(0, min(255, newb));
			t[3 * j + 2] = static_cast<uchar>(newr);
			t[3 * j + 1] = static_cast<uchar>(newg);
			t[3 * j] = static_cast<uchar>(newb);
		}
	}
	return temp;
}

test effect

Figure 1 Original diagram

 

Fig. 2 Effect of parameter 50

Fig. 3 Effect of parameter -50

Image contrast adjustment can be achieved by adjusting PERCENT.

To this article on the OpenCV image contrast of the practice of the article is introduced to this, more related to OpenCV image contrast content please search my previous articles or continue to browse the following related articles I hope you will support me in the future!