使用 OpenCV 降低图像亮度的 JavaFX 示例。
使用 Java 改变图像亮度的一种方法是使用 convertTo() 方法。此方法对给定的矩阵执行必需的计算,以改变图像的对比度和亮度。此方法接受 4 个参数 −
mat − 空矩阵,用于保存结果,其大小和类型与源矩阵相同。
rtype − 整数值,指定输出矩阵的类型。如果该值是负值,则类型将与源相同。
alpha − 增益值,必须大于 0(默认值 1)。
beta − 偏置值(默认值 0)。
如果为参数 beta 选择的值为负值(0 到 -255),则图像亮度会减小。
示例
import java.awt.image.BufferedImage; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import javafx.application.Application; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.embed.swing.SwingFXUtils; import javafx.geometry.Insets; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.control.Slider; import javafx.scene.image.ImageView; import javafx.scene.image.WritableImage; import javafx.scene.layout.VBox; import javafx.stage.Stage; import javax.imageio.ImageIO; import org.opencv.core.Core; import org.opencv.core.Mat; import org.opencv.core.MatOfByte; import org.opencv.imgcodecs.Imgcodecs; public class DecreasingBrightnessJavaFX extends Application { double contrast = 1; private final int rtype = -1; double alpha = 1; double beta = 0; Slider slider1; int sliderMinVal = 0; int sliderMaxVal = 255; int sliderInitVal = 255; Mat src = null; public void start(Stage stage) throws IOException { //Loading the OpenCV core library System.loadLibrary( Core.NATIVE_LIBRARY_NAME ); String file ="D:\Image\cuba.jpg"; src = Imgcodecs.imread(file); WritableImage writableImage = loadImage(src); //Setting the image view ImageView imageView = new ImageView(writableImage); imageView.setX(25); imageView.setY(25); imageView.setFitHeight(400); imageView.setFitWidth(550); imageView.setPreserveRatio(true); //Setting the slider slider1 = new Slider(sliderMinVal, sliderMaxVal, sliderInitVal); slider1.setShowTickLabels(true); slider1.setShowTickMarks(true); slider1.setMajorTickUnit(25); slider1.setBlockIncrement(10); //Setting the label Label label1 = new Label(); Label label2 = new Label(); //Listener for the slider slider1.valueProperty().addListener(new ChangeListener<Number>() { public void changed(ObservableValue <?extends Number>observable, Number oldValue, Number newValue){ try { alpha = newValue.doubleValue(); Mat dest = new Mat(src.rows(), src.cols(), src.type()); alpha = (alpha/sliderMaxVal); beta = 1.0 - alpha; label1.setText("α-value: " + alpha); label2.setText("β-value: " + beta); src.convertTo(dest, rtype, alpha, beta); imageView.setImage(loadImage(dest)); } catch(Exception e) { System.out.println(""); } } }); //VBox to arrange all the three components VBox vbox = new VBox(); vbox.setPadding(new Insets(20)); vbox.setSpacing(10); vbox.getChildren().addAll(label1, label2, slider1, imageView); //Creating a scene object Scene scene = new Scene(vbox, 600, 450); stage.setTitle("Decreasing an image"); stage.setScene(scene); stage.show(); } public WritableImage loadImage(Mat image) throws IOException { MatOfByte matOfByte = new MatOfByte(); Imgcodecs.imencode(".jpg", image, matOfByte); //Storing the encoded Mat in a byte array byte[] byteArray = matOfByte.toArray(); //Displaying the image InputStream in = new ByteArrayInputStream(byteArray); BufferedImage bufImage = ImageIO.read(in); WritableImage writableImage = SwingFXUtils.toFXImage(bufImage, null); return writableImage; } public static void main(String args[]) { launch(args); } }
输入
输出
广告