OpenCV JavaFX application to alter the sharpness of an image

JavaObject Oriented ProgrammingProgramming

Sharpening an image is the opposite of blur. To alter the sharpness of an image using the OpenCV library, you need to smooth/blur it using the Gaussian filter and subtract the smoothed version from the original image.

Example

Following is a JavaFX program with two sliders representing the alpha and beta values.

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);
   }
}

Input

Output

raja
Published on 09-Apr-2020 11:38:37
Advertisements