Maruthi Krishna

Maruthi Krishna

500 Articles Published

Articles by Maruthi Krishna

Page 31 of 50

How to compare two images using Java OpenCV library?

Maruthi Krishna
Maruthi Krishna
Updated on 09-Apr-2020 7K+ Views

To compare two images −Read Both of them using the Image.IO.read() method.Get the height and width of both of them to make sure they are equal.Get the pixel values and, get the RGB values of both of the images.Get the sum of the differences between the RGB values of these two images.Calculate the percentage of the difference using the following formula −Average = difference/weight*height*3; Percentage = (Average/255)*100;Exampleimport java.awt.Color; import java.awt.image.BufferedImage; import javax.imageio.ImageIO; import java.io.File; public class ComparingImages {    public static void main(String[] args) throws Exception {       BufferedImage img1 = ImageIO.read(new File("D:\Images\test1.jpg"));       BufferedImage img2 ...

Read More

How to flip an image using Java OpenCV library?

Maruthi Krishna
Maruthi Krishna
Updated on 09-Apr-2020 1K+ Views

The flip() method of the Core class of OpenCV flips an image along the x/y axis. This method accepts −A source matrix congaing the data of the original image.An empty destination matrix to hold the data of the resultant image.A flip code to specify the direction of the image (0 –x axis, +ve – y axis, – ve both the axes).To flip an image −Load the OpenCV core native library, using the loadLibrary() method.Read the contents of the image file to a matrix using the imread() method.Create an empty matric to hold the result.Invoke the flip() method by passing the ...

Read More

How to create a mirror image using Java OpenCV library?

Maruthi Krishna
Maruthi Krishna
Updated on 09-Apr-2020 830 Views

To create a mirror imageRead the required image using ImageIO.read() method.Get the height and width of the image.Create an empty buffered image to store the resultUsing nested for loops traverse through each pixel in the image.Iterate the width of the image from right to left.Get the pixel value using the getRGB() method.Set the pixel values to the result image object using the setRGB() method, by replacing the new width values.Exampleimport java.io.File; import java.io.IOException; import java.awt.image.BufferedImage; import javax.imageio.ImageIO; public class MirrorImage {    public static void main(String args[])throws IOException {       //Reading the image       File file= ...

Read More

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

Maruthi Krishna
Maruthi Krishna
Updated on 09-Apr-2020 948 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

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

Maruthi Krishna
Maruthi Krishna
Updated on 08-Apr-2020 432 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 write an image using Java OpenCV library?

Maruthi Krishna
Maruthi Krishna
Updated on 08-Apr-2020 789 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

Non capturing groups Java regular expressions:

Maruthi Krishna
Maruthi Krishna
Updated on 21-Feb-2020 1K+ Views

Using capturing groups you can treat multiple characters as a single unit. You just need to place the characters to be grouped inside a set of parentheses. For example −(.*)(\d+)(.*)If you are trying to match multiple groups the match results of each group is captured. You can get the results a group by passing its respective group number to the group() method. 1, 2, 3 etc.. (from right to left) group 0 indicates the whole match.Example Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class CapturingGroups {    public static void main( String args[] ) {       System.out.println("Enter input ...

Read More

Java regex program to match parenthesis "(" or, ")".

Maruthi Krishna
Maruthi Krishna
Updated on 21-Feb-2020 6K+ Views

Following regular expression accepts a string with parenthesis −"^.*[\(\)].*$";^ matches the starting of the sentence..* Matches zero or more (any) characters.[\(\)] matching parenthesis.$ indicates the end of the sentence.Example 1 Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class SampleTest {    public static void main( String args[] ) {       String regex = "^.*[\(\)].*$";       //Reading input from user       Scanner sc = new Scanner(System.in);       System.out.println("Enter data: ");       String input = sc.nextLine();       //Instantiating the Pattern class       Pattern pattern = Pattern.compile(regex);     ...

Read More

Regular Expression Q Metacharacter in Java

Maruthi Krishna
Maruthi Krishna
Updated on 21-Feb-2020 1K+ Views

The subexpression/metacharacter "\Q" escapes all characters up to "\E" i.e. you can escape metacharacters in the regular expressions by placing them in between \Q and \E. For example, the expression [aeiou] matches the strings with vowel letters in it.Example  Live Demoimport java.util.Scanner; import java.util.regex.Matcher; import java.util.regex.Pattern; public class SampleProgram {    public static void main( String args[] ) {       String regex = "[aeiou]";       Scanner sc = new Scanner(System.in);       System.out.println("Enter input string: ");       String input = sc.nextLine();       //Creating a Pattern object       Pattern pattern = ...

Read More

Accepting date strings (MM-dd-yyyy format) using Java regex?

Maruthi Krishna
Maruthi Krishna
Updated on 21-Feb-2020 4K+ Views

The following is the regular expression to match the date in the dd-MM-yyyy format.^(1[0-2]|0[1-9])/(3[01]|[12][0-9]|0[1-9])/[0-9]{4}$To match a date in a string in that format.Compile the above expression of the compile() method of the Pattern class.Get the Matcher object bypassing the required input string as a parameter to the matcher() method of the Pattern class.The matches() method of the Matcher class returns true if a match occurs else it returns false. Therefore, invoke this method to validate the data.Example 1import java.util.regex.Matcher; import java.util.regex.Pattern; public class MatchingDate {    public static void main(String[] args) {       String date = "01/12/2019";   ...

Read More
Showing 301–310 of 500 articles
« Prev 1 29 30 31 32 33 50 Next »
Advertisements