Convert Image to Grayscale Without Using Any Methods in Java OpenCV

Maruthi Krishna
Updated on 09-Apr-2020 07:14:40

506 Views

To convert the colored image to grayscale.Get the red green blue values of each pixelGet the average of these 3 colors.Replace the RGB values with the average.Create a new pixel value from the modified colors.Set the new value to the pixel.Exampleimport java.io.File; import java.io.IOException; import java.awt.Color; import java.awt.image.BufferedImage; import javax.imageio.ImageIO; public class Color2Grey {    public static void main(String args[])throws IOException {       //Reading the image       File file= new File("D:\Images\car.jpg");       BufferedImage img = ImageIO.read(file);       for (int y = 0; y < img.getHeight(); y++) {          for ... Read More

Convert Colored Image to Sepia Image Using Java OpenCV Library

Maruthi Krishna
Updated on 09-Apr-2020 07:11:01

889 Views

Algorithm to convert a colored image to sepia −Get the red green blue values of each pixelGet the average of these 3 colors.Define the depth and intensity values (ideally 20, and 30).Modify the values as −red = red + (depth*2).Green = green +depth.blue = blue-intensity.Make sure that the modified values are between 0 to 255.Create a new pixel value from the modified colors and set the new value to the pixel.Implementation in JavaRead the required image using ImageIO.read() method.Get the height and width of the image.Using nested for loops traverse through each pixel in the image.Get the pixel value using ... Read More

Convert Negative Image to Positive Image using Java OpenCV Library

Maruthi Krishna
Updated on 09-Apr-2020 07:07:30

551 Views

To convert a negative image to positive −Read the required image using ImageIO.read() method.Get the height and width of the image.Using nested for loops traverse through each pixel in the image.Get the pixel value using the getRGB() method.To retrieve each value from a pixel you need to right shift to the beginning position of each color i.e. 24 for alpha 16 for red etc. and perform bitwise and operation with 0Xff. This masks the variable leaving the last 8 bits and ignoring all the rest of the bits.Calculate the new red, green and blue values by subtracting them from 255.Reconstruct ... Read More

Save Current JShell Session in Java 9

raja
Updated on 08-Apr-2020 16:05:07

800 Views

Java 9 has introduced a new feature is the creation of a REPL (Read-Evaluate-Print-Loop) called JShell. It is a command-line prompt tool to evaluate Java code without the need to write a complete program.When we can enter code or internal commands in JShell, we need to use it during the current session. When we can close JShell and log-in again, all of the code previously entered has lost. An internal command has been implemented in order to save all code entered into a file using the "/save" command./ save [file-path] / save -all [file-path] / save -history [file-path] / save -start ... Read More

Modify RGB Values of an Image Using Java OpenCV Library

Maruthi Krishna
Updated on 08-Apr-2020 14:14:13

4K+ Views

A pixel is the smallest element of a digital image, each pixel contains the values of alpha, red, green, blue values. The pixel value(s) is stored in a 32-bit memory space holding ARGB values (8 bits each) in the same order. Therefore, to modify the color of an image −You need to loop through each pixel in the image.Get the pixel value using the getRGB() method.Retrieve the ARGB values from the pixel value.Perform the required modifications to the RGB values.Create a new pixel value with the modified RGB values.Set the new pixel value(s) using the setRGB() method.Setting the ARGB values ... Read More

Perform Bitwise OR Operation on Two Images Using Java OpenCV

Maruthi Krishna
Updated on 08-Apr-2020 14:12:14

336 Views

You can compute bitwise or between two images using the bitwise_or() method of the org.opencv.core.Core class.This method accepts three Mat objects representing the source, destination and result matrices, calculates the bitwise disjunction of each element in the source matrices and stores the result in the destination matrix.ExampleIn the following Java example, we are converting an image into a binary and grayscale and calculating the bitwise disjunction of the results.import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.highgui.HighGui; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; public class BitwiseORExample {    public static void main(String args[]) throws Exception {       //Loading the OpenCV core library     ... Read More

List All Collections in a MongoDB Database Using Java

Maruthi Krishna
Updated on 08-Apr-2020 14:11:06

3K+ Views

You can print a list of all the existing collections in a database using show collections.ExampleAssume we have created 3 collections in a MongoDB database as shown below −> use sampleDatabase switched to db sampleDatabase > db.createCollection("students") { "ok" : 1 } > db.createCollection("teachers") { "ok" : 1 } > db.createCollection("sample") { "ok" : 1 }Following query lists all the collections in the database −> use sampleDatabase switched to db sampleDatabase > show collections sample students teachersUsing Java programIn Java, you can get the names of all collections in the current database using the listCollectionNames() method of the com.mongodb.client.MongoCollection interface.Therefore ... Read More

Reference to a Constructor Using Method References in Java 8

Maruthi Krishna
Updated on 08-Apr-2020 14:09:28

788 Views

Lambda expressions In Java allows you to pass functionality as an argument to a method. You can also call an existing method using lambda expressions.list.forEach(n -> System.out.println(n));Method references are simple, easy-to-read lambda expressions to call/refer and the existing method by name in a lambda expression. In addition to the instance and static methods, you can also refer a constructor by using the new keyword.SyntaxFollowing is the syntax to reference a constructor in Java.ClassName::newExampleinterface myInterface{    Test greet(String data); } class Test{    Test(String data){          System.out.println(data);    } } public class MethodReferences {    public static void ... Read More

Convert Colored Image to Blue, Green, Red Image Using Java OpenCV

Maruthi Krishna
Updated on 08-Apr-2020 14:06:04

400 Views

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_RGB2BGR as the third parameter to this method.Exampleimport 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 );   ... Read More

Convert Colored Image to Grayscale Using Java OpenCV Library

Maruthi Krishna
Updated on 08-Apr-2020 14:03:11

2K+ Views

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.Exampleimport 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 );   ... Read More

Advertisements