OPENCV Image Enhancement Algorithm implementation (histogram equalization, Laplace, Log, Gamma)

Source: Internet
Author: User
Tags log log

1. Image enhancement based on histogram equalization
The histogram equalization is by adjusting the gray scale distribution of the image, making the distribution more balanced on the 0~255 gray scale, improving the contrast of the image and achieving the objective of improving the subjective visual effect of the image. Lower contrast images are suitable for using histogram equalization to enhance image detail.
the histogram equalization of color image is realized:

   
  
  1. #include <opencv2/highgui/highgui.hpp>
  2. #include <opencv2/imgproc/imgproc.hpp>
  3. #include <iostream>
  4. using namespace CV;
  5. int Main (int argc, char *argv[])
  6. {
  7. Mat image = Imread ("test.jpg", 1);
  8. if (Image.empty ())
  9. {
  10. std::cout << "failed to open the picture, please check" << std::Endl;
  11. return -1;
  12. }
  13. Imshow ("original image", image);
  14. Mat imagergb[3];
  15. Split (image, Imagergb);
  16. for (int i = 0; i < 3; i++)
  17. {
  18. Equalizehist (Imagergb[i], imagergb[i]);
  19. }
  20. Merge (Imagergb, 3, image);
  21. Imshow ("Histogram equalization Image enhancement effect", image);
  22. Waitkey ();
  23. return 0;
  24. }

histogram equalization Enhanced original image:

Histogram equalization Enhanced effect:


2. Image enhancement based on Laplace operator
The use of a center 5 of the 8 neighborhood Laplace operator and image convolution can be achieved to sharpen the image enhancement, the Laplace operator as shown:      

the Laplace operator can enhance the local image contrast ratio:

   
  
  1. #include <opencv2/highgui/highgui.hpp>
  2. #include <opencv2/imgproc/imgproc.hpp>
  3. #include <iostream>
  4. using namespace CV;
  5. int Main (int argc, char *argv[])
  6. {
  7. Mat image = Imread ("test.jpg", 1);
  8. if (Image.empty ())
  9. {
  10. std::cout << "failed to open the picture, please check" << std::Endl;
  11. return -1;
  12. }
  13. Imshow ("original image", image);
  14. Mat imageenhance;
  15. Mat kernel = (mat_<float> (3, 3) << 0, -1, 0, 0 , 5, 0, 0, -1, 0);
  16. Filter2d (image, Imageenhance, CV_8UC3, kernel);
  17. Imshow ("Laplace operator image Enhancement effect", imageenhance);
  18. Waitkey ();
  19. return 0;
  20. }


Laplace operator Enhanced original image:

Laplace operator Enhanced effect:


3. Image enhancement based on log log transform

The logarithmic transform can expand the low gray value of the image, show the more detail of the Low gray section, compress the high gray value part, reduce the detail of the high gray value part, and achieve the aim of emphasizing the low gray level of the image. Transform method:



The function of logarithmic transform on the detail enhancement of low gray-level image can be intuitively understood from the logarithmic graph:



The x-axis of 0.4 corresponds to the y-axis of 0.8, that is, the original image of the 0~0.4 of the low gray section after the logarithmic operation to expand to the 0~0.8 portion, and the entire 0.4~1 of the high gray section is projected to only the 0.8~1 interval, so that the expansion and enhancement of the low gray section, compression of the value of the High gray section.

As you can see, for different bases, the larger the base, the stronger the expansion of the lower gray section, and the stronger the compression of the High gray section.



   
  
  1. #include <opencv2/highgui/highgui.hpp>
  2. #include <opencv2/imgproc/imgproc.hpp>
  3. using namespace CV;
  4. int Main (int argc, char *argv[])
  5. {
  6. Mat image = Imread ("test.jpg");
  7. Mat imagelog(image.size (), CV_32FC3);
  8. for (int i = 0; i < image.rows; i++)
  9. {
  10. for (int j = 0; j < Image.cols; J + +)
  11. {
  12. Imagelog.at<vec3f> (i, j) [0] = log(1 + image.at<vec3b> (i, j) [0]);
  13. Imagelog.at<vec3f> (i, J) [1] = log(1 + image.at<vec3b> (i, J) [1]);
  14. Imagelog.at<vec3f> (i, J) [2] = log(1 + image.at<vec3b> (i, J) [2]);
  15. }
  16. }
  17. Normalized to 0~255
  18. Normalize (Imagelog, Imagelog, 0, 255, Cv_minmax);
  19. //Convert to 8bit image display
  20. Convertscaleabs (Imagelog, Imagelog);
  21. Imshow ("Soure", image);
  22. Imshow ("after", Imagelog);
  23. Waitkey ();
  24. return 0;
  25. }

log Log transform enhances the original image:


Logarithmic log transform enhanced effect:


The logarithmic transform is good for image enhancement with low overall contrast and low gray value .

4. Image enhancement based on Gamma transform

The gamma transform is mainly used for image correction, which improves the contrast by correcting the gray-high or low-grey images. The transformation formula is the multiplication of each pixel value on the original image:



The correction effect of gamma transform on image is realized by enhancing the detail of low gray level or high grayscale, which can be intuitively understood from the gamma curve:



Gamma value of 1 as the demarcation, the smaller the value of the image of the lower gray part of the expansion of the stronger, the greater the value of the image of the higher gray part of the expansion of the stronger, through the different gamma values, can be achieved to enhance the low gray level or high gray part of the role of detail.

The gamma transform is relatively low in image contrast, and the overall luminance value is high (for the camera over exposure) The image enhancement effect is obvious.

   
  
  1. #include <opencv2/highgui/highgui.hpp>
  2. #include <opencv2/imgproc/imgproc.hpp>
  3. using namespace CV;
  4. int Main (int argc, char *argv[])
  5. {
  6. Mat image = Imread ("test.jpg");
  7. Mat imagegamma(image.size (), CV_32FC3);
  8. for (int i = 0; i < image.rows; i++)
  9. {
  10. for (int j = 0; j < Image.cols; J + +)
  11. {
  12. Imagegamma.at<vec3f> (i, j) [0] = (Image.at<vec3b> (i, j) [0]) * (Image.at<vec3b> (i, j) [0]) * (Image.at<vec3b> (i, j) [0]);
  13. Imagegamma.at<vec3f> (i, J) [1] = (Image.at<vec3b> (i, J) [1]) * (Image.at<vec3b> (i, j) [1]) * (Image.at<vec3b> (i, J) [1]);
  14. Imagegamma.at<vec3f> (i, J) [2] = (Image.at<vec3b> (i, J) [2]) * (Image.at<vec3b> (i, j) [2]) * (Image.at<vec3b> (i, J) [2]);
  15. }
  16. }
  17. Normalized to 0~255
  18. Normalize (Imagegamma, Imagegamma, 0, 255, Cv_minmax);
  19. //Convert to 8bit image display
  20. Convertscaleabs (Imagegamma, Imagegamma);
  21. Imshow ("original", image);
  22. Imshow ("Gamma transform image enhancement effect", Imagegamma);
  23. Waitkey ();
  24. return 0;
  25. }

Gamma Transform Enhanced original image:

Gamma Transform enhanced effect:


(turn) OpenCV Image enhancement Algorithm implementation (histogram equalization, Laplace, Log, Gamma)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.