convertTo() org.opencv.core.Mat 메소드 클래스는 같은 순서로 mat(빈 행렬), rtype(integer), alpha(integer), beta(integer)의 4가지 매개변수를 허용합니다.
-
밝기를 높이려면 − 베타 값을 0에서 -255로 줄여야 합니다(알파 값 1 유지).
-
밝기를 낮추려면 − 베타 값을 0에서 255로 늘려야 합니다(알파 값 1 유지).
-
대비를 높이려면 − 알파 값을 1에서 100으로 늘려야 합니다(베타 값 0 유지).
-
대비를 줄이려면 − 알파 값을 1에서 0으로 줄여야 합니다(베타 값 0 유지).
예시
다음은 알파 베타 값을 변경하는 두 개의 슬라이드 막대가 있는 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); } }
이미지 입력
출력