JavaFX example to decrease the brightness of an image using OpenCV.

JavaObject Oriented ProgrammingProgramming

One way to alter the brightness of an image using Java is to use the convertTo() method. This method performs the required calculations on the given matrix to alter the contrast and brightness of an image. This method accepts 4 parameters −

  • mat − Empty matrix to hold the result with the same size and type as the source matrix.

  • rtype − integer value specifying the type of the output matrix. If this value is negative, the type will be same as the source.

  • alpha − Gain value, which must be greater than 0 (default value 1).

  • beta − Bias value (default value 0).

if the chosen value for the parameter beta is a negative value (0 to -255) the brightness of the image is reduced.

Example

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 DecreasingBrightnessJavaFX extends Application {
   double contrast = 1;
   private final int rtype = -1;
   double alpha = 1;
   double beta = 0;
   Slider slider1;
   int sliderMinVal = 0;
   int sliderMaxVal = 255;
   int sliderInitVal = 255;
   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\\cuba.jpg";
      src = Imgcodecs.imread(file);
      WritableImage writableImage = loadImage(src);
      //Setting the image view
      ImageView imageView = new ImageView(writableImage);
      imageView.setX(25);
      imageView.setY(25);
      imageView.setFitHeight(400);
      imageView.setFitWidth(550);
      imageView.setPreserveRatio(true);
      //Setting the slider
      slider1 = new Slider(sliderMinVal, sliderMaxVal, sliderInitVal);
      slider1.setShowTickLabels(true);
      slider1.setShowTickMarks(true);
      slider1.setMajorTickUnit(25);
      slider1.setBlockIncrement(10);
      //Setting the label
      Label label1 = new Label();
      Label label2 = new Label();
      //Listener for the slider
      slider1.valueProperty().addListener(new ChangeListener<Number>() {
         public void changed(ObservableValue <?extends Number>observable, Number
         oldValue, Number newValue){
            try {
               alpha = newValue.doubleValue();
               Mat dest = new Mat(src.rows(), src.cols(), src.type());
               alpha = (alpha/sliderMaxVal);
               beta = 1.0 - alpha;
               label1.setText("α-value: " + alpha);
               label2.setText("β-value: " + beta);
               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, label2, slider1, imageView);
      //Creating a scene object
      Scene scene = new Scene(vbox, 600, 450);
      stage.setTitle("Decreasing 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);
      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:28:21
Advertisements