While image processing is a complex task, we will introduce a few techniques to clean and extract information from an image. This will provide the reader with some insight into image processing. We will also demonstrate how to extract text data from an image using Optical Character Recognition (OCR).
There are several techniques used to improve the quality of an image. Many of these require tweaking of parameters to get the improvement desired. We will demonstrate how to:
We will use OpenCV (http://opencv.org/), an open source project for image processing. There are several classes that we will use:
Mat
: This represents an n-dimensional array holding image data such as channel, grayscale, or color valuesImgproc
: Possesses many methods that process an imageImgcodecs
: Possesses methods to read and write image filesThe OpenCV Javadocs is found at http://docs.opencv.org/java/2.4.9/. In the examples that follow, we will use Wikipedia images since they can be freely downloaded. Specifically we will use the following images:
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:
Smoothing an image, also called blurring, will make the edges of an image smoother. Blurring is the process of making an image less distinct. We recognize blurred objects when we take a picture with the camera out of focus. Blurring can be used for special effects. Here, we will use it to create an image that we will then sharpen.
The following example loads an image of a cat and repeatedly applies the blur
method to the image. In this example, the process is repeated 25
times. Increasing the number of iterations will result in more blur or smoothing.
The third argument of the blur method is the blurring kernel size. The kernel is a matrix of pixels, 3 by 3 in this example, that is used for convolution. This is the process of multiplying each element of an image by weighted values of its neighbors. This allows neighboring values to effect an element's value:
Mat source = Imgcodecs.imread("cat.jpg"); Mat destination = source.clone(); for (int i = 0; i < 25; i++) { Mat sourceImage = destination.clone(); Imgproc.blur(sourceImage, destination, new Size(3.0, 3.0)); } Imgcodecs.imwrite("smoothCat.jpg", destination);
The following is the original image:
The enhanced image follows:
The convertTo
method provides a means of brightening an image. The original image is copied to a new image where the contrast and brightness is adjusted. The first parameter is the destination image. The second specifies that the type of image should not be changed. The third and fourth parameters control the contrast and brightness respectively. The first value is multiplied by this value while the second is added to the multiplied value:
Mat source = Imgcodecs.imread("cat.jpg"); Mat destination = new Mat(source.rows(), source.cols(), source.type()); source.convertTo(destination, -1, 1, 50); Imgcodecs.imwrite("brighterCat.jpg", destination);
The enhanced image follows:
Sometimes it is desirable to resize an image. The resize
method shown next illustrates how this is done. The image is read in and a new Mat
object is created. The resize
method is then applied where the width and height are specified in the Size
object parameter. The resized image is then saved:
Mat source = Imgcodecs.imread("cat.jpg"); Mat resizeimage = new Mat(); Imgproc.resize(source, resizeimage, new Size(250, 250)); Imgcodecs.imwrite("resizedCat.jpg", resizeimage);
The enhanced image follows:
Another common operation is to convert an image that uses one format into an image that uses a different format. In OpenCV, this is easy to accomplish as shown next. The image is read in and then immediately written out. The extension of the file is used by the imwrite
method to convert the image to the new format:
Mat source = Imgcodecs.imread("cat.jpg"); Imgcodecs.imwrite("convertedCat.jpg", source); Imgcodecs.imwrite("convertedCat.jpeg", source); Imgcodecs.imwrite("convertedCat.webp", source); Imgcodecs.imwrite("convertedCat.png", source); Imgcodecs.imwrite("convertedCat.tiff", source);
The images can now be used for specialized processing if necessary.