Learning OpenCV – Erosion work project 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

Learning OpenCV – Filter2D work project 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

Learning OpenCV – Drawing Convex Polylines work project make money

OpenCV – Drawing Convex Polylines You can draw convex polylines on an image using the method fillconvexPoly() of the imgproc class. Following is the syntax of this method. fillConvexPoly(Mat img, MatOfPoint points, Scalar color) This method accepts the following parameters − mat − A Mat object representing the image on which the convex Polylines are to be drawn. points − A MatOfPoint object representing points between which the convex polylines are to be drawn. scalar − A Scalar object representing the color of the convex Polylines. (BGR) The constructor of the MatOfPoint class accepts objects of the class Point. MatOfPoint(Point… a) Example The following program demonstrates how to draw convex polylines 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.MatOfPoint; import org.opencv.core.Point; import org.opencv.core.Scalar; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; public class FillConvexPoly extends Application { Mat matrix = null; @Override public void start(Stage stage) throws Exception { // Capturing the snapshot from the camera FillConvexPoly obj = new FillConvexPoly(); 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 convex Polylines (fill) 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); MatOfPoint matOfPoint = new MatOfPoint ( new Point(75, 100), new Point(350, 100), new Point(75, 150), new Point(350, 150), new Point(75, 200), new Point(350, 200), new Point(75, 250), new Point(350, 250) ); // Drawing polylines Imgproc.fillConvexPoly ( matrix, // Matrix obj of the image matOfPoint, // java.util.List<MatOfPoint> pts new Scalar(0, 0, 255) // Scalar object for color ); // 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 OpenCV – Adding Text work project make money

OpenCV – Adding Text You can add text to an image using the method arrowedLine() of the imgproc class. Following is the syntax of this method. putText(img, text, org, fontFace, fontScale, Scalar color, int thickness) This method accepts the following parameters − mat − A Mat object representing the image to which the text is to be added. text − A string variable of representing the text that is to be added. org − A Point object representing the bottom left corner text string in the image. fontFace − A variable of the type integer representing the font type. fontScale − A variable of the type double representing the scale factor that is multiplied by the font-specific base size. scalar − A Scalar object representing the color of the text that is to be added. (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 add text to 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 AddingTextToImage extends Application { Mat matrix = null; @Override public void start(Stage stage) throws Exception { // Capturing the snapshot from the camera AddingTextToImage obj = new AddingTextToImage(); 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(“Adding text to an 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); // Adding Text Imgproc.putText ( matrix, // Matrix obj of the image “Ravivarma”s Painting”, // Text to be added new Point(10, 50), // point Core.FONT_HERSHEY_SIMPLEX , // front face 1, // front scale new Scalar(0, 0, 0), // Scalar object for color 4 // Thickness ); // 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 OpenCV – Median Blur work project make money

OpenCV – Median Blur The Median blur operation is similar to the other averaging methods. Here, the central element of the image is replaced by the median of all the pixels in the kernel area. This operation processes the edges while removing the noise. You can perform this operation on an image using the medianBlur() method of the imgproc class. Following is the syntax of this method − medianBlur(src, dst, ksize) 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. ksize − A Size object representing the size of the kernel. Example The following program demonstrates how to perform the median blur 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 MedianBlurTest { 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(); // Applying MedianBlur on the Image Imgproc.medianBlur(src, dst, 15); // Writing the image Imgcodecs.imwrite(“E:/OpenCV/chap9/median.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 Processed If you open the specified path, you can observe the output image as follows − Learning working make money

Learning OpenCV – Drawing a Rectangle work project make money

OpenCV – Drawing a Rectangle You can draw a rectangle on an image using the method rectangle() of the imgproc class. Following is the syntax of this method − rectangle(img, pt1, pt2, color, thickness) This method accepts the following parameters − mat − A Mat object representing the image on which the rectangle is to be drawn. pt1 and pt2 − Two Point objects representing the vertices of the rectangle that is to be drawn. 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. Example The following example demonstrates how to draw a rectangle 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 DrawingRectangle extends Application { Mat matrix = null; @Override public void start(Stage stage) throws Exception { // Capturing the snapshot from the camera DrawingRectangle obj = new DrawingRectangle(); 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 Rectangle 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 Rectangle Imgproc.rectangle ( matrix, //Matrix obj of the image new Point(130, 50), //p1 new Point(300, 280), //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 OpenCV – Gaussian Blur work project make money

OpenCV – Gaussian Blur In Gaussian Blur operation, the image is convolved with a Gaussian filter instead of the box filter. The Gaussian filter is a low-pass filter that removes the high-frequency components are reduced. You can perform this operation on an image using the Gaussianblur() method of the imgproc class. Following is the syntax of this method − GaussianBlur(src, dst, ksize, sigmaX) 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. ksize − A Size object representing the size of the kernel. sigmaX − A variable of the type double representing the Gaussian kernel standard deviation in X direction. Example The following program demonstrates how to perform the Gaussian blur 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 GaussianTest { 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(); // Applying GaussianBlur on the Image Imgproc.GaussianBlur(src, dst, new Size(45, 45), 0); // Writing the image Imgcodecs.imwrite(“E:/OpenCV/chap9/Gaussian.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 Processed If you open the specified path, you can observe the output image as follows − Learning working make money

Learning OpenCV – Drawing Polylines work project make money

OpenCV – Drawing Polylines You can draw Polylines on an image using the method polylines() of the imgproc class. Following is the syntax of this method. polylines(img, pts, isClosed, color, thickness) This method accepts the following parameters − mat − A Mat object representing the image on which the Polylines are to be drawn. pts − A List object holding the objects of the type MatOfPoint. isClosed − A parameter of the type boolean specifying weather the polylines are closed. scalar − A Scalar object representing the color of the Polylines. (BGR) thickness − An integer representing the thickness of the Polylines; by default, the value of thickness is 1. The constructor of the MatOfPoint class accepts objects of the class Point. MatOfPoint(Point… a) Example The following program demonstrates how to draw polylines on an image and display it using JavaFX window. import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.InputStream; import java.util.ArrayList; import java.util.List; 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.MatOfPoint; import org.opencv.core.Point; import org.opencv.core.Scalar; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; public class DrawingPolyLines extends Application { Mat matrix = null; @Override public void start(Stage stage) throws Exception { // Capturing the snapshot from the camera DrawingPolyLines obj = new DrawingPolyLines(); 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 Polylines 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); List<MatOfPoint> list = new ArrayList(); list.add( new MatOfPoint ( new Point(75, 100), new Point(350, 100), new Point(75, 150), new Point(350, 150), new Point(75, 200), new Point(350, 200), new Point(75, 250), new Point(350, 250) ) ); // Drawing polylines Imgproc.polylines ( matrix, // Matrix obj of the image list, // java.util.List<MatOfPoint> pts false, // isClosed new Scalar(0, 0, 255), // Scalar object for color 2 // 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 OpenCV – Drawing a Circle work project make money

OpenCV – Drawing a Circle You can draw various shapes like Circle, Rectangle, Line, Ellipse, Polylines, Convex, Polylines, Polylines on an image using the respective methods of the org.opencv.imgproc package. You can draw a circle on an image using the method circle() of the imgproc class. Following is the syntax of this method − circle(img, center, radius, color, thickness) This method accepts the following parameters − mat − A Mat object representing the image on which the circle is to be drawn. point − A Point object representing the center of the circle. radius − A variable of the type integer representing the radius of the circle. scalar − A Scalar object representing the color of the circle. (BGR) thickness − An integer representing the thickness of the circle; by default, the value of thickness is 1. Example The following program demonstrates how to draw a circle 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 DrawingCircle extends Application { Mat matrix = null; @Override public void start(Stage stage) throws Exception { // Capturing the snapshot from the camera DrawingCircle obj = new DrawingCircle(); 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 Circle 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 Circle Imgproc.circle ( matrix, //Matrix obj of the image new Point(230, 160), //Center of the circle 100, //Radius new Scalar(0, 0, 255), //Scalar object for color 10 //Thickness of the circle ); // 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 OpenCV – Colored Image to Binary work project make money

OpenCV – Colored Image to Binary A method called threshold() is used to convert grayscale images to binary image. Following is the syntax of this method. threshold(Mat src, Mat dst, double thresh, double maxval, int type) This method accepts the following parameters − mat − A Mat object representing the input image. dst − A Mat object representing the output image. thresh − An integer representing the threshold value. maxval − An integer representing the maximum value to use with the THRESH_BINARY and THRESH_BINARY_INV thresholding types. type − An integer code representing the type of the conversion, for example, RGB to Grayscale. You can convert a grayscale image to binary image by passing the code Imgproc.THRESH_BINARY along with the values to the remaining parameters. Example The following program demonstrates how to read a colored 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 ColorToBinary 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(“Loading an 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 Imgcodecs class Imgcodecs imageCodecs = new Imgcodecs(); // File input = new File(“C:/EXAMPLES/OpenCV/sample.jpg”); String input = “C:/EXAMPLES/OpenCV/sample.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_GRAY); // 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