Learning OpenCV – Grayscale to Binary work project make money

OpenCV – Grayscale to Binary You can use the same method mentioned in the previous chapter to convert a grayscale image to a binary image. Just pass the path for a grayscale image as input to this program. Example The following program demonstrates how to read a grayscale image as a binary image and display it using JavaFX window. import java.awt.image.BufferedImage; import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; 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; public class GrayScaleToBinary extends Application { @Override public void start(Stage stage) throws Exception { WritableImage writableImage = loadAndConvert(); // Setting the image view ImageView imageView = new ImageView(writableImage); // Setting the position of the image imageView.setX(10); imageView.setY(10); // 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(“Grayscale to binary image”); // Adding scene to the stage stage.setScene(scene); // Displaying the contents of the stage stage.show(); } public WritableImage loadAndConvert() throws Exception { // Loading the OpenCV core library System.loadLibrary( Core.NATIVE_LIBRARY_NAME ); // Instantiating the imagecodecs class Imgcodecs imageCodecs = new Imgcodecs(); String input = “E:/OpenCV/chap7/grayscale.jpg”; // Reading the image Mat src = imageCodecs.imread(input); // Creating the destination matrix Mat dst = new Mat(); // Converting to binary image… Imgproc.threshold(src, dst, 200, 500, Imgproc.THRESH_BINARY); // Extracting data from the transformed image (dst) byte[] data1 = new byte[dst.rows() * dst.cols() * (int)(dst.elemSize())]; dst.get(0, 0, data1); // Creating Buffered image using the data BufferedImage bufImage = new BufferedImage(dst.cols(),dst.rows(), BufferedImage.TYPE_BYTE_BINARY); // Setting the data elements to the image bufImage.getRaster().setDataElements(0, 0, dst.cols(), dst.rows(), data1); // Creating a Writable image WritableImage writableImage = SwingFXUtils.toFXImage(bufImage, null); System.out.println(“Converted to binary”); return writableImage; } public static void main(String args[]) throws Exception { launch(args); } } Input Image Assume that following is the input image sample.jpg specified in the above program. Output Image On executing the program, you will get the following output. Learning working make money

Learning OpenCV – Drawing an Ellipse work project make money

OpenCV – Drawing an Ellipse You can draw an ellipse on an image using the method rectangle() of the imgproc class. Following is the syntax of this method − ellipse(img, box, color, thickness) This method accepts the following parameters − mat − A Mat object representing the image on which the Rectangle is to be drawn. box − A RotatedRect object (The ellipse is drawn inscribed in this rectangle.) scalar − A Scalar object representing the color of the Rectangle. (BGR) thickness − An integer representing the thickness of the Rectangle; by default, the value of thickness is 1. The constructor of the RotatedRect class accepts an object of the class Point, an object of the class Size, and a variable of the type double, as shown below. RotatedRect(Point c, Size s, double a) Example The following program demonstrates how to draw an ellipse on an image and display it using JavaFX window. import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.InputStream; import javax.imageio.ImageIO; 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.MatOfByte; import org.opencv.core.Point; import org.opencv.core.RotatedRect; import org.opencv.core.Scalar; import org.opencv.core.Size; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; public class DrawingEllipse extends Application { Mat matrix = null; @Override public void start(Stage stage) throws Exception { // Capturing the snapshot from the camera DrawingEllipse obj = new DrawingEllipse(); WritableImage writableImage = obj.LoadImage(); // Setting the image view ImageView imageView = new ImageView(writableImage); // setting the fit height and width of the image view imageView.setFitHeight(600); 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(“Drawing Ellipse on the image”); // Adding scene to the stage stage.setScene(scene); // Displaying the contents of the stage stage.show(); } public WritableImage LoadImage() 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/chap8/input.jpg”; Mat matrix = Imgcodecs.imread(file); // Drawing an Ellipse Imgproc.ellipse ( matrix, //Matrix obj of the image new RotatedRect ( // RotatedRect(Point c, Size s, double a) new Point(200, 150), new Size(260, 180), 180 ), new Scalar(0, 0, 255), //Scalar object for color 10 //Thickness of the line ); // Encoding the image MatOfByte matOfByte = new MatOfByte(); Imgcodecs.imencode(“.jpg”, matrix, matOfByte); // Storing the encoded Mat in a byte array byte[] byteArray = matOfByte.toArray(); // Displaying the image InputStream in = new ByteArrayInputStream(byteArray); BufferedImage bufImage = ImageIO.read(in); this.matrix = matrix; // Creating the Writable Image WritableImage writableImage = SwingFXUtils.toFXImage(bufImage, null); return writableImage; } public static void main(String args[]) { launch(args); } } On executing the above program, you will get the following output − Learning working make money

Learning Colored Images to GrayScale work project make money

OpenCV – Colored Images to GrayScale In the earlier chapters, we discussed how to read an input image as different types (binary, grayscale, BGR, etc.). In this chapter, we will learn how to convert one type of image to another. The class named Imgproc of the package org.opencv.imgproc provides methods to convert an image from one color to another. Converting Colored Images to Grayscale A method named cvtColor() is used to convert colored images to grayscale. Following is the syntax of this method. cvtColor(Mat src, Mat dst, int code) This method accepts the following parameters − src − A matrix representing the source. dst − A matrix representing the destination. code − An integer code representing the type of the conversion, for example, RGB to Grayscale. You can convert colored images to gray scale by passing the code Imgproc.COLOR_RGB2GRAY along with the source and destination matrices as a parameter to the cvtColor() method. Example The following program demonstrates how to read a colored image as a grayscale image and display it using JavaFX window. import java.awt.image.BufferedImage; import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; 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; public class ColorToGrayscale extends Application { @Override public void start(Stage stage) throws Exception { WritableImage writableImage = loadAndConvert(); // Setting the image view ImageView imageView = new ImageView(writableImage); // Setting the position of the image imageView.setX(10); imageView.setY(10); // 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(“Colored to grayscale image”); // Adding scene to the stage stage.setScene(scene); // Displaying the contents of the stage stage.show(); } public WritableImage loadAndConvert() throws Exception { //Loading the OpenCV core library System.loadLibrary( Core.NATIVE_LIBRARY_NAME ); String input = “C:/EXAMPLES/OpenCV/sample.jpg”; //Reading the image Mat src = Imgcodecs.imread(input); //Creating the empty destination matrix Mat dst = new Mat(); //Converting the image to gray sacle and saving it in the dst matrix Imgproc.cvtColor(src, dst, Imgproc.COLOR_RGB2GRAY); //Extracting data from the transformed image (dst) byte[] data1 = new byte[dst.rows() * dst.cols() * (int)(dst.elemSize())]; dst.get(0, 0, data1); //Creating Buffered image using the data BufferedImage bufImage = new BufferedImage(dst.cols(),dst.rows(), BufferedImage.TYPE_BYTE_GRAY); //Setting the data elements to the image bufImage.getRaster().setDataElements(0, 0, dst.cols(), dst.rows(), data1); //Creating a WritableImage WritableImage writableImage = SwingFXUtils.toFXImage(bufImage, null); System.out.println(“Converted to Grayscale”); return writableImage; } public static void main(String args[]) throws Exception { launch(args); } } Input Image Assume that following is the input image sample.jpg specified in the above program. Output Image On executing the program, you will get the following output. Learning working make money

Learning OpenCV – Drawing a Line work project make money

OpenCV – Drawing a Line You can draw a line on an image using the method line() of the imgproc class. Following is the syntax of this method. line(img, pt1, pt2, color, thickness) This method accepts the following parameters − mat − A Mat object representing the image on which the line is to be drawn. pt1 and pt2 − Two Point objects representing the points between which the line is to be drawn. scalar − A Scalar object representing the color of the circle. (BGR) thickness − An integer representing the thickness of the line; by default, the value of thickness is 1. Example The following program demonstrates how to draw a line on an image and display it using JavaFX window. import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.InputStream; import javax.imageio.ImageIO; 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.MatOfByte; import org.opencv.core.Point; import org.opencv.core.Scalar; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; public class DrawingLine extends Application { Mat matrix = null; @Override public void start(Stage stage) throws Exception { // Capturing the snapshot from the camera DrawingLine obj = new DrawingLine(); WritableImage writableImage = obj.LoadImage(); // Setting the image view ImageView imageView = new ImageView(writableImage); // setting the fit height and width of the image view imageView.setFitHeight(600); 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(“Drawing a line on the image”); // Adding scene to the stage stage.setScene(scene); // Displaying the contents of the stage stage.show(); } public WritableImage LoadImage() 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/chap8/input.jpg”; Mat matrix = Imgcodecs.imread(file); // Drawing a line Imgproc.line ( matrix, //Matrix obj of the image new Point(10, 200), //p1 new Point(300, 200), //p2 new Scalar(0, 0, 255), //Scalar object for color 5 //Thickness of the line ); // Encoding the image MatOfByte matOfByte = new MatOfByte(); Imgcodecs.imencode(“.jpg”, matrix, matOfByte); // Storing the encoded Mat in a byte array byte[] byteArray = matOfByte.toArray(); // Displaying the image InputStream in = new ByteArrayInputStream(byteArray); BufferedImage bufImage = ImageIO.read(in); this.matrix = matrix; // Creating the Writable Image WritableImage writableImage = SwingFXUtils.toFXImage(bufImage, null); return writableImage; } public static void main(String args[]) { launch(args); } } On executing the above program, you will get the following output − Learning working make money

Learning Reading an Image as Grayscale work project make money

OpenCV – Reading an Image as Grayscale The following program demonstrates how to read a colored image as grayscale and display it using JavaFX window. In here, we have read the image by passing the flag IMREAD_GRAYSCALE along with the String holding the path of a colored image. import java.awt.image.BufferedImage; import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.imgcodecs.Imgcodecs; 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; public class ReadingAsGrayscale extends Application { @Override public void start(Stage stage) throws Exception { WritableImage writableImage = loadAndConvert(); // Setting the image view ImageView imageView = new ImageView(writableImage); // Setting the position of the image imageView.setX(10); imageView.setY(10); // 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(“Reading image as grayscale”); // Adding scene to the stage stage.setScene(scene); // Displaying the contents of the stage stage.show(); } public WritableImage loadAndConvert() throws Exception { // Loading the OpenCV core library System.loadLibrary( Core.NATIVE_LIBRARY_NAME ); // Instantiating the imagecodecs class Imgcodecs imageCodecs = new Imgcodecs(); String input = “C:/EXAMPLES/OpenCV/sample.jpg”; // Reading the image Mat src = imageCodecs.imread(input, Imgcodecs.IMREAD_GRAYSCALE); byte[] data1 = new byte[src.rows() * src.cols() * (int)(src.elemSize())]; src.get(0, 0, data1); // Creating the buffered image BufferedImage bufImage = new BufferedImage(src.cols(),src.rows(), BufferedImage.TYPE_BYTE_GRAY); // Setting the data elements to the image bufImage.getRaster().setDataElements(0, 0, src.cols(), src.rows(), data1); // Creating a WritableImage WritableImage writableImage = SwingFXUtils.toFXImage(bufImage, null); System.out.println(“Image Read”); return writableImage; } public static void main(String args[]) throws Exception { launch(args); } } Input Image Assume that following is the input image sample.jpg specified in the above program. Output Image On executing the program, you will get the following output. Learning working make money

Learning OpenCV – Color Maps work project make money

OpenCV – Color Maps In OpenCV, you can apply different color maps to an image using the method applyColorMap() of the class Imgproc. Following is the syntax of this method − applyColorMap(Mat src, Mat dst, int colormap) It accepts three parameters − src − An object of the class Mat representing the source (input) image. dst − An object of the class Mat representing the destination (output) image. colormap − A variable of integer type representing the type of the color map to be applied. Example The following program demonstrates how to apply color map to an image. import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; public class ColorMapTest { 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/chap25/color_input.jpg”; Mat src = Imgcodecs.imread(file); // Creating an empty matrix to store the result Mat dst = new Mat(); // Applying color map to an image Imgproc.applyColorMap(src, dst, Imgproc.COLORMAP_HOT); // Writing the image Imgcodecs.imwrite(“E:/OpenCV/chap25/colormap_hot.jpg”, dst); System.out.println(“Image processed”); } } Assume that following is the input image color_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 − More Operations In addition to COLORMAP_HOT demonstrated in the previous example, OpenCV caters various other types of color maps. All these types are represented by predefined static fields (fixed values) of Imgproc class. You can choose the type of the colormap you need, by passing its respective predefined value to the parameter named colormap of the applyColorMap() method. Imgproc.applyColorMap(src, dst, Imgproc.COLORMAP_HOT); Following are the values representing various types of color maps and their respective outputs. Operation and Description Output COLORMAP_AUTUMN COLORMAP_BONE COLORMAP_COOL COLORMAP_HOT COLORMAP_HSV COLORMAP_JET COLORMAP_OCEAN COLORMAP_PARULA COLORMAP_PINK COLORMAP_RAINBOW COLORMAP_SPRING COLORMAP_SUMMER COLORMAP_WINTER Learning working make money

Learning OpenCV – Reading Image as BGR work project make money

OpenCV – Reading Image as BGR The following program demonstrates how to read a colored image as BGR type image and display it using JavaFX window. In here, we have read the image by passing the flag IMREAD_COLOR to the method imread() along with the String holding the path of a colored image. import java.awt.image.BufferedImage; import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.imgcodecs.Imgcodecs; 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; public class ReadingAsColored extends Application { @Override public void start(Stage stage) throws Exception { WritableImage writableImage = loadAndConvert(); // Setting the image view ImageView imageView = new ImageView(writableImage); // Setting the position of the image imageView.setX(10); imageView.setY(10); // 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(“Reading as colored image”); // Adding scene to the stage stage.setScene(scene); // Displaying the contents of the stage stage.show(); } public WritableImage loadAndConvert() throws Exception { // Loading the OpenCV core library System.loadLibrary( Core.NATIVE_LIBRARY_NAME ); String input = “C:/EXAMPLES/OpenCV/sample.jpg”; Mat dst = new Mat(); // Reading the image Mat src = Imgcodecs.imread(input, Imgcodecs.IMREAD_COLOR); byte[] data1 = new byte[src.rows() * src.cols() * (int)(src.elemSize())]; src.get(0, 0, data1); // Creating the buffered image BufferedImage bufImage = new BufferedImage(src.cols(),src.rows(), BufferedImage.TYPE_3BYTE_BGR); // Setting the data elements to the image bufImage.getRaster().setDataElements(0, 0, src.cols(), src.rows(), data1); // Creating a WritableImage WritableImage writableImage = SwingFXUtils.toFXImage(bufImage, null); System.out.println(“Image read”); return writableImage; } public static void main(String args[]) throws Exception { launch(args); } } Input Image Assume that following is the input image sample.jpg specified in the above program. Output Image On executing the program, you will get the following output. Learning working make money

Learning OpenCV – Face Detection in a Picture work project make money

OpenCV – Face Detection in a Picture The VideoCapture class of the org.opencv.videoio package contains classes and methods to capture video using the system camera. Let’s go step by step and learn how to do it. Step 1: Load the OpenCV native library While writing Java code using OpenCV library, the first step you need to do is to load the native library of OpenCV using the loadLibrary(). Load the OpenCV native library as shown below. // Loading the core library System.loadLibrary(Core.NATIVE_LIBRARY_NAME); Step 2: Instantiate the CascadeClassifier class The CascadeClassifier class of the package org.opencv.objdetect is used to load the classifier file. Instantiate this class by passing the xml file lbpcascade_frontalface.xml as shown below. // Instantiating the CascadeClassifier String xmlFile = “E:/OpenCV/facedetect/lbpcascade_frontalface.xml”; CascadeClassifier classifier = new CascadeClassifier(xmlFile); Step 3: Detect the faces You can detect the faces in the image using method detectMultiScale() of the class named CascadeClassifier. This method accepts an object of the class Mat holding the input image and an object of the class MatOfRect to store the detected faces. // Detecting the face in the snap MatOfRect faceDetections = new MatOfRect(); classifier.detectMultiScale(src, faceDetections); Example The following program demonstrates how to detect faces in an image. 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; public class FaceDetectionImage { 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/chap23/facedetection_input.jpg”; Mat src = Imgcodecs.imread(file); // Instantiating the CascadeClassifier String xmlFile = “E:/OpenCV/facedetect/lbpcascade_frontalface.xml”; CascadeClassifier classifier = new CascadeClassifier(xmlFile); // Detecting the face in the snap MatOfRect faceDetections = new MatOfRect(); classifier.detectMultiScale(src, faceDetections); System.out.println(String.format(“Detected %s faces”, faceDetections.toArray().length)); // Drawing boxes for (Rect rect : faceDetections.toArray()) { Imgproc.rectangle( src, // 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), 3 // RGB colour ); } // Writing the image Imgcodecs.imwrite(“E:/OpenCV/chap23/facedetect_output1.jpg”, src); System.out.println(“Image Processed”); } } Assume that following is the input image facedetection_input.jpg specified in the above program. Output On executing the program, you will get the following output − Detected 3 faces Image Processed If you open the specified path, you can observe the output image as follows − Learning working make money

Learning OpenCV – Image Pyramids work project make money

OpenCV – Image Pyramids Pyramid is an operation on an image where, An input image is initially smoothed using a particular smoothing filter (ex: Gaussian, Laplacian) and then the smoothed image is subsampled. This process is repeated multiple times. During the pyramid operation, the smoothness of the image is increased and the resolution (size) is decreased. Pyramid Up In Pyramid Up, the image is initially up-sampled and then blurred. You can perform Pyramid Up operation on an image using the pyrUP() method of the imgproc class. Following is the syntax of this method − pyrUp(src, dst, dstsize, borderType) This method accepts the following parameters − src − An object of the class Mat representing the source (input) image. mat − An object of the class Mat representing the destination (output) image. size − An object of the class Size representing the size to which the image is to be increased or decreased. borderType − A variable of integer type representing the type of border to be used. Example The following program demonstrates how to perform the Pyramid Up operation on an 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 PyramidUp { 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/chap13/pyramid_input.jpg”; Mat src = Imgcodecs.imread(file); // Creating an empty matrix to store the result Mat dst = new Mat(); // Applying pyrUp on the Image Imgproc.pyrUp(src, dst, new Size(src.cols()*2, src.rows()*2), Core.BORDER_DEFAULT); // Writing the image Imgcodecs.imwrite(“E:/OpenCV/chap13/pyrUp_output.jpg”, dst); System.out.println(“Image Processed”); } } Assume that following is the input image pyramid_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 − Pyramid Down In Pyramid Down, the image is initially blurred and then down-sampled. You can perform Pyramid Down operation on an image using the pyrDown() method of the imgproc class. Following is the syntax of this method − pyrDown(src, dst, dstsize, borderType) This method accepts the following parameters − src − An object of the class Mat representing the source (input) image. mat − An object of the class Mat representing the destination (output) image. size − An object of the class Size representing the size to which the image is to be increased or decreased. borderType − A variable of integer type representing the type of border to be used. Example The following program demonstrates how to perform the Pyramid Down operation on an 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 PyramidDown { 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/chap13/pyramid_input.jpg”; Mat src = Imgcodecs.imread(file); // Creating an empty matrix to store the result Mat dst = new Mat(); // Applying pyrDown on the Image Imgproc.pyrDown(src, dst, new Size(src.cols()/2, src.rows()/2), Core.BORDER_DEFAULT); // Writing the image Imgcodecs.imwrite(“E:/OpenCV/chap13/pyrDown_output.jpg”, dst); System.out.println(“Image Processed”); } } Assume that following is the input image pyramid_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 − Mean Shift Filtering In Mean Shifting pyramid operation, an initial step of mean shift segmentation of an image is carried out. You can perform pyramid Mean Shift Filtering operation on an image using the pyrDown() method of the imgproc class. Following is the syntax of this method. pyrMeanShiftFiltering(src, dst, sp, sr) This method accepts the following parameters − src − An object of the class Mat representing the source (input) image. mat − An object of the class Mat representing the destination (output) image. sp − A variable of the type double representing the spatial window radius. sr − A variable of the type double representing the color window radius. Example The following program demonstrates how to perform a Mean Shift Filtering 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 PyramidMeanShift { 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/chap13/pyramid_input.jpg”; Mat src = Imgcodecs.imread(file); // Creating an empty matrix to store the result Mat dst = new Mat(); // Applying meanShifting on the Image Imgproc.pyrMeanShiftFiltering(src, dst, 200, 300); // Writing the image Imgcodecs.imwrite(“E:/OpenCV/chap13/meanShift_output.jpg”, dst); System.out.println(“Image Processed”); } } Assume that following is the input image pyramid_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

Learning OpenCV – The IMREAD_XXX Flag work project make money

OpenCV – The IMREAD_XXX Flag OpenCV supports various types of images such as colored, binary, grayscale, etc. Using the imread() method and predefined fields of the Imgcodecs class, you can read a given image as another type. The flags parameter of imread() method (IMREAD_XXX) In the earlier chapters, we have seen the syntax of imread() method of the Imgcodecs class. It accepts a string argument representing the location of the image that is to be read. imread(filename) The imread() method has another syntax. imread(filename, int flags) This syntax accepts two parameters − filename − It accepts an argument (filename), a variable of the String type representing the path of the file that is to be read. flags − An integer value representing a predefined flag value. For each value, this reads the given image as a specific type (gray scale color etc.) Following is the table listing various fields provided in the Imgproc class as values for this parameter. S.No Fields and Description 1 IMREAD_COLOR If the flag is set to this value, the loaded image will be converted to a 3-channel BGR (Blue Green Red) color image. 2 IMREAD_GRAYSCALE If the flag is set to this value, the loaded image will be converted to a single-channel grayscale image. 3 IMREAD_LOAD_GDAL If the flag is set to this value, you can load the image using the gdal driver. 4 IMREAD_ANYCOLOR If the flag is set to this value, the image is read in any possible color format. 5 IMREAD_REDUCED_COLOR_2 IMREAD_REDUCED_COLOR_4 IMREAD_REDUCED_COLOR_8 If the flag is set to this value, the image is read as three-channel BGR, and the size of the image is reduced to ½, ¼th or ⅛th of the original size of the image with respect to the field used. 6 IMREAD_REDUCED_GRAYSCALE_2 IMREAD_REDUCED_GRAYSCALE_4 IMREAD_REDUCED_GRAYSCALE_8 If the flag is set to this value, the image is read as a single-channel grayscale image, and the size of the image is reduced to ½, ¼th or ⅛th of the original size of the image with respect to the field used. 7 IMREAD_UNCHANGED If the flag is set to this value, the loaded image is returned as it is. Learning working make money