이미지를 선명하게 하는 것은 흐림의 반대입니다. OpenCV 라이브러리를 사용하여 이미지의 선명도를 변경하려면 가우시안 필터를 사용하여 매끄럽게/흐리게 처리하고 원본 이미지에서 부드러운 버전을 빼야 합니다.
예
다음은 알파 및 베타 값을 나타내는 두 개의 슬라이더가 있는 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.core.Size; import org.opencv.imgcodecs.Imgcodecs; import org.opencv.imgproc.Imgproc; public class SharpnessJavaFX 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(-2, 2, 0); slider2.setShowTickLabels(true); slider2.setShowTickMarks(true); slider2.setMajorTickUnit(1); slider2.setBlockIncrement(.5); //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()); Imgproc.GaussianBlur(src, dest, new Size(0,0), 10); Core.addWeighted(src, alpha, dest, beta, 0, dest); 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()); Imgproc.GaussianBlur(src, dest, new Size(0,0), 10); Core.addWeighted(src, alpha, dest, beta, 0, dest); 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, 560); stage.setTitle("Altering the sharpness"); 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); } }
입력
출력