OpenCV – Histogram Equalization The histogram of an image shows the frequency of pixels’ intensity values. In an image histogram, the X-axis shows the gray level intensities and the Y-axis shows the frequency of these intensities. Histogram equalization improves the contrast of an image, in order to stretch out the intensty range. You can equalize the histogram of a given image using the method equalizeHist() of the Imgproc class. Following is the syntax of this method. equalizeHist(src, dst) This method accepts the following parameters − src − An object of the class Mat representing the source (input) image. dst − An object of the class Mat representing the output. (Image obtained after equalizing the histogram) Example The following program demonstrates how to equalize the histogram of a given image. import java.util.ArrayList; import java.util.List; import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.Size; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; public class HistoTest { public static void main (String[] args) { // Loading the OpenCV core library System.loadLibrary( Core.NATIVE_LIBRARY_NAME ); // Reading the Image from the file and storing it in to a Matrix object String file =”E:/OpenCV/chap20/histo_input.jpg”; // Load the image Mat img = Imgcodecs.imread(file); // Creating an empty matrix Mat equ = new Mat(); img.copyTo(equ); // Applying blur Imgproc.blur(equ, equ, new Size(3, 3)); // Applying color Imgproc.cvtColor(equ, equ, Imgproc.COLOR_BGR2YCrCb); List<Mat> channels = new ArrayList<Mat>(); // Splitting the channels Core.split(equ, channels); // Equalizing the histogram of the image Imgproc.equalizeHist(channels.get(0), channels.get(0)); Core.merge(channels, equ); Imgproc.cvtColor(equ, equ, Imgproc.COLOR_YCrCb2BGR); Mat gray = new Mat(); Imgproc.cvtColor(equ, gray, Imgproc.COLOR_BGR2GRAY); Mat grayOrig = new Mat(); Imgproc.cvtColor(img, grayOrig, Imgproc.COLOR_BGR2GRAY); Imgcodecs.imwrite(“E:/OpenCV/chap20/histo_output.jpg”, equ); System.out.println(“Image Processed”); } } Assume that following is the input image histo_input.jpg specified in the above program. Output On executing the program, you will get the following output − Image Processed If you open the specified path, you can observe the output image as follows − Learning working make money
Category: opencv
OpenCV – Canny Edge Detection Canny Edge Detection is used to detect the edges in an image. It accepts a gray scale image as input and it uses a multistage algorithm. You can perform this operation on an image using the Canny() method of the imgproc class, following is the syntax of this method. Canny(image, edges, threshold1, threshold2) This method accepts the following parameters − image − A Mat object representing the source (input image) for this operation. edges − A Mat object representing the destination (edges) for this operation. threshold1 − A variable of the type double representing the first threshold for the hysteresis procedure. threshold2 − A variable of the type double representing the second threshold for the hysteresis procedure. Example Following program is an example demonstrating, how to perform Canny Edge Detection operation on a given image. import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; public class CannyEdgeDetection { public static void main(String args[]) throws Exception { // Loading the OpenCV core library System.loadLibrary(Core.NATIVE_LIBRARY_NAME); // Reading the Image from the file and storing it in to a Matrix object String file = “E:/OpenCV/chap17/canny_input.jpg”; // Reading the image Mat src = Imgcodecs.imread(file); // Creating an empty matrix to store the result Mat gray = new Mat(); // Converting the image from color to Gray Imgproc.cvtColor(src, gray, Imgproc.COLOR_BGR2GRAY); Mat edges = new Mat(); // Detecting the edges Imgproc.Canny(gray, edges, 60, 60*3); // Writing the image Imgcodecs.imwrite(“E:/OpenCV/chap17/canny_output.jpg”, edges); System.out.println(“Image Loaded”); } } Assume that following is the input image canny_input.jpg specified in the above program. Output On executing the above program, you will get the following output − Image Processed If you open the specified path, you can observe the output image as follows − Learning working make money
OpenCV – Quick Guide OpenCV – Overview OpenCV is a cross-platform library using which we can develop real-time computer vision applications. It mainly focuses on image processing, video capture and analysis including features like face detection and object detection. Let’s start the chapter by defining the term “Computer Vision”. Computer Vision Computer Vision can be defined as a discipline that explains how to reconstruct, interrupt, and understand a 3D scene from its 2D images, in terms of the properties of the structure present in the scene. It deals with modeling and replicating human vision using computer software and hardware. Computer Vision overlaps significantly with the following fields − Image Processing − It focuses on image manipulation. Pattern Recognition − It explains various techniques to classify patterns. Photogrammetry − It is concerned with obtaining accurate measurements from images. Computer Vision Vs Image Processing Image processing deals with image-to-image transformation. The input and output of image processing are both images. Computer vision is the construction of explicit, meaningful descriptions of physical objects from their image. The output of computer vision is a description or an interpretation of structures in 3D scene. Applications of Computer Vision Here we have listed down some of major domains where Computer Vision is heavily used. Robotics Application Localization − Determine robot location automatically Navigation Obstacles avoidance Assembly (peg-in-hole, welding, painting) Manipulation (e.g. PUMA robot manipulator) Human Robot Interaction (HRI) − Intelligent robotics to interact with and serve people Medicine Application Classification and detection (e.g. lesion or cells classification and tumor detection) 2D/3D segmentation 3D human organ reconstruction (MRI or ultrasound) Vision-guided robotics surgery Industrial Automation Application Industrial inspection (defect detection) Assembly Barcode and package label reading Object sorting Document understanding (e.g. OCR) Security Application Biometrics (iris, finger print, face recognition) Surveillance − Detecting certain suspicious activities or behaviors Transportation Application Autonomous vehicle Safety, e.g., driver vigilance monitoring Features of OpenCV Library Using OpenCV library, you can − Read and write images Capture and save videos Process images (filter, transform) Perform feature detection Detect specific objects such as faces, eyes, cars, in the videos or images. Analyze the video, i.e., estimate the motion in it, subtract the background, and track objects in it. OpenCV was originally developed in C++. In addition to it, Python and Java bindings were provided. OpenCV runs on various Operating Systems such as windows, Linux, OSx, FreeBSD, Net BSD, Open BSD, etc. This tutorial explains the concepts of OpenCV with examples using Java bindings. OpenCV Library Modules Following are the main library modules of the OpenCV library. Core Functionality This module covers the basic data structures such as Scalar, Point, Range, etc., that are used to build OpenCV applications. In addition to these, it also includes the multidimensional array Mat, which is used to store the images. In the Java library of OpenCV, this module is included as a package with the name org.opencv.core. Image Processing This module covers various image processing operations such as image filtering, geometrical image transformations, color space conversion, histograms, etc. In the Java library of OpenCV, this module is included as a package with the name org.opencv.imgproc. Video This module covers the video analysis concepts such as motion estimation, background subtraction, and object tracking. In the Java library of OpenCV, this module is included as a package with the name org.opencv.video. Video I/O This module explains the video capturing and video codecs using OpenCV library. In the Java library of OpenCV, this module is included as a package with the name org.opencv.videoio. calib3d This module includes algorithms regarding basic multiple-view geometry algorithms, single and stereo camera calibration, object pose estimation, stereo correspondence and elements of 3D reconstruction. In the Java library of OpenCV, this module is included as a package with the name org.opencv.calib3d. features2d This module includes the concepts of feature detection and description. In the Java library of OpenCV, this module is included as a package with the name org.opencv.features2d. Objdetect This module includes the detection of objects and instances of the predefined classes such as faces, eyes, mugs, people, cars, etc. In the Java library of OpenCV, this module is included as a package with the name org.opencv.objdetect. Highgui This is an easy-to-use interface with simple UI capabilities. In the Java library of OpenCV, the features of this module is included in two different packages namely, org.opencv.imgcodecs and org.opencv.videoio. A Brief History of OpenCV OpenCV was initially an Intel research initiative to advise CPU-intensive applications. It was officially launched in 1999. In the year 2006, its first major version, OpenCV 1.0 was released. In October 2009, the second major version, OpenCV 2 was released. In August 2012, OpenCV was taken by a nonprofit organization OpenCV.org. OpenCV – Environment In this chapter, you will learn how to install OpenCV and set up its environment in your system. Installing OpenCV First of all, you need to download OpenCV onto your system. Follow the steps given below. Step 1 − Open the homepage of OpenCV by clicking the following link: On clicking, you will see its homepage as shown below. Step 2 − Now, click the Downloads link highlighted in the above screenshot. On clicking, you will be directed to the downloads page of OpenCV. Step 3 − On clicking the highlighted link in the above screenshot, a file named opencv-3.1.0.exe will be downloaded. Extract this file to generate a folder opencv in your system, as shown in the following screenshot. Step 4 − Open the folder OpenCV → build → java. Here you will find the jar file of OpenCV named opencv-310.jar. Save this file in a separate folder for further use. Eclipse Installation After downloading the required JAR files, you have to embed these JAR files to your Eclipse environment. You can do this by setting the Build Path to these JAR files and by using pom.xml. Setting Build Path Following are the steps to set up OpenCV in Eclipse − Step 1 − Ensure that you have installed Eclipse in your system. If not, download
OpenCV – Face Detection using Camera The following program demonstrates how to detect faces using system camera and display it using JavaFX window. Example import java.awt.image.BufferedImage; import java.awt.image.DataBufferByte; import java.awt.image.WritableRaster; import java.io.FileNotFoundException; import java.io.IOException; import javafx.application.Application; import javafx.embed.swing.SwingFXUtils; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.image.ImageView; import javafx.scene.image.WritableImage; import javafx.stage.Stage; import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.MatOfRect; import org.opencv.core.Point; import org.opencv.core.Rect; import org.opencv.core.Scalar; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; import org.opencv.objdetect.CascadeClassifier; import org.opencv.videoio.VideoCapture; public class faceDetectionJavaFXX extends Application { Mat matrix = null; @Override public void start(Stage stage) throws FileNotFoundException, IOException { // Capturing the snapshot from the camera faceDetectionJavaFXX obj = new faceDetectionJavaFXX(); WritableImage writableImage = obj.capureFrame(); // Saving the image obj.saveImage(); // Setting the image view ImageView imageView = new ImageView(writableImage); // setting the fit height and width of the image view imageView.setFitHeight(400); imageView.setFitWidth(600); // Setting the preserve ratio of the image view imageView.setPreserveRatio(true); // Creating a Group object Group root = new Group(imageView); // Creating a scene object Scene scene = new Scene(root, 600, 400); // Setting title to the Stage stage.setTitle(“Capturing an image”); // Adding scene to the stage stage.setScene(scene); // Displaying the contents of the stage stage.show(); } public WritableImage capureFrame() { WritableImage writableImage = null; // Loading the OpenCV core library System.loadLibrary( Core.NATIVE_LIBRARY_NAME ); // Instantiating the VideoCapture class (camera:: 0) VideoCapture capture = new VideoCapture(0); // Reading the next video frame from the camera Mat matrix = new Mat(); capture.read(matrix); // If camera is opened if(!capture.isOpened()) { System.out.println(“camera not detected”); } else System.out.println(“Camera detected “); // If there is next video frame if (capture.read(matrix)) { /////// Detecting the face in the snap ///// String file = “E:/OpenCV/facedetect/lbpcascade_frontalface.xml”; CascadeClassifier classifier = new CascadeClassifier(file); MatOfRect faceDetections = new MatOfRect(); classifier.detectMultiScale(matrix, faceDetections); System.out.println(String.format(“Detected %s faces”, faceDetections.toArray().length)); // Drawing boxes for (Rect rect : faceDetections.toArray()) { Imgproc.rectangle( matrix, //where to draw the box new Point(rect.x, rect.y), //bottom left new Point(rect.x + rect.width, rect.y + rect.height), //top right new Scalar(0, 0, 255) //RGB colour ); } // Creating BuffredImage from the matrix BufferedImage image = new BufferedImage(matrix.width(), matrix.height(), BufferedImage.TYPE_3BYTE_BGR); WritableRaster raster = image.getRaster(); DataBufferByte dataBuffer = (DataBufferByte) raster.getDataBuffer(); byte[] data = dataBuffer.getData(); matrix.get(0, 0, data); this.matrix = matrix; // Creating the Writable Image writableImage = SwingFXUtils.toFXImage(image, null); } return writableImage; } public void saveImage() { // Saving the Image String file = “E:/OpenCV/chap23/facedetected.jpg”; // Instantiating the imagecodecs class Imgcodecs imageCodecs = new Imgcodecs(); // Saving it again imageCodecs.imwrite(file, matrix); } public static void main(String args[]) { launch(args); } } Output On executing the program, you will get the following output. If you open the specified path, you can see the same snapshot saved as a jpg image. Learning working make money
OpenCV – Affine Translation You can perform affine translation on an image using the warpAffine() method of the imgproc class. Following is the syntax of this method − Imgproc.warpAffine(src, dst, tranformMatrix, size); This method accepts the following parameters − src − A Mat object representing the source (input image) for this operation. dst − A Mat object representing the destination (output image) for this operation. tranformMatrix − A Mat object representing the transformation matrix. size − A variable of the type integer representing the size of the output image. Example The following program demonstrates how to apply affine operation on a given image. import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.MatOfPoint2f; import org.opencv.core.Point; import org.opencv.core.Size; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; public class AffineTranslation { public static void main(String args[]) { // Loading the OpenCV core library System.loadLibrary( Core.NATIVE_LIBRARY_NAME ); // Reading the Image from the file and storing it in to a Matrix object String file =”E:/OpenCV/chap24/transform_input.jpg”; Mat src = Imgcodecs.imread(file); //Creating an empty matrix to store the result Mat dst = new Mat(); Point p1 = new Point( 0,0 ); Point p2 = new Point( src.cols() – 1, 0 ); Point p3 = new Point( 0, src.rows() – 1 ); Point p4 = new Point( src.cols()*0.0, src.rows()*0.33 ); Point p5 = new Point( src.cols()*0.85, src.rows()*0.25 ); Point p6 = new Point( src.cols()*0.15, src.rows()*0.7 ); MatOfPoint2f ma1 = new MatOfPoint2f(p1,p2,p3); MatOfPoint2f ma2 = new MatOfPoint2f(p4,p5,p6); // Creating the transformation matrix Mat tranformMatrix = Imgproc.getAffineTransform(ma1,ma2); // Creating object of the class Size Size size = new Size(src.cols(), src.cols()); // Applying Wrap Affine Imgproc.warpAffine(src, dst, tranformMatrix, size); // Writing the image Imgcodecs.imwrite(“E:/OpenCV/chap24/Affinetranslate.jpg”, dst); System.out.println(“Image Processed”); } } Assume that following is the input image transform_input.jpg specified in the above program. Output On executing it, you will get the following output − Image Processed If you open the specified path, you can observe the output image as follows − Learning working make money
OpenCV – Laplacian Transformation Laplacian Operator is also a derivative operator which is used to find edges in an image. It is a second order derivative mask. In this mask we have two further classifications one is Positive Laplacian Operator and other is Negative Laplacian Operator. Unlike other operators Laplacian didn’t take out edges in any particular direction but it takes out edges in following classification. Inward Edges Outward Edges You can perform Laplacian Transform operation on an image using the Laplacian() method of the imgproc class, following is the syntax of this method. Laplacian(src, dst, ddepth) This method accepts the following parameters − src − A Mat object representing the source (input image) for this operation. dst − A Mat object representing the destination (output image) for this operation. ddepth − A variable of the type integer representing depth of the destination image. Example The following program demonstrates how to perform Laplace transform operation on a given image. import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; public class LaplacianTest { public static void main(String args[]) { // Loading the OpenCV core library System.loadLibrary(Core.NATIVE_LIBRARY_NAME); //Reading the Image from the file and storing it in to a Matrix object String file =”E:/OpenCV/chap18/laplacian_input.jpg”; Mat src = Imgcodecs.imread(file); // Creating an empty matrix to store the result Mat dst = new Mat(); // Applying GaussianBlur on the Image Imgproc.Laplacian(src, dst, 10); // Writing the image Imgcodecs.imwrite(“E:/OpenCV/chap18/laplacian.jpg”, dst); System.out.println(“Image Processed”); } } Assume that following is the input image laplacian_input.jpg specified in the above program. Output On executing the program, you will get the following output − Image Processed If you open the specified path, you can observe the output image as follows − Learning working make money
OpenCV – Erosion Erosion is quite a similar process as dilation. But the pixel value computed here is minimum rather than maximum in dilation. The image is replaced under the anchor point with that minimum pixel value. With this procedure, the areas of dark regions grow in size and bright regions reduce. For example, the size of an object in dark shade or black shade increases, while it decreases in white shade or bright shade. Example You can perform this operation on an image using the erode() method of the imgproc class. Following is the syntax of this method − erode(src, dst, kernel) This method accepts the following parameters − src − A Mat object representing the source (input image) for this operation. dst − A Mat object representing the destination (output image) for this operation. kernel − A Mat object representing the kernel. You can prepare the kernel matrix using the getStructuringElement() method. This method accepts an integer representing the morph_rect type and an object of the type Size. Imgproc.getStructuringElement(int shape, Size ksize); The following program demonstrates how to perform the erosion operation on a given image. import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.Size; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; public class ErodeTest { public static void main( String[] args ) { // Loading the OpenCV core library System.loadLibrary( Core.NATIVE_LIBRARY_NAME ); // Reading the Image from the file and storing it in to a Matrix object String file =”C:/EXAMPLES/OpenCV/sample.jpg”; Mat src = Imgcodecs.imread(file); // Creating an empty matrix to store the result Mat dst = new Mat(); // Preparing the kernel matrix object Mat kernel = Imgproc.getStructuringElement(Imgproc.MORPH_RECT, new Size((2*2) + 1, (2*2)+1)); // Applying erode on the Image Imgproc.erode(src, dst, kernel); // Writing the image Imgcodecs.imwrite(“E:/OpenCV/chap10/Erosion.jpg”, dst); System.out.println(“Image processed”); } } Assume that following is the input image sample.jpg specified in the above program. Output On executing the program, you will get the following output − Image Loaded If you open the specified path, you can observe the output image as follows − Learning working make money
OpenCV – Filter2D The Filter2D operation convolves an image with the kernel. You can perform this operation on an image using the Filter2D() method of the imgproc class. Following is the syntax of this method − filter2D(src, dst, ddepth, kernel) This method accepts the following parameters − src − A Mat object representing the source (input image) for this operation. dst − A Mat object representing the destination (output image) for this operation. ddepth − A variable of the type integer representing the depth of the output image. kernel − A Mat object representing the convolution kernel. Example The following program demonstrates how to perform the Filter2D operation on an image. import org.opencv.core.Core; import org.opencv.core.CvType; import org.opencv.core.Mat; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; public class Filter2D { public static void main( String[] args ) { //Loading the OpenCV core library System.loadLibrary( Core.NATIVE_LIBRARY_NAME ); //Reading the Image from the file and storing it in to a Matrix object String file =”E:/OpenCV/chap11/filter_input.jpg”; Mat src = Imgcodecs.imread(file); //Creating an empty matrix to store the result Mat dst = new Mat(); // Creating kernel matrix Mat kernel = Mat.ones(2,2, CvType.CV_32F); for(int i = 0; i<kernel.rows(); i++) { for(int j = 0; j<kernel.cols(); j++) { double[] m = kernel.get(i, j); for(int k = 1; k<m.length; k++) { m[k] = m[k]/(2 * 2); } kernel.put(i,j, m); } } Imgproc.filter2D(src, dst, -1, kernel); Imgcodecs.imwrite(“E:/OpenCV/chap11/filter2d.jpg”, dst); System.out.println(“Image Processed”); } } Assume that following is the input image filter_input.jpg specified in the above program. Output On executing the program, you will get the following output − Image Processed If you open the specified path, you can observe the output image as follows − Learning working make money
OpenCV – Bilateral Filter Image filtering allows you to apply various effects to an image. In this chapter and the subsequent three chapters, we are going to discuss various filter operations such as Bilateral Filter, Box Filter, SQR Box Filter and Filter2D. Bilateral Filter The Bilateral Filter operation applies a bilateral image to a filter. You can perform this operation on an image using the medianBlur() method of the imgproc class. Following is the syntax of this method. bilateralFilter(src, dst, d, sigmaColor, sigmaSpace, borderType) This method accepts the following parameters − src − A Mat object representing the source (input image) for this operation. dst − A Mat object representing the destination (output image) for this operation. d − A variable of the type integer representing the diameter of the pixel neighborhood. sigmaColor − A variable of the type integer representing the filter sigma in the color space. sigmaSpace − A variable of the type integer representing the filter sigma in the coordinate space. borderType − An integer object representing the type of the border used. Example The following program demonstrates how to perform the Bilateral Filter operation on an image. import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; public class BilateralFilter { public static void main(String args[]) { // Loading the OpenCV core library System.loadLibrary( Core.NATIVE_LIBRARY_NAME ); // Reading the Image from the file and storing it in to a Matrix object String file =”E:/OpenCV/chap11/filter_input.jpg”; Mat src = Imgcodecs.imread(file); // Creating an empty matrix to store the result Mat dst = new Mat(); // Applying Bilateral filter on the Image Imgproc.bilateralFilter(src, dst, 15, 80, 80, Core.BORDER_DEFAULT); // Writing the image Imgcodecs.imwrite(“E:/OpenCV/chap11/bilateralfilter.jpg”, dst); System.out.println(“Image Processed”); } } Assume that following is the input image filter_input.jpg specified in the above program. Output On executing the program, you will get the following output − Image Processed If you open the specified path, you can observe the output image as follows − Learning working make money
OpenCV – Box Filter The Box Filter operation is similar to the averaging blur operation; it applies a bilateral image to a filter. Here, you can choose whether the box should be normalized or not. You can perform this operation on an image using the boxFilter() method of the imgproc class. Following is the syntax of this method − boxFilter(src, dst, ddepth, ksize, anchor, normalize, borderType) This method accepts the following parameters − src − A Mat object representing the source (input image) for this operation. dst − A Mat object representing the destination (output image) for this operation. ddepth − A variable of the type integer representing the depth of the output image. ksize − A Size object representing the size of the blurring kernel. anchor − A variable of the type integer representing the anchor point. Normalize − A variable of the type boolean specifying weather the kernel should be normalized. borderType − An integer object representing the type of the border used. Example The following program demonstrates how to perform the Box Filter operation on an image. import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.Point; import org.opencv.core.Size; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; public class BoxFilterTest { public static void main( String[] args ) { // Loading the OpenCV core library System.loadLibrary( Core.NATIVE_LIBRARY_NAME ); // Reading the Image from the file and storing it in to a Matrix object String file = “E:/OpenCV/chap11/filter_input.jpg”; Mat src = Imgcodecs.imread(file); // Creating an empty matrix to store the result Mat dst = new Mat(); // Creating the objects for Size and Point Size size = new Size(45, 45); Point point = Point(-1, -1); // Applying Box Filter effect on the Image Imgproc.boxFilter(src, dst, 50, size, point, true, Core.BORDER_DEFAULT); // Writing the image Imgcodecs.imwrite(“E:/OpenCV/chap11/boxfilterjpg”, dst); System.out.println(“Image Processed”); } } Assume that following is the input image filter_input.jpg specified in the above program. Output On executing the program, you will get the following output − Image Processed If you open the specified path, you can observe the output image as follows − Learning working make money