Altering the brightness and contrast of an image using JavaFX and OpenCV

JavaObject Oriented ProgrammingProgramming

The convertTo() method of the org.opencv.core.Mat class accepts 4 parameters namely: mat(empty matrix), rtype(integer), alpha(integer), beta(integer), in the same order.

  • To increase the brightness − You need to reduce the beta value from 0 towards -255(keeping alpha value 1).

  • To decrease the brightness − You need to increase the beta value from 0 towards 255(keeping alpha value 1).

  • To increase the contrast − You need to increase the alpha value from 1 towards 100(keeping beta value 0).

  • To decrease the contrast − You need to decrease the alpha value from 1 to 0 (keeping beta value 0).

Example

Following is a JavaFX program with two slide bars to alter the alpha 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.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);
   }
}

Input image 

Output

raja
Published on 09-Apr-2020 08:03:41
Advertisements