使用 JavaFX 和 OpenCV 更改图像的亮度和对比度
org.opencv.core.Mat 类中的 convertTo() 方法接受 4 个参数,即:mat(空矩阵)、rtype(整数)、alpha(整数)、beta(整数),顺序相同。
增加亮度 − 您需要将 beta 值从 0 减小到 -255(保持 alpha 值为 1)。
减少亮度 − 您需要将 beta 值从 0 增加到 255(保持 alpha 值为 1)。
增加对比度 − 您需要将 alpha 值从 1 增加到 100(保持 beta 值为 0)。
降低对比度 − 您需要将 alpha 值从 1 减小到 0(保持 beta 值为 0)。
示例
以下是使用两个滑动条更改 alpha beta 值的 JavaFX 程序。
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 AlphaBetaValuesJavaFX extends Application { double contrast = 1; private final int rtype = -1; double alpha = 1; double beta = 0; Slider slider1; Slider slider2; 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\lamma2.jpg"; src = Imgcodecs.imread(file); WritableImage writableImage = loadImage(src); //Setting the image view ImageView imageView = new ImageView(writableImage); imageView.setX(50); imageView.setY(25); imageView.setFitHeight(400); imageView.setFitWidth(550); imageView.setPreserveRatio(true); Label label1 = new Label("Alpha value"); //Setting the slider slider1 = new Slider(0.1, 3, 1); slider1.setShowTickLabels(true); slider1.setShowTickMarks(true); slider1.setMajorTickUnit(1); slider1.setBlockIncrement(0.05); //Setting the label Label label2 = new Label(); Label label3 = new Label("Beta value"); //Setting the slider slider2 = new Slider(-100, 100, 0); slider2.setShowTickLabels(true); slider2.setShowTickMarks(true); slider2.setMajorTickUnit(25); slider2.setBlockIncrement(10); //Setting the label Label label4 = new Label(); //Listener for the slider slider1.valueProperty().addListener(new ChangeListener<Number>() { public void changed(ObservableValue <?extends Number>observable, Number oldValue, Number newValue){ try { label2.setText("α-value: " + newValue); alpha = newValue.doubleValue(); Mat dest = new Mat(src.rows(), src.cols(), src.type()); src.convertTo(dest, rtype, alpha, beta); imageView.setImage(loadImage(dest)); } catch(Exception e) { System.out.println(""); } } }); slider2.valueProperty().addListener(new ChangeListener<Number>() { public void changed(ObservableValue <?extends Number>observable, Number oldValue, Number newValue){ try { label4.setText("β-value: " + newValue); beta = newValue.doubleValue(); Mat dest = new Mat(src.rows(), src.cols(), src.type()); 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, slider1, label2, imageView, label3, slider2, label4); //Creating a scene object Scene scene = new Scene(vbox, 600, 580); stage.setTitle("Loading 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); System.out.println("Image Loaded"); WritableImage writableImage = SwingFXUtils.toFXImage(bufImage, null); return writableImage; } public static void main(String args[]) { launch(args); } }
输入图像
输出
广告