Cases of OPencv image enhancement

Implementation of Opencv image enhancement algorithm (histogram equalization, Laplace, Log, Gamma) | Chuan · Yen

1, Introduction

Image enhancement algorithm is a process of image distortion, which is to highlight some characteristics of the response and facilitate our processing of the region of interest. Generally, there are four methods: histogram equalization, Laplace, Log and Gamma

2, Histogram equalization

Image contrast enhancement methods can be divided into two categories: one is direct contrast enhancement method; The other is indirect contrast enhancement.
Histogram stretching and histogram equalization are the two most common indirect contrast enhancement methods.

Histogram stretching is to adjust the histogram through contrast stretching, so as to "expand" the difference between foreground and background gray, so as to enhance the contrast. This method can be realized by linear or nonlinear methods.
Histogram equalization uses the cumulative function to "adjust" the gray value to enhance the contrast.

  • Advantages: this method is very useful for images with too bright or too dark background and foreground. In particular, this method can bring better display of bone structure in X-ray images and better details in overexposed or underexposed photos. One of the main advantages of this method is that it is a very intuitive technology and reversible operation. If the equalization function is known, the original histogram can be restored, and the amount of calculation is small.
  • Disadvantages: the disadvantage is that it does not select the processed data. It may increase the contrast of background noise and reduce the contrast of useful signals; The gray level of the transformed image decreases and some details disappear; Some images, such as histogram, have peaks, and the contrast is unnaturally enhanced after processing.
    Histogram equalization of color image:

 

#if 1 / / image enhancement algorithm -- histogram equalization image enhancement
int main(int args, char* arg)
{
	Mat	src = imread("C:\\Users\\19473\\Desktop\\opencv_images\\88.jpg");
	if (!src.data)
	{
		printf("could not  load  image....\n");
	}
	imshow("input_demo", src);

	Mat stc_bgr[3];
	Mat  dst;// Enhanced image
	split(src, stc_bgr); // 
	for (int i=0;i<3;i++) 
	{
		equalizeHist(stc_bgr[i], stc_bgr[i]);
	}
	merge(stc_bgr,3,dst); // merge
	imshow("Enhanced image", dst);
	waitKey(0);
	return -1;
}
#endif

3, Laplace image enhancement

Laplace operator can enhance the local image contrast.
Laplace 8 neighborhood convolution kernel:
0  -1  0
-1  5  -1
0   -1  0
The convolution function of filter2D is used to realize the image:

#if 1 / / image enhancement algorithm -- Laplacian enhancement using convolution kernel
int main(int args, char* arg)
{
	Mat	src = imread("C:\\Users\\19473\\Desktop\\opencv_images\\88.jpg");
	if (!src.data)
	{
		printf("could not  load  image....\n");
	}
	imshow("input_demo", src);
    Mat dst;
	Mat kernel = (Mat_<int>(3, 3) << 0, -1, 0, -1, 5, -1, 0, -1, 0);
	filter2D(src, dst, src.depth(), kernel);

	
	imshow("Enhanced image", dst);
	waitKey(0);
	return -1;
}
#endif

4, Logarithmic Log transform image enhancement

Logarithmic transformation can expand the low gray value part of the image, show more details of the low gray value part, compress the high gray value part and reduce the details of the high gray value part, so as to achieve the purpose of emphasizing the low gray part of the image. Transformation method:

For different base numbers, the larger the base number, the stronger the expansion of the low gray part and the stronger the compression of the high gray part.

 

 

#if 1 / / image enhancement algorithm -- log transform enhancement
int main(int args, char* arg)
{
	Mat	src = imread("C:\\Users\\19473\\Desktop\\opencv_images\\613.png");
	if (!src.data)
	{
		printf("could not  load  image....\n");
	}
	imshow("Original image", src);
	// Note: CV_32FC3
	Mat dst(src.size(), CV_32FC3);

	for (int i = 0; i < src.rows; i++)
	{
		for (int j = 0; j < src.cols; j++)
		{
			// Each channel of bgr is calculated
			dst.at<Vec3f>(i, j)[0] = log(1 + src.at<Vec3b>(i, j)[0]);
		    dst.at<Vec3f>(i, j)[1] = log(1 + src.at<Vec3b>(i, j)[1]);
			dst.at<Vec3f>(i, j)[2] = log(1 + src.at<Vec3b>(i, j)[2]);
		}
	}
	// normalization
	normalize(dst, dst, 0, 255, CV_MINMAX);

	convertScaleAbs(dst, dst);

	imshow("Enhanced image", dst);
	waitKey(0);
	return -1;
}
#endif

5, Image enhancement based on gamma transform

Gamma transform is mainly used for image correction, which modifies the images with too high or too low gray level to enhance the contrast. The transformation formula is to multiply each pixel value on the original image:

 

Gamma transform has an obvious effect on image enhancement when the image contrast is low and the overall brightness value is high (for camera overexposure).

#if 1 / / image enhancement algorithm -- gamma
int  Gamma = 2;
int main(int args, char* arg)
{
	Mat	src = imread("C:\\Users\\19473\\Desktop\\opencv_images\\88.jpg");
	if (!src.data)
	{
		printf("could not  load  image....\n");
	}
	imshow("Original image", src);
	// Note: CV_32FC3
	Mat dst(src.size(), CV_32FC3);

	for (int i = 0; i < src.rows; i++)
	{
		for (int j = 0; j < src.cols; j++)
		{
			// Each channel of bgr is calculated
			dst.at<Vec3f>(i, j)[0] = pow(src.at<Vec3b>(i, j)[0], Gamma);
			dst.at<Vec3f>(i, j)[1] = pow(src.at<Vec3b>(i, j)[1], Gamma);
			dst.at<Vec3f>(i, j)[2] = pow(src.at<Vec3b>(i, j)[2], Gamma);
		}
	}
	// normalization
	normalize(dst, dst, 0, 255, CV_MINMAX);

	convertScaleAbs(dst, dst);

	imshow("Enhanced image", dst);
	waitKey(0);
	return -1;
}
#endif

Keywords: OpenCV AI Computer Vision

Added by Loldongs on Sat, 12 Feb 2022 16:55:21 +0200