Add noise to a given image using OpenCV -
Read the contents of the given image into a Mat object.
Create two empty matrices to store the noise and results
Create two MatOfDouble matrices to store the mean and standard deviation.
Use the meanStdDev() function to obtain the mean and standard deviation values Use the randn() method to create a matrix with random elements (used to store noise).
Pass the source, mean and standard deviation created above to this method objects.
Finally, the noise matrix and the source matrix are added and saved as the target matrix.
Example
import 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.MatOfDouble; import org.opencv.highgui.HighGui; import org.opencv.imgcodecs.Imgcodecs; public class AddingNoise extends Application { public void start(Stage stage) throws IOException { //Loading the OpenCV core library System.loadLibrary(Core.NATIVE_LIBRARY_NAME); //Reading the Image from the file String file ="D://images//elephant.jpg"; Mat src = Imgcodecs.imread(file); System.out.println("Image Loaded"); //Creating destination matrix Mat dst = new Mat(src.rows(), src.cols(), src.type()); //Creating a matrix for the noise Mat noise = new Mat(src.rows(), src.cols(), src.type()); //Calculating the mean and standard deviation MatOfDouble mean = new MatOfDouble(); MatOfDouble dev = new MatOfDouble(); Core.meanStdDev(src, mean, dev); //Filling the noise matrix Core.randn(noise, mean.get(0,0)[0], dev.get(0,0)[0]); //Adding noise to the destination Core.add(src, noise, dst); //Converting matrix to JavaFX writable image Image img = HighGui.toBufferedImage(dst); WritableImage writableImage= SwingFXUtils.toFXImage((BufferedImage) img, null); //Setting the image view ImageView imageView = new ImageView(writableImage); imageView.setX(10); imageView.setY(10); imageView.setFitWidth(575); imageView.setPreserveRatio(true); //Setting the Scene object Group root = new Group(imageView); Scene scene = new Scene(root, 595, 400); stage.setTitle("Adding Noise Example"); stage.setScene(scene); stage.show(); } public static void main(String args[]) { launch(args); } }
Input image
Output
After executing the above program, the following output will be generated−
The above is the detailed content of How to add noise to image using Java OpenCV library?. For more information, please follow other related articles on the PHP Chinese website!

When using MyBatis-Plus or tk.mybatis...

How to query personnel data through natural language processing? In modern data processing, how to efficiently query personnel data is a common and important requirement. ...

In processing next-auth generated JWT...

In IntelliJ...

Discussion on the reasons why JavaScript cannot obtain user computer hardware information In daily programming, many developers will be curious about why JavaScript cannot be directly obtained...

RuoYi framework circular dependency problem troubleshooting and solving the problem of circular dependency when using RuoYi framework for development, we often encounter circular dependency problems, which often leads to the program...

About SpringCloudAlibaba microservices modular development using SpringCloud...

Questions about a curve integral This article will answer a curve integral question. The questioner had a question about the standard answer to a sample question...


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

SublimeText3 Linux new version
SublimeText3 Linux latest version

Dreamweaver Mac version
Visual web development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment

SecLists
SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

SublimeText3 Mac version
God-level code editing software (SublimeText3)