Here, we analyze some of the basic image processing tools in OpenCV and their use in GoatImage.

All code is available on GitHub under GoatImage. To fully understand this article, read the related articles and look at this code.

Select functions are exemplified here. In GoatImage, JavaDocs can be generated further explaining the functions. The functions explained are:

Dilate, rotate, erode, min thresholding, and max thresholding are left to the code. Thresholding in OpenCV is described in depth with graphs and charts via the documentation.

Related Articles:

## Basic Processing in Computer Vision

Basic processing is the key to successful recognition. Training sets come in a specific form. Pre-processing is usually required to ensure the accuracy and quality of a program. JavaCV and OpenCV are fast enough to work in a variety of circumstances to improve algorithmic performance at a much lower speed reduction cost. Each transform applied to an image takes time and memory but will pay off handsomely if done correctly.

## Kernel Processing

Most of these functions are linear transformations. A linear transformation uses a function to map one matrix to another (Ax = b). In image processing, the matrix kernel is used to do this. Basically a weighted matrix can be used to map a certain point or pixel value.

For an overview of image processing kernels, see wikipedia.

Kernels may be generated in JavaCV.

/**
* Create a kernel from a double array (write large kernels more understandably)
* @param kernelArray The double array of doubles with the kernel values as signed ints
* @return The kernel mat
*/
def generateKernel(kernelArray: Array[Array[Int]]):Mat={
val m = if(kernelArray != null) kernelArray.length else 0
if(m == 0 ){
throw new IllegalStateException("Your Kernel Array Must be Initialized with values")
}
if(kernelArray(0).length != m){
throw new IllegalStateException("Your Kernel Array Must be Square and not sparse.")
}
val kernel = new Mat(m,m,CV_32F,new Scalar(0))
val ki = kernel.createIndexer().asInstanceOf[FloatIndexer]
for(i <- 0 until m){
for(j <- 0 until m){
ki.put(i,j,kernelArray(i)(j))
}
}
kernel
}

More reliably, there is a function for generating a Gaussian Kernel.

/**
* Generate the square gaussian kernel. I think the pattern is a(0,0)=1 a(1,0) = n a(2,0) = n+2i with rows as a(2,1) = a(2,0) * n and adding two to the middle then subtracting.
* However, there were only two examples on the page I found so do not use that without verification.
*
* @param kernelMN The m and n for our kernel matrix
* @param sigma The sigma to multiply by (kernel standard deviation)
* @return The resulting kernel matrix
*/
def generateGaussianKernel(kernelMN : Int, sigma : Double):Mat={
getGaussianKernel(kernelMN,sigma)
}

## Sharpen with A Cutom Kernel

Applying a kernel in OpenCV can be done with the *filter2D* method.

filter2D(srcMat,outMat,srcMat.depth(),kernel)

Here a sharpening kernel using the function above is applied.

/**
* Sharpen an image with a standard sharpening kernel.
* @param image The image to sharpen
* @return A new and sharper image
*/
def sharpen(image : Image):Image={
val srcMat = new Mat(image.image)
val outMat = new Mat(srcMat.rows(),srcMat.cols(),srcMat.`type`())
val karr : Array[Array[Int]] = Array[Array[Int]](Array(0,-1,0),Array(-1,5,-1),Array(0,-1,0))
val kernel : Mat = this.generateKernel(karr)
filter2D(srcMat,outMat,srcMat.depth(),kernel)
new Image(new IplImage(outMat),image.name,image.itype)
}

## Contrast

Contrast kicks up the color intensity in images by equation, equalization, or based on neighboring pixels.

One form of Contrast applies a direct function to an image:

/**
* Use an equation applied to the pixels to increase contrast. It appears that
* the majority of the effect occurs from converting back and forth with a very
* minor impact for the values. However, the impact is softer than with equalizing
* histograms. Try sharpen as well. The kernel kicks up contrast around edges.
*
* (maxIntensity/phi)*(x/(maxIntensity/theta))**0.5
*
* @param image The image to use
* @param maxIntensity The maximum intensity (numerator)
* @param phi Phi value to use
* @param theta Theta value to use
* @return
*/
def contrast(image : Image, maxIntensity : Double, phi : Double = 0.5, theta : Double = 0.5):Image={
val srcMat = new Mat(image.image)
val outMat = new Mat(srcMat.rows(),srcMat.cols(),srcMat.`type`())
val usrcMat = new Mat()
val dest = new Mat(srcMat.rows(),srcMat.cols(),usrcMat.`type`())
srcMat.convertTo(usrcMat,CV_32F,1/255.0,0)
pow(usrcMat,0.5,dest)
multiply(dest,(maxIntensity / phi))
val fm = 1 / Math.pow(maxIntensity / theta,0.5)
multiply(dest, fm)
dest.convertTo(outMat,CV_8U,255.0,0)
new Image(new IplImage(outMat),image.name,image.itype)
}

Here the image is manipulated using matrix equations to form a new image where pixel intensities are improved for clarity.

Another form of contrast equalizes the image histogram:

/**

* A form of contrast based around equalizing image histograms.

*

* @param image The image to equalize

* @return A new Image

*/

def equalizeHistogram(image : Image):Image={

val srcMat = new Mat(image.image)

val outMat = new Mat(srcMat.rows(),srcMat.cols(),srcMat.`type`())

equalizeHist(srcMat,outMat)

new Image(new IplImage(outMat),image.name,image.itype)

}

The JavaCV method *equalizeHist* is used here.

## Blur

Blurring uses averaging to dull images.

Gaussian blurring uses a Gaussian derived kernel to blur. This kernel uses an averaging function as opposed to equal weighting of neighboring pixels.

/**
* Perform a Gaussian blur. The larger the kernel the more blurred the image will be.
*
* @param image The image to use
* @param degree Strength of the blur
* @param kernelMN The kernel height and width should match (for instance 5x5)
* @param sigma The sigma to use in generating the matrix
* @param depth The depth to use
* @param brightenFactor A factor to brighten the result by with 0){
outImage = this.brighten(outImage,brightenFactor)
}
outImage
}

A box blur uses a straight kernel to blur, often weighting pixels equally.

/**
* Perform a box blur and return a new Image. Increasing the factor has a significant impact.
* This algorithm tends to be overly powerful. It wiped the lines out of my test image.
*
* @param image The Image object
* @param depth The depth to use with -1 as default corresponding to image.depth
* @return A new Image
*/
def boxBlur(image : Image,factor: Int = 1,depth : Int = -1):Image={
val srcMat = new Mat(image.image)
val outMat = new Mat(srcMat.rows(),srcMat.cols(),srcMat.`type`())
//build kernel
val kernel : Mat = this.generateKernel(Array(Array(factor,factor,factor),Array(factor,factor,factor),Array(factor,factor,factor)))
divide(kernel,9.0)
//apply kernel
filter2D(srcMat,outMat, depth, kernel)
new Image(new IplImage(outMat),image.name,image.itype)
}

## Unsharp Masking

Once a blurred *Mat* is achieved, it is possible to perform an unsharp mask. The unsharp mask brings out certain features by subtracting the blurred image from the original while taking into account an aditional factor.

def unsharpMask(image : Image, kernelMN : Int = 3, sigma : Double = 60,alpha : Double = 1.5, beta : Double= -0.5,gamma : Double = 2.0,brightenFactor : Int = 0):Image={
val srcMat : Mat = new Mat(image.image)
val outMat = new Mat(srcMat.rows(),srcMat.cols(),srcMat.`type`())
val retMat = new Mat(srcMat.rows(),srcMat.cols(),srcMat.`type`())
//using htese methods allows the matrix kernel size to grow
GaussianBlur(srcMat,outMat,new Size(kernelMN,kernelMN),sigma)
addWeighted(srcMat,alpha,outMat,beta,gamma,retMat)
var outImage : Image = new Image(new IplImage(outMat),image.name,image.itype)
if(brightenFactor > 0){
outImage = this.brighten(outImage,brightenFactor)
}
outImage
}

## Conclusion

This article examined various image processing techniques.

### Like this:

Like Loading...