
- Example - Home
- Example - Environment
- Example - Strings
- Example - Arrays
- Example - Date & Time
- Example - Methods
- Example - Files
- Example - Directories
- Example - Exceptions
- Example - Data Structure
- Example - Collections
- Example - Networking
- Example - Threading
- Example - Applets
- Example - Simple GUI
- Example - JDBC
- Example - Regular Exp
- Example - Apache PDF Box
- Example - Apache POI PPT
- Example - Apache POI Excel
- Example - Apache POI Word
- Example - OpenCV
- Example - Apache Tika
- Example - iText
- Java Useful Resources
- Java - Quick Guide
- Java - Useful Resources
How to convert an image to Binary format using Java
Problem Description
How to convert an image to Binary format using Java
Solution
Following is the program to convert an image to Binary format using Java.
import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; public class ConvertImageToBinary { public static void main(String args[]) throws Exception { //Loading the OpenCV core library System.loadLibrary( Core.NATIVE_LIBRARY_NAME ); String input = "C:/opencv/logo.jpg"; //Reading the image Mat src = Imgcodecs.imread(input); //Creating the destination matrix Mat dst = new Mat(); //Converting to binary image... Imgproc.threshold(src, dst, 200, 500, Imgproc.THRESH_BINARY); //Writing the image Imgcodecs.imwrite("C:/opencv/imageToBinary.jpg", dst); System.out.println("Converted to binary"); } }
Input

Output

java_opencv
Advertisements