Found 2616 Articles for Java

How to convert a negative image to positive image using Java OpenCV library?

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

415 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

How to convert a positive image to Negative to using OpenCV library?

Maruthi Krishna
Updated on 09-Apr-2020 13:59:35

252 Views

Algorithm to convert an image to negativeGet the red green blue values of each pixelSubtract each color value from 255 and save them as new color values.Create a new pixel value from the modified colors.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 the getRGB() method.Create a Color object bypassing the above-retrieved pixel value as parameter.Get the red, green, blue values from the color object using the getRed(), getGreen() and getBlue() methods respectively.Calculate ... Read More

How to save the current JShell session in Java 9?

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

527 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

How to Set/modify the pixels(RGB values) of an image using Java OpenCV Library?

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

3K+ 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

How to perform Bitwise OR operation on two images using Java OpenCV?

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

254 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

How to list all the Collections in a MongoDB database using Java?

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

2K+ 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

How to convert a colored image to blue/green/red image using Java OpenCV library?

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

314 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

How to convert a colored image to grayscale using Java OpenCV library?

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

1K+ 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

Reading a colored image as grey scale using Java OpenCV library.

Maruthi Krishna
Updated on 08-Apr-2020 14:00:57

219 Views

The imread() method of the Imgcodecs class accepts a string value representing a file name as a 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 an image.In addition to this, the Imgcodecs class also provides another variant of this method which accepts an integer value representing a flag specifying the required reading mode.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 ... Read More

How to write an image using Java OpenCV library?

Maruthi Krishna
Updated on 08-Apr-2020 13:53:05

542 Views

Using the OpenCV library you can perform image processing operations such as image filtering, geometrical image transformations, color space conversion, histograms, etc.Writing an imageWhenever you read the contents of an image using the imread() method of the Imgcodecs class the result is read into the Matrix object.You can write/save an image using the imwrite() method. This accepts two parameters namely −File − A string value representing the file path to which the result should be stored.Img − A matrix object containing the data of the image to be saved.ExampleThe following Java example reads the contents of the image cat.jpg as a ... Read More

Advertisements