6 Enhancing Image
6 Enhancing Image
Enhancing Image
Imgproc.equalizeHist(source, destination);
Sr.No. Paramaeters
1 Source
2 Destination
Apart from the equalizeHist() method, there are other methods provided by the Imgproc class.
They are described briefly:
Sr.No Method
.
4 filter2D(Mat src, Mat dst, int ddepth, Mat kernel, Point anchor, double
delta)
Example
The following example demonstrates the use of Imgproc class to enhance contrast of an image:
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;
try {
System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
Mat source = Highgui.imread("grayscale.jpg",
Highgui.CV_LOAD_IMAGE_GRAYSCALE);
Imgproc.equalizeHist(source, destination);
Highgui.imwrite("contrast.jpg", destination);
}catch (Exception e) {
Output
Original Image
Enhanced Contrast Image
int alpha = 2;
Sr.No Parameters
.
1 destination
It is destination image.
2 rtype
It is desired output matrix type or, rather the depth, since the number of channels
are the same as the input has. if rtype is negative, the output matrix will have the
same type as the input.
3 alpha
4 beta
Apart from the convertTo method, there are other methods provided by the Mat class. They are
described briefly:
Sr.No Methods
.
2 copyTo(Mat m)
3 diag()
4 dot(Mat m)
5 reshape(int cn)
Example
The following example demonstrates the use of Mat class to enhance brightness of an image:
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.highgui.Highgui;
try{
System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
Mat source =
Highgui.imread("digital_image_processing.jpg",Highgui.CV_LOAD_IMAGE_COLOR);
source.type());
}catch (Exception e) {
Output
When you execute the given code, the following output is seen:
Original Image
Enhanced Bright Image (Alpha=1 & Beta=50)
Enhanced Bright Image (Alpha=2 & Beta=50)
Sr.No. Parameters
1 source
It is source image.
2 destination
It is destination image.
3 Size
4 sigmaX
src1
1
It is first input array.
alpha
2
It is weight of the first array elements.
src2
3
It is second input array of the same size and channel number as src1.
Beta
4
It is weight of the second array elements.
gamma
5
It is scalar added to each sum.
dst
6
It is output array that has the same size and number of channels as the input arrays.
Apart from the GaussianBlur method, there are other methods provided by the Imgproc class.
They are described briefly:
Sr.No Methods
.
4 filter2D(Mat src, Mat dst, int ddepth, Mat kernel, Point anchor, double
delta)
Example
The following example demonstrates the use of Imgproc and Core class to apply sharpening to
an image:
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;
try{
System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
Highgui.CV_LOAD_IMAGE_COLOR);
Highgui.imwrite("sharp.jpg", destination);
}catch (Exception e) {
Output
When you execute the given code, the following output is seen:
Original Image
Sharpened Image
Java DIP - Image Compression Technique
An image can easily be compressed and stored through Java. Compression of image involves
converting an image into jpg and storing it.
In order to compress an image, we read the image and convert into BufferedImage object.
From this ImageWriteParam object, you can set the compression by calling these two methods
which are setCompressionMode() and setCompressionQuality(). Their syntaxes are as given
below:
obj.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
obj.setCompressionQuality(0.05f);
Sr.No Modes
.
1 MODE_DEFAULT
It is a constant value that may be passed into methods to enable that feature for
future writes.
2 MODE_DISABLED
It is a constant value that may be passed into methods to disable that feature for
future writes.
3 MODE_EXPLICIT
It is a constant value that may be passed into methods to enable that feature for
future writes.
Apart from the compressions methods, there are other methods provided by the
ImageWriteParam class. They are described briefly:
Sr.No Methods
.
1 canOffsetTiles()
It returns true if the writer can perform tiling with non-zero grid offsets while
writing.
2 getBitRate(float quality)
It returns a float indicating an estimate of the number of bits of output data for
each bit of input image data at the given quality level.
3 getLocale()
It returns the currently set Locale, or null if only a default Locale is supported.
4 isCompressionLossless()
5 unsetCompression()
6 unsetTiling()
Example
The following example demonstrates the use of ImageWriteParam class to compress an image:
import java.io.*;
import java.util.*;
import java.awt.image.*;
import javax.imageio.*;
import javax.imageio.stream.ImageOutputStream;
class Compresssion {
Iterator<ImageWriter>writers = ImageIO.getImageWritersByFormatName("jpg");
writer.setOutput(ios);
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(0.05f);
os.close();
ios.close();
writer.dispose();
Output
When you execute the given code, it compresses the image digital_image_processing.jpg to its
equivalent compressed image and writes it on the hard disk with the name compress.jpg.
Original Image
Compressed Image - Quality Factor: 0.05
Imgproc.copyMakeBorder(source,destination,top,bottom,left,right,borderType);
Sr.No Parameters
.
1 source
It is source image.
2 destination
It is destination image.
3 top
4 bottom
5 left
6 right
7 borderType
Sr.No Methods
.
4 filter2D(Mat src, Mat dst, int ddepth, Mat kernel, Point anchor, double
delta)
Example
The following example demonstrates the use of Imgproc class to add border to an image:
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;
try {
System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
Highgui.CV_LOAD_IMAGE_COLOR);
int borderType;
destination = source;
Imgproc.copyMakeBorder(source, destination, top, bottom, left, right,
Imgproc.BORDER_WRAP);
Highgui.imwrite("borderWrap.jpg", destination);
}catch (Exception e) {
Output
When you execute the given code, the following output is seen:
Original Image
Isolated Border Image
Imgproc.pyrDown(source, destination,destinationSize);
Sr.No Parameters
.
1 source
It is the source image.
2 destination
3 destinationSize
Apart from the pyrUp and pyrDown methods, there are other methods provided by the Imgproc
class. They are described briefly:
Sr.No Methods
.
4 filter2D(Mat src, Mat dst, int ddepth, Mat kernel, Point anchor, double
delta)
Example
The following example demonstrates the use of Imgproc class to perform up sampling and
down sampling on an image.
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;
try{
System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
Highgui.CV_LOAD_IMAGE_COLOR);
destination1 = source;
Imgproc.pyrUp(source, destination1, new Size(source.cols()*2 source.rows()*2));
Highgui.imwrite("pyrUp.jpg", destination1);
source = Highgui.imread("digital_image_processing.jpg",
Highgui.CV_LOAD_IMAGE_COLOR);
destination = source;
Highgui.imwrite("pyrDown.jpg", destination);
Output
When you execute the given code, the following output is seen:
Original Image
On the original image, pyrUp(UP Sampling) and pyrDown(Down Sampling) are performed.
The output after sampling is as shown below:
PyrUP Image
pyrDown Image
Sr.No Parameters
.
1 source
It is source image.
2 destination
It is destination image.
3 thresh
It is threshold value.
4 maxval
5 type
Sr.No Methods
.
4 filter2D(Mat src, Mat dst, int ddepth, Mat kernel, Point anchor, double
delta)
Example
The following example demonstrates the use of Imgproc class to perform thresholding
operations to an image:
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.highgui.Highgui;
import org.opencv.imgproc.Imgproc;
try{
System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
destination = source;
Imgproc.threshold(source,destination,127,255,Imgproc.THRESH_TOZERO);
Highgui.imwrite("ThreshZero.jpg", destination);
}catch (Exception e) {
Output
When you execute the given code, the following output is seen:
Original Image
On the above original image, some thresholding operations is performed which is shown in the
output below:
Thresh Binary
Thresh Binary Invert
Thresh Zero
In order to change the shape of the image, we read the image and convert into Mat object. Its
syntax is given below:
Flipping an Image
OpenCV allows three types of flip codes which are described below:
1 0
2 1
3 -1
We pass the appropriate flip code into method flip() in the Core class. Its syntax is given
below:
The method flip() takes three parameters: the source image matrix, the destination image
matrix, and the flip code.
Apart from the flip method, there are other methods provided by the Core class. They are
described briefly:
Sr.No Methods
.
It draws a circle.
5 sumElems(Mat src)
It calculates the per-element difference between two arrays or array and a scalar.
Example
The following example demonstrates the use of Core class to flip an image:
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferByte;
import java.io.File;
import javax.imageio.ImageIO;
import org.opencv.core.Core;
import org.opencv.core.CvType;
import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;
try {
System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
mat.put(0, 0, data);
mat1.get(0, 0, data1);
image1.getRaster().setDataElements(0,0,mat1.cols(),mat1.rows(),data1);
File ouptut = new File("hsv.jpg");
} catch (Exception e) {
Output
When you run the above example, it would flip an image
name digital_image_processing.jpg to its equivalent HSV color space image and write it on
hard disk with name flip.jpg.
ORIGINAL IMAGE
FLIPPED IMAGE