Explain the Mat Class in Java OpenCV Library

Maruthi Krishna
Updated on 09-Apr-2020 09:22:09

2K+ Views

In OpenCV, images are stored in Using Mat object. It is nothing but an n-dimensional array and is used to store image data of grayscale or color images, voxel volumes, vector fields, point clouds, tensors, histograms, etc.If you try to read an image using the OpenCV library it will be read to a Mat object.Mat matrix = Imgcodecs.imread(filePath);You can instantiate this class manually using one of the following constructors −Mat() − A no-arg constructor, used to create an empty matrix and pass this to other OpenCV methods.Mat(int rows, int cols, int type) − This constructor accepts three parameters of integer ... Read More

Create Custom Color Maps in Java Using OpenCV

Maruthi Krishna
Updated on 09-Apr-2020 09:20:38

448 Views

The applyColorMap() method of the Imgproc class applies specified color map to the given image. This method accepts three parameters −Two Mat objects representing the source and destination images.An integer variable representing the type of the color map to be applied.You can pass any of the following as color map value to this method.COLORMAP_AUTUMN, COLORMAP_BONE, COLORMAP_COOL, COLORMAP_HOT, COLORMAP_HSV, COLORMAP_JET, COLORMAP_OCEAN , COLORMAP_PARULA, COLORMAP_PINK, COLORMAP_RAINBOW, COLORMAP_SPRING, COLORMAP_SUMMER, COLORMAP_WINTER.Exampleimport org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; public class CustomColorMaps {    public static void main(String args[]) {       // Loading the OpenCV core library       System.loadLibrary(Core.NATIVE_LIBRARY_NAME);     ... Read More

Alter Brightness of a Grey Scale Image

Maruthi Krishna
Updated on 09-Apr-2020 09:18:12

306 Views

The equalizeHist() method of the Imgproc class accepts a greyscale image and equalizes its histogram, which will, in turn, normalizes the brightness and increases the contrast of the given image. This method accepts two parameters −A Mat object representing the source image (greyscale).A Mat object to save the result.ExampleFollowing Java program reads a colored image as greyscale, saves it, normalizes the brightness and increases the contrast of the given image and saves it.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 HstExample {    public static void main(String args[]) {       //Loading the OpenCV core ... Read More

Add Noise to an Image Using Java OpenCV Library

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

740 Views

To add noise to a given image using OpenCV −Read the contents of the given image to a Mat object.Create two more empty matrices to store the noise and the resultant matrices.Create two MatOfDouble matrices to store mean and standard deviation.Get the mean and standard deviation values using the meanStdDev() method.Create a matrix with random elements (to store the noise) using the randn() method.To this method pass the above-created source, mean and standard deviation objects.Finally, add the noise matrix and source matrix and save as destination.Exampleimport java.awt.Image; import java.awt.image.BufferedImage; import java.io.IOException; import javafx.application.Application; import javafx.embed.swing.SwingFXUtils; import javafx.scene.Group; import javafx.scene.Scene; import ... Read More

OpenCV Java Example to Scale an Image

Maruthi Krishna
Updated on 09-Apr-2020 09:04:53

811 Views

The resize() method of the Imgproc class resizes the specified image. This method accepts −Two Mat objects representing the source and destination images.A Size object representing the size of the output image.A double variable representing the scale factor along the horizontal axis.A double variable representing the scale factor along the vertical axis.An integer variable representing the interpolation method to be used in the operation.Exampleimport java.awt.Image; import java.awt.image.BufferedImage; import java.io.IOException; import javafx.application.Application; import javafx.embed.swing.SwingFXUtils; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.image.ImageView; import javafx.scene.image.WritableImage; import javafx.stage.Stage; import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.Size; import org.opencv.highgui.HighGui; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; public class ScalingAnImage extends Application { ... Read More

Java Image Translation Example Using OpenCV

Maruthi Krishna
Updated on 09-Apr-2020 09:02:08

549 Views

The warpAffine() method of the Imgproc class applies an affine transformation to the specified image. This method accepts −Three Mat objects representing the source, destination, and transformation matrices.An integer value representing the size of the output image.To translate an image Create a translation matrix and pass it as a transformation matrix to this method along with the other parameters.Exampleimport java.awt.Image; import java.awt.image.BufferedImage; import java.io.IOException; import javafx.application.Application; import javafx.embed.swing.SwingFXUtils; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.image.ImageView; import javafx.scene.image.WritableImage; import javafx.stage.Stage; import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.MatOfPoint2f; import org.opencv.core.Point; import org.opencv.core.Size; import org.opencv.highgui.HighGui; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; public class TranslatingAnImage extends Application { ... Read More

Rotate an Image with OpenCV Using Java

Maruthi Krishna
Updated on 09-Apr-2020 08:58:54

707 Views

The warpAffine() method of the Imgproc class applies an affine transformation to the specified image. This method accepts −Three Mat objects representing the source, destination, and transformation matrices.An integer value representing the size of the output image.To rotate an image Create a rotation matrix and pass it as a transformation matrix to this method along with the other parameters.Exampleimport java.awt.Image; import java.awt.image.BufferedImage; import java.io.IOException; import javafx.application.Application; import javafx.embed.swing.SwingFXUtils; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.image.ImageView; import javafx.scene.image.WritableImage; import javafx.stage.Stage; import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.Point; import org.opencv.core.Size; import org.opencv.highgui.HighGui; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; public class RotatingAnImage extends Application {    public ... Read More

Image Dilation in Java using OpenCV

Maruthi Krishna
Updated on 09-Apr-2020 08:55:48

1K+ Views

Erosion and dilation are the two basic morphological operations. As the name implies, morphological operations are the set of operations that process images according to their shapes.During dilation operation additional pixels are added to an image boundary, a total number of pixels added during the dilation process depends on the dimensions of the structuring element used.You can dilate an image using the dilate() method of the Imgproc class, this method three mat objects representing source, destination, and kernel.Exampleimport java.awt.Image; import java.awt.image.BufferedImage; import java.io.IOException; import javafx.application.Application; import javafx.embed.swing.SwingFXUtils; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.image.ImageView; import javafx.scene.image.WritableImage; import javafx.stage.Stage; import org.opencv.core.Core; import ... Read More

Image Erosion Example in Java using OpenCV

Maruthi Krishna
Updated on 09-Apr-2020 08:52:53

1K+ Views

Erosion and dilation are the two basic morphological operations. As the name implies, morphological operations are the set of operations that process images according to their shapes.During erosion operation, additional pixels are removed from image boundaries, total number of pixels removed during the erosion process depends on the dimensions of the structuring element used.You can perform erosion operation on an image using the erode() method of the Imgproc class, this method three mat objects representing source, destination, and kernel.Exampleimport java.awt.Image; import java.awt.image.BufferedImage; import java.io.IOException; import javafx.application.Application; import javafx.embed.swing.SwingFXUtils; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.image.ImageView; import javafx.scene.image.WritableImage; import javafx.stage.Stage; import org.opencv.core.Core; ... Read More

Implement Median Blur in OpenCV Using Java

Maruthi Krishna
Updated on 09-Apr-2020 08:49:06

584 Views

You can blur an image by filtering it using a low-pass filter, this removes high frequency content (noise, edges) from an image.Median Blurring is one of the blurring techniques provided by OpenCV, it is highly efficient in removing salt and pepper noise of an image. This replaces the central element with the median of all the pixels in the kernel area.You can filter/blur an image by this technique using the medianBlur() method, this method acceptsTwo Mat objects representing the source and destination images.A Size object representing the size of the kernel.Exampleimport java.awt.Image; import java.awt.image.BufferedImage; import java.io.IOException; import org.opencv.core.Core; import org.opencv.core.Mat; ... Read More

Advertisements