Java:Data Science Made Easy
上QQ阅读APP看书,第一时间看更新

Changing the contrast of an image

Here we will demonstrate how to enhance a black-and-white image of a parrot. The Imgcodecs class's imread method reads in the image. Its second parameter specifies the type of color used by the image, which is grayscale in this case. A new Mat object is created for the enhanced image using the same size and color type as the original.

The actual work is performed by the equalizeHist method. This equalizes the histogram of the image which has the effect of normalizing the brightness and increases the contrast of the image. An image histogram is a histogram representing the tonal distribution of an image. Tonal is also referred to as lightness. It represents the variation in the brightness found in an image.

The last step is to write out the image.

Mat source = Imgcodecs.imread("GrayScaleParrot.png", 
Imgcodecs.CV_LOAD_IMAGE_GRAYSCALE);
Mat destination = new Mat(source.rows(), source.cols(), source.type());
Imgproc.equalizeHist(source, destination);
Imgcodecs.imwrite("enhancedParrot.jpg", destination);

The following is the original image:

The enhanced image follows: