DIP – Watermark

Java DIP – Applying Watermark ”; Previous Next In this chapter we learn two ways of applying watermark on images. These ways are − Applying Text Watermark Applying Image watermark Applying Text Watermark We use OpenCV function putText to apply text watermark to image. It can be found under Core package. Its syntax is given below − Core.putText(source, Text, Point, fontFace ,fontScale , color); The parameters of this function are described below − Sr.No. Parameter & Description 1 Source It is source image. 2 Text It is the string text that would appear on the image. 3 Point It is the point where text should appear on image. 4 fontFace Font type. For example − FONT_HERSHEY_SIMPLEX, FONT_HERSHEY_PLAIN, FONT_HERSHEY_COMPLEX etc. 5 fontScale It is font scale factor that is multiplied by the font-specific base size. 6 color It is text color. Apart from the putText method, there are other methods provided by the Core class. They are described briefly − Sr.No. Method & Description 1 normalize(Mat src, Mat dst, double alpha, double beta, int norm_type) It normalizes the norm or value range of an array. 2 perspectiveTransform(Mat src, Mat dst, Mat m) It performs the perspective matrix transformation of vectors. 3 phase(Mat x, Mat y, Mat angle) It calculates the rotation angle of 2D vectors. 4 rectangle(Mat img, Point pt1, Point pt2, Scalar color) It draws a simple, thick, or filled up-right rectangle. 5 reduce(Mat src, Mat dst, int dim, int rtype, int dtype) It reduces a matrix to a vector. 6 transform(Mat src, Mat dst, Mat m) It performs the matrix transformation of every array element. Example The following example demonstrates the use of Core class to apply text watermark to an image − import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.highgui.Highgui; import org.opencv.imgproc.Imgproc; public class Main { public static void main( String[] args ) { try{ System.loadLibrary( Core.NATIVE_LIBRARY_NAME ); Mat source = Highgui.imread(“digital_image_processing.jpg”, Highgui.CV_LOAD_IMAGE_COLOR); Mat destination = new Mat(source.rows(),source.cols(), source.type()); Core.putText(source, “Tutorialspoint.com”, new Point (source.rows()/2,source.cols()/2), Core.FONT_ITALIC,new Double(1),new Scalar(255)); Highgui.imwrite(“watermarked.jpg”, source); } catch (Exception e) { System.out.println(“Error: “+e.getMessage()); } } } Output When you execute the given code, the following output is seen − Original Image Text Watermarked Image Applying Image Watermark on Image We are going to use OpenCV function addWeighted to apply image watermark to image. It can be found under Core package. Its syntax is given below − Core.addWeighted(InputArray src1, alpha, src2 (Watermark image), beta, gamma, OutputArray dst); The parameters of this function are described below − Sr.No. Parameter & Description 1 src1 It is first input array. 2 alpha It is the weight of the first array elements. 3 src2 It is the second input array of the same size and channel number as src1. 4 beta It is the weight of the second array elements. 5 gamma It is the scalar added to each sum. 6 dst It is the output array that has the same size and number of channels as the input arrays. Example The following example demonstrates the use of Core class to apply image watermark to an image − import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.highgui.Highgui; import org.opencv.imgproc.Imgproc; public class Main { public static void main( String[] args ) { try{ System.loadLibrary( Core.NATIVE_LIBRARY_NAME ); Mat source = Highgui.imread(“digital_image_processing.jpg”, Highgui.CV_LOAD_IMAGE_COLOR); Mat waterMark = Highgui.imread(“watermark.png”, Highgui.CV_LOAD_IMAGE_COLOR); Rect ROI = new Rect(waterMark.rows() * 4,waterMark.cols(), waterMark.cols(),waterMark.rows()); Core.addWeighted(source.submat(ROI), 0.8, waterMark, 0.2, 1, source.submat(ROI)); Highgui.imwrite(“watermarkedImage.jpg”, source); } catch (Exception e) { System.out.println(“Error: ” + e.getMessage()); } } } Output When you execute the given code, the following output is seen − Original Image The Watermark Image Watermarked Image Print Page Previous Next Advertisements ”;

DIP – Prewitt Operator

Java DIP – Prewitt Operator ”; Previous Next Prewitt operator is used for edge detection in an image. It detects two types of edges: vertical edges and horizontal edges. We use OpenCV function filter2D to apply Prewitt operator to images. It can be found under Imgproc package. Its syntax is given below − filter2D(src, dst, depth , kernel, anchor, delta, BORDER_DEFAULT ); The function arguments are described below − Sr.No. Argument & Description 1 src It is source image. 2 dst It is destination image. 3 depth It is the depth of dst. A negative value (such as -1) indicates that the depth is the same as the source. 4 kernel It is the kernel to be scanned through the image. 5 anchor It is the position of the anchor relative to its kernel. The location Point (-1, -1) indicates the center by default. 6 delta It is a value to be added to each pixel during the convolution. By default it is 0. 7 BORDER_DEFAULT We let this value by default. Apart from the filter2D method, there are other methods provide by the Imgproc class. They are described briefly − Sr.No. Method & Description 1 cvtColor(Mat src, Mat dst, int code, int dstCn) It converts an image from one color space to another. 2 dilate(Mat src, Mat dst, Mat kernel) It dilates an image by using a specific structuring element. 3 equalizeHist(Mat src, Mat dst) It equalizes the histogram of a grayscale image. 4 filter2D(Mat src, Mat dst, int depth, Mat kernel, Point anchor, double delta) It convolves an image with the kernel. 5 GaussianBlur(Mat src, Mat dst, Size ksize, double sigmaX) It blurs an image using a Gaussian filter. 6 integral(Mat src, Mat sum) It calculates the integral of an image. Example The following example demonstrates the use of Imgproc class to apply Prewitt operator to an image of Grayscale. 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; public class convolution { public static void main( String[] args ) { try { int kernelSize = 9; System.loadLibrary( Core.NATIVE_LIBRARY_NAME ); Mat source = Highgui.imread(“grayscale.jpg”, Highgui.CV_LOAD_IMAGE_GRAYSCALE); Mat destination = new Mat(source.rows(),source.cols(),source.type()); Mat kernel = new Mat(kernelSize,kernelSize, CvType.CV_32F) { { put(0,0,-1); put(0,1,0); put(0,2,1); put(1,0-1); put(1,1,0); put(1,2,1); put(2,0,-1); put(2,1,0); put(2,2,1); } }; Imgproc.filter2D(source, destination, -1, kernel); Highgui.imwrite(“output.jpg”, destination); } catch (Exception e) { System.out.println(“Error: ” + e.getMessage()); } } } Output When you execute the given code, the following output is seen − Original Image This original image is convolved with the Prewitt operator of vertical edges as given below − Vertical direction -1 0 1 -1 0 1 -1 0 1 Convolved Image(Vertical Direction) This original image has also been convolved with the Prewitt operator of horizontal edges, which is given below − Horizontal Direction -1 -1 -1 0 0 0 1 1 1 Convolved Image(Horizontal Direction) Print Page Previous Next Advertisements ”;

DIP – Gaussian Filter

Java DIP – Applying Gaussian Filter ”; Previous Next In this chapter, we apply Gaussian filter to an image that blurs an image. We are going to use OpenCV function GaussianBlur to apply Gaussian filter to images. It can be found under Imgproc package. Its syntax is given below − Imgproc.GaussianBlur(source, destination,Size,SigmaX); The function arguments are described below − Sr.No. Argument & Description 1 source It is source image. 2 destination It is destination image. 3 Size It is Gaussian kernel size. 4 SigmaX It is Gaussian kernel standard deviation in X direction. Apart from the GaussianBlur method, there are other methods provided by the Imgproc class. They are described briefly − Sr.No. Method & Description 1 cvtColor(Mat src, Mat dst, int code, int dstCn) It converts an image from one color space to another. 2 dilate(Mat src, Mat dst, Mat kernel) It dilates an image by using a specific structuring element. 3 equalizeHist(Mat src, Mat dst) It equalizes the histogram of a grayscale image. 4 filter2D(Mat src, Mat dst, int depth, Mat kernel, Point anchor, double delta) It convolves an image with the kernel. 5 GaussianBlur(Mat src, Mat dst, Size ksize, double sigmaX) It blurs an image using a Gaussian filter. 6 integral(Mat src, Mat sum) It calculates the integral of an image. Example The following example demonstrates the use of Imgproc class to apply Gaussian filter to 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; public class Main { public static void main( String[] args ) { try { System.loadLibrary( Core.NATIVE_LIBRARY_NAME ); Mat source = Highgui.imread(“digital_image_processing.jpg”, Highgui.CV_LOAD_IMAGE_COLOR); Mat destination = new Mat(source.rows(),source.cols(),source.type()); Imgproc.GaussianBlur(source, destination,new Size(45,45), 0); Highgui.imwrite(“Gaussian45.jpg”, destination); } catch (Exception e) { System.out.println(“Error:” + e.getMessage()); } } } Output When you execute the given code, the following output is seen − Original Image When this original image is convolved with the Gaussian filter of size 11 and 45, the following output is seen. Gaussian filter of size 11 Gaussian filter of size 45 Print Page Previous Next Advertisements ”;

DIP – Sobel Operator

Java DIP – Sobel Operator ”; Previous Next Sobel operator is very similar to Prewitt operator. It is also a derivative mask and is used for edge detection. Sobel operator is used to detect two kinds of edges in an image: Vertical direction edges and Horizontal direction edges. We are going to use OpenCV function filter2D to apply Sobel operator to images. It can be found under Imgproc package. Its syntax is given below − filter2D(src, dst, depth , kernel, anchor, delta, BORDER_DEFAULT ); The function arguments are described below − Sr.No. Argument 1 src It is source image. 2 dst It is destination image. 3 depth It is the depth of dst. A negative value (such as -1) indicates that the depth is the same as the source. 4 kernel It is the kernel to be scanned through the image. 5 anchor It is the position of the anchor relative to its kernel. The location Point (-1, -1) indicates the center by default. 6 delta It is a value to be added to each pixel during the convolution. By default it is 0. 7 BORDER_DEFAULT We let this value by default. Apart from the filter2D method, there are other methods provide by the Imgproc class. They are described briefly − Sr.No. Method & Description 1 cvtColor(Mat src, Mat dst, int code, int dstCn) It converts an image from one color space to another. 2 dilate(Mat src, Mat dst, Mat kernel) It dilates an image by using a specific structuring element. 3 equalizeHist(Mat src, Mat dst) It equalizes the histogram of a grayscale image. 4 filter2D(Mat src, Mat dst, int depth, Mat kernel, Point anchor, double delta) It convolves an image with the kernel. 5 GaussianBlur(Mat src, Mat dst, Size ksize, double sigmaX) It blurs an image using a Gaussian filter. 6 integral(Mat src, Mat sum) It calculates the integral of an image. Example The following example demonstrates the use of Imgproc class to apply Sobel operator to an image of Grayscale. 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; public class convolution { public static void main( String[] args ) { try { int kernelSize = 9; System.loadLibrary( Core.NATIVE_LIBRARY_NAME ); Mat source = Highgui.imread(“grayscale.jpg”, Highgui.CV_LOAD_IMAGE_GRAYSCALE); Mat destination = new Mat(source.rows(),source.cols(),source.type()); Mat kernel = new Mat(kernelSize,kernelSize, CvType.CV_32F) { { put(0,0,-1); put(0,1,0); put(0,2,1); put(1,0-2); put(1,1,0); put(1,2,2); put(2,0,-1); put(2,1,0); put(2,2,1); } }; Imgproc.filter2D(source, destination, -1, kernel); Highgui.imwrite(“output.jpg”, destination); } catch (Exception e) { System.out.println(“Error: ” + e.getMessage()); } } } Output When you execute the given code, the following output is seen − Original Image This original image is convolved with the Sobel operator of vertical edges, which is given below − Vertical Direction -1 0 1 -2 0 2 -1 0 1 Convolved Image(Vertical Direction) This original is convolved with the Sobel operator of horizontal edges, which is given below − Horizontal Direction -1 -2 -1 0 0 0 1 2 1 Convolved Image(Horizontal Direction) Print Page Previous Next Advertisements ”;

DIP – Image Pyramids

Java DIP – Image Pyramids ”; Previous Next Image pyramid is nothing but a method to display a multi-resolution image. The lowermost layer is a highest-resolution version of image and the topmost layer is a lowest-resolution version of the image. Image pyramids are used to handle image at different scales. In this chapter we perform some down sampling and up sampling on images. We use OpenCV functions pyrUp and pyrDown. They can be found under Imgproc package. Its syntax is given below − Imgproc.pyrUp(source, destination, destinationSize); Imgproc.pyrDown(source, destination,destinationSize); The parameters are described below − Sr.No. Parameter & Description 1 source It is the source image. 2 destination It is the destination image. 3 destinationSize It is the size of the output image. By default, it is computed as Size((src.cols*2), (src.rows*2)). Apart from the pyrUp and pyrDown methods, there are other methods provided by the Imgproc class. They are described briefly − Sr.No. Method & Description 1 cvtColor(Mat src, Mat dst, int code, int dstCn) It converts an image from one color space to another. 2 dilate(Mat src, Mat dst, Mat kernel) It dilates an image by using a specific structuring element. 3 equalizeHist(Mat src, Mat dst) It equalizes the histogram of a grayscale image. 4 filter2D(Mat src, Mat dst, int depth, Mat kernel, Point anchor, double delta) It convolves an image with the kernel. 5 GaussianBlur(Mat src, Mat dst, Size ksize, double sigmaX) It blurs an image using a Gaussian filter. 6 integral(Mat src, Mat sum) It calculates the integral of an image. 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; public class main { public static void main( String[] args ) { try{ System.loadLibrary( Core.NATIVE_LIBRARY_NAME ); Mat source = Highgui.imread(“digital_image_processing.jpg”, Highgui.CV_LOAD_IMAGE_COLOR); Mat destination1 = new Mat(source.rows()*2, source.cols()*2,source.type()); 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); Mat destination = new Mat(source.rows()/2,source.cols()/2, source.type()); destination = source; Imgproc.pyrDown(source, destination, new Size(source.cols()/2, source.rows()/2)); Highgui.imwrite(“pyrDown.jpg”, destination); } catch (Exception e) { System.out.println(“error: ” + e.getMessage()); } } } 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 Print Page Previous Next Advertisements ”;

DIP – Basic Thresholding

Java DIP – Basic Thresholding ”; Previous Next Thresholding enables to achieve image segmentation in the easiest way. Image segmentation means dividing the complete image into a set of pixels in such a way that the pixels in each set have some common characteristics. Image segmentation is highly useful in defining objects and their boundaries. In this chapter we perform some basic thresholding operations on images. We use OpenCV function threshold. It can be found under Imgproc package. Its syntax is given below − Imgproc.threshold(source, destination, thresh , maxval , type); The parameters are described below − Sr.No. Parameter & Description 1 source It is source image. 2 destination It is destination image. 3 thresh It is threshold value. 4 maxval It is the maximum value to be used with the THRESH_BINARY and THRESH_BINARY_INV threshold types. 5 type The possible types are THRESH_BINARY, THRESH_BINARY_INV, THRESH_TRUNC, and THRESH_TOZERO. Apart from these thresholding methods, there are other methods provided by the Imgproc class. They are described briefly − Sr.No. Method & Description 1 cvtColor(Mat src, Mat dst, int code, int dstCn) It converts an image from one color space to another. 2 dilate(Mat src, Mat dst, Mat kernel) It dilates an image by using a specific structuring element. 3 equalizeHist(Mat src, Mat dst) It equalizes the histogram of a grayscale image. 4 filter2D(Mat src, Mat dst, int ddepth, Mat kernel, Point anchor, double delta) It convolves an image with the kernel. 5 GaussianBlur(Mat src, Mat dst, Size ksize, double sigmaX) It blurs an image using a Gaussian filter. 6 integral(Mat src, Mat sum) It calculates the integral of an image. 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; public class main { public static void main( String[] args ) { try{ System.loadLibrary( Core.NATIVE_LIBRARY_NAME ); Mat source = Highgui.imread(“digital_image_processing.jpg”, Highgui.CV_LOAD_IMAGE_COLOR); Mat destination = new Mat(source.rows(),source.cols(),source.type()); destination = source; Imgproc.threshold(source,destination,127,255,Imgproc.THRESH_TOZERO); Highgui.imwrite(“ThreshZero.jpg”, destination); } catch (Exception e) { System.out.println(“error: ” + e.getMessage()); } } } 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 Print Page Previous Next Advertisements ”;

DIP – Enhancing Image Sharpness

Java DIP – Enhancing Image Sharpness ”; Previous Next In this chapter we learn to increase the sharpness of an image using Gaussian filter. First we use OpenCV function GaussianBlur. It can be found under Imgproc package. Its syntax is given below − Imgproc.GaussianBlur(source, destination, new Size(0,0), sigmaX); The parameters are described briefly − Sr.No. Parameter & Description 1 source It is source image. 2 destination It is destination image. 3 Size It is Gaussian kernel size. 4 sigmaX It is Gaussian kernel standard deviation in X direction. Further, we use OpenCV function addWeighted to apply image watermark to image. It can be found under Core package. Its syntax is given below − Core.addWeighted(InputArray src1, alpha, src2, beta, gamma, OutputArray dst); The parameters of this function are described below − Sr.No. Parameter & Description 1 src1 It is first input array. 2 alpha It is weight of the first array elements. 3 src2 It is second input array of the same size and channel number as src1. 4 Beta It is weight of the second array elements. 5 gamma It is scalar added to each sum. 6 dst 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. Method & Description 1 cvtColor(Mat src, Mat dst, int code, int dstCn) It converts an image from one color space to another. 2 dilate(Mat src, Mat dst, Mat kernel) It dilates an image by using a specific structuring element. 3 equalizeHist(Mat src, Mat dst) It equalizes the histogram of a grayscale image. 4 filter2D(Mat src, Mat dst, int depth, Mat kernel, Point anchor, double delta) It convolves an image with the kernel. 5 GaussianBlur(Mat src, Mat dst, Size ksize, double sigmaX) It blurs an image using a Gaussian filter. 6 integral(Mat src, Mat sum) It calculates the integral of an image. 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; public class Main { public static void main( String[] args ) { try{ System.loadLibrary( Core.NATIVE_LIBRARY_NAME ); Mat source = Highgui.imread(“digital_image_processing.jpg”, Highgui.CV_LOAD_IMAGE_COLOR); Mat destination = new Mat(source.rows(),source.cols(),source.type()); Imgproc.GaussianBlur(source, destination, new Size(0,0), 10); Core.addWeighted(source, 1.5, destination, -0.5, 0, destination); Highgui.imwrite(“sharp.jpg”, destination); } catch (Exception e) { } } } Output When you execute the given code, the following output is seen − Original Image Sharped Image Print Page Previous Next Advertisements ”;

DIP – Eroding & Dilation

Java DIP – Eroding and Dilating ”; Previous Next In this chapter we learn apply two very common morphology operators:Dilation and Erosion. We use OpenCV function erode and dilate. They can be found under Imgproc package. Its syntax is given below − Imgproc.erode(source, destination, element); Imgproc.dilate(source, destination, element); The parameters are described below − Sr.No. Parameter & Description 1 source It is Source image. 2 destination It is destination image. 3 element It is a structuring element used for erosion and dilation, if element=Mat(), a 3 x 3 rectangular structuring element is used. Apart from erode() and dilate() methods, there are other methods provided by the Imgproc class. They are described briefly − Sr.No. Method & Description 1 cvtColor(Mat src, Mat dst, int code, int dstCn) It converts an image from one color space to another. 2 dilate(Mat src, Mat dst, Mat kernel) It dilates an image by using a specific structuring element. 3 equalizeHist(Mat src, Mat dst) It equalizes the histogram of a grayscale image. 4 filter2D(Mat src, Mat dst, int depth, Mat kernel, Point anchor, double delta) It convolves an image with the kernel. 5 GaussianBlur(Mat src, Mat dst, Size ksize, double sigmaX) It blurs an image using a Gaussian filter. 6 integral(Mat src, Mat sum) It calculates the integral of an image. Example The following example demonstrates the use of Imgproc class to perform erosion and dilation on 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; public class main { public static void main( String[] args ) { try{ System.loadLibrary( Core.NATIVE_LIBRARY_NAME ); Mat source = Highgui.imread(“digital_image_processing.jpg”, Highgui.CV_LOAD_IMAGE_COLOR); Mat destination = new Mat(source.rows(),source.cols(),source.type()); destination = source; int erosion_size = 5; int dilation_size = 5; Mat element = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(2*erosion_size + 1, 2*erosion_size+1)); Imgproc.erode(source, destination, element); Highgui.imwrite(“erosion.jpg”, destination); source = Highgui.imread(“digital_image_processing.jpg”, Highgui.CV_LOAD_IMAGE_COLOR); destination = source; Mat element1 = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size(2*dilation_size + 1, 2*dilation_size+1)); Imgproc.dilate(source, destination, element1); Highgui.imwrite(“dilation.jpg”, destination); } catch (Exception e) { System.out.println(“error:” + e.getMessage()); } } } Output When you execute the given code, the following output is seen − Original Image On the above original image, some erosion and dilation operations have been performed which have been shown in the output below − Erosion Dilation Print Page Previous Next Advertisements ”;

DIP – Introduction To OpenCV

Java DIP – Introduction to OpenCV ”; Previous Next OpenCV is released under a BSD license and hence it is free for both academic and commercial use. It has C++, C, Python, and Java interfaces, and it supports Windows, Linux, Mac OS, iOS, and Android. OpenCV was designed for computational efficiency and with a strong focus on real-time applications. Written in optimized C/C++, the library can take advantage of multi-core processing. Some of the basic features of OpenCV are described below − Sr.No. Feature & Description 1 Smoothing Images This involves applying Blur, GaussianBlur, medianBlur, and bilateral Filter. 2 Eroding and Dilating It can apply two very common morphology operators − Dilation and Erosion. 3 Morphology Transformations OpenCV function morphologyEx to apply Morphological Transformation such as opening, closing, TopHat, and BlackHat etc. 4 Image Pyramids OpenCV functions pyrUp and pyrDown to down sample or up sample a given image. 4 Basic Thresholding Operations It can perform basic thresholding operations using OpenCV function threshold. 5 Adding borders to your images OpenCV function copyMakeBorder is used to set the borders(extra padding to your image). 7 Remapping In OpenCV, the function remap offers a simple remapping implementation. 8 Histogram Calculation For simple purposes, OpenCV implements the function calcHist, which calculates the histogram of a set of arrays (usually images or image planes). It can operate with up to 32 dimensions. Integrating OpenCV These following steps explain how to integrate OpenCV into your applications. Download OpenCV You can download OpenCV from their official Website here. Create User Library Further, we create a user library of OpenCV, so that we can use it as a future project. Launch Eclipse Select Window -> Preferences from the menu. Navigate under Java -> Build Path -> User Libraries and click New. Now enter the name for your library. For example, OpenCV-2.4.6. After that, select your new user library(i.e. OpenCV-2.4.6) and click on Add External JARs. Browse through C:OpenCV-2.4.6buildjava and select opencv-246.jar. After adding the jar, extend the opencv-246.jar and select Native library location and press Edit. Select External Folder… and browse to select the folder C:OpenCV-2.4.6buildjavax64. If you have a 32-bit system, you need to select the x86 folder instead of x64. Press Ok and you are done. Now your user library is created. Now you can reuse this configuration in any of the project. Create OpenCV Project Create a new java project in eclipse. On the Java Settings step, under Libraries tab, select Add Library… and select OpenCV-2.4.6, then click Finish. Click finish and you are done. Print Page Previous Next Advertisements ”;

DIP – Image Compression Technique

Java DIP – Image Compression Technique ”; Previous Next 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. Further, we get an ImageWriter from getImageWritersByFormatName() method found in the ImageIO class. From this ImageWriter, create an ImageWriteParam object. Its syntax is given below − Iterator<ImageWriter> list = ImageIO.getImageWritersByFormatName(“jpg”); ImageWriteParam obj = writer_From_List.getDefaultWriteParam(); 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); The setCompressionMode() method takes Mode_EXPLICIT as the parameter. Some of the other MODES are described briefly − 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. Method & Description 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() It returns true if the current compression type provides lossless compression. 5 unsetCompression() It removes any previous compression type and quality settings. 6 unsetTiling() It removes any previous tile grid parameters specified by calls to setTiling. 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 Compression { public static void main(String[] args) throws IOException { File input = new File(“digital_image_processing.jpg”); BufferedImage image = ImageIO.read(input); File compressedImageFile = new File(“compress.jpg”); OutputStream os =new FileOutputStream(compressedImageFile); Iterator<ImageWriter>writers = ImageIO.getImageWritersByFormatName(“jpg”); ImageWriter writer = (ImageWriter) writers.next(); ImageOutputStream ios = ImageIO.createImageOutputStream(os); writer.setOutput(ios); ImageWriteParam param = writer.getDefaultWriteParam(); param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT); param.setCompressionQuality(0.05f); writer.write(null, new IIOImage(image, null, null), param); 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 Compressed Image – Quality Factor − 0.5 Print Page Previous Next Advertisements ”;