
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
How to convert a colored image to grayscale using Java OpenCV library?
The cvtColor() method of the Imgproc class changes/converts the color of the image from one to another. This method accepts three parameters −
src − A Matrix object representing source.
dst − A Matrix object representing the destination.
code − An integer value representing the color of the destination image.
To convert a colored image to grayscale you need to pass Imgproc.COLOR_RGB2GRAY as the third parameter to this method.
Example
import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; public class ColorToGrayscale { public static void main(String args[]) throws Exception { //Loading the OpenCV core library System.loadLibrary( Core.NATIVE_LIBRARY_NAME ); //Reading the image Mat src = Imgcodecs.imread("spiderman.jpg"); //Creating the empty destination matrix Mat dst = new Mat(); //Converting the image to grey scale Imgproc.cvtColor(src, dst, Imgproc.COLOR_RGB2GRAY); //Instantiating the Imagecodecs class Imgcodecs imageCodecs = new Imgcodecs(); //Writing the image imageCodecs.imwrite("colortogreyscale.jpg", dst); System.out.println("Image Saved"); } }
Input
Output
- Related Articles
- How to convert a colored image to Sepia image using Java OpenCV library?
- How to convert HSV to colored image using Java OpenCV library?
- How to convert HLS to colored image using Java OpenCV library?
- How to convert colored image to HLS using Java OpenCV library?
- How to convert a colored image to blue/green/red image using Java OpenCV library?
- How to convert a negative image to positive image using Java OpenCV library?
- Reading a colored image as grey scale using Java OpenCV library.
- How to convert RGB image to HSV using Java OpenCV library?
- How to convert HSV to BGR image using Java OpenCV library?
- How to convert a colored image to HLS in OpenCV using Python?
- OpenCV Python – How to convert a colored image to a binary image?
- How to convert a positive image to Negative to using OpenCV library?
- Converting image to Grayscale without using any methods Java OpenCV.
- How to create a mirror image using Java OpenCV library?
- How to write an image using Java OpenCV library?

Advertisements