Imgcodecs Class imread() method accepts a string representing the file name as the value of the parameter . This method reads the contents of the specified file into a matrix object and returns it. Using this method you can read the contents of the image.
Besides this, the Imgcodecs class also provides another variant of this method that accepts an integer value representing a flag specifying the desired reading mode.
p>
The following are the various fields of the Imgcodecs class that can be used as flag values.
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.
-
IMREAD_GRAYSCALE - If the flag is set to this value, the loaded image will be converted to a single channel grayscale image.
IMREAD_LOAD_GDAL - If the flag is set to this value, images can be loaded using the GDAL driver.
-
IMREAD_ANYCOLOR - If the flag is set to this value, the image will be read in any possible color format.
li> -
##IMREAD_REDUCED_COLOR_2 or,
IMREAD_REDUCED_COLOR_4 or, IMREAD_REDUCED_COLOR_8- if the flag is set to this value When , the image is read as a three-channel BGR and the size of the image is reduced to 1/2, 1/4 or 1/8 of the original size of the image relative to the field used.
-
IMREAD_UNCHANGED - If the flag is set to this value, the loaded image is returned as-is.
So if you need to read a color image into grayscale, you should pass IMREAD_GRAYSCALE or IMREAD_REDUCED_GRAYSCALE_X as parameter.
##IMREAD_REDUCED_GRAYSCALE_2 or, IMREAD_REDUCED_GRAYSCALE_4 or,
IMREAD_REDUCED_GRAYSCALE_8
- if the flag is set to this value , the image is read as a single-channel grayscale image, and the image size is reduced relative to the field used to 1/2, 1/4, or 1/8 of the original size of the image.
Example 1
import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.imgcodecs.Imgcodecs; public class ReadAsGreyScale { public static void main(String args[]) { //Loading the OpenCV core library System.loadLibrary(Core.NATIVE_LIBRARY_NAME); //Reading the Image from the file Mat matrix = Imgcodecs.imread("D://images//sunset.jpg", Imgcodecs.IMREAD_GRAYSCALE); //Writing the image Imgcodecs.imwrite("D://images//sunset_greyscale.jpg", matrix); System.out.println("Image re-Saved"); } }Enter
Output
##Example 2
import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.imgcodecs.Imgcodecs; public class ReadingAsGrey2 { public static void main(String args[]) { //Loading the OpenCV core library System.loadLibrary(Core.NATIVE_LIBRARY_NAME); String input = "D://images//sunset.jpg"; //Reading the Image from the file Mat matrix = Imgcodecs.imread(input, Imgcodecs.IMREAD_REDUCED_GRAYSCALE_4 ); //Writing the image Imgcodecs.imwrite("D://images//sunset_grey_8.jpg", matrix); System.out.println("Image re-Saved"); } }Output
The above is the detailed content of Read color image into grayscale image using Java OpenCV library. For more information, please follow other related articles on the PHP Chinese website!

JVM'sperformanceiscompetitivewithotherruntimes,offeringabalanceofspeed,safety,andproductivity.1)JVMusesJITcompilationfordynamicoptimizations.2)C offersnativeperformancebutlacksJVM'ssafetyfeatures.3)Pythonisslowerbuteasiertouse.4)JavaScript'sJITisles

JavaachievesplatformindependencethroughtheJavaVirtualMachine(JVM),allowingcodetorunonanyplatformwithaJVM.1)Codeiscompiledintobytecode,notmachine-specificcode.2)BytecodeisinterpretedbytheJVM,enablingcross-platformexecution.3)Developersshouldtestacross

TheJVMisanabstractcomputingmachinecrucialforrunningJavaprogramsduetoitsplatform-independentarchitecture.Itincludes:1)ClassLoaderforloadingclasses,2)RuntimeDataAreafordatastorage,3)ExecutionEnginewithInterpreter,JITCompiler,andGarbageCollectorforbytec

JVMhasacloserelationshipwiththeOSasittranslatesJavabytecodeintomachine-specificinstructions,managesmemory,andhandlesgarbagecollection.ThisrelationshipallowsJavatorunonvariousOSenvironments,butitalsopresentschallengeslikedifferentJVMbehaviorsandOS-spe

Java implementation "write once, run everywhere" is compiled into bytecode and run on a Java virtual machine (JVM). 1) Write Java code and compile it into bytecode. 2) Bytecode runs on any platform with JVM installed. 3) Use Java native interface (JNI) to handle platform-specific functions. Despite challenges such as JVM consistency and the use of platform-specific libraries, WORA greatly improves development efficiency and deployment flexibility.

JavaachievesplatformindependencethroughtheJavaVirtualMachine(JVM),allowingcodetorunondifferentoperatingsystemswithoutmodification.TheJVMcompilesJavacodeintoplatform-independentbytecode,whichittheninterpretsandexecutesonthespecificOS,abstractingawayOS

Javaispowerfulduetoitsplatformindependence,object-orientednature,richstandardlibrary,performancecapabilities,andstrongsecurityfeatures.1)PlatformindependenceallowsapplicationstorunonanydevicesupportingJava.2)Object-orientedprogrammingpromotesmodulara

The top Java functions include: 1) object-oriented programming, supporting polymorphism, improving code flexibility and maintainability; 2) exception handling mechanism, improving code robustness through try-catch-finally blocks; 3) garbage collection, simplifying memory management; 4) generics, enhancing type safety; 5) ambda expressions and functional programming to make the code more concise and expressive; 6) rich standard libraries, providing optimized data structures and algorithms.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version
Visual web development tools

EditPlus Chinese cracked version
Small size, syntax highlighting, does not support code prompt function
