📜  Java DIP-增强图像亮度

📅  最后修改于: 2020-12-14 05:37:03             🧑  作者: Mango


在本章中,我们通过将图像的每个像素与一个alpha值相乘并向其添加另一个beta值来增强图像的亮度。

我们的OpenCV函数convertTo会自动执行上述操作。可以在Mat包下找到。其语法如下-

int alpha = 2;
int beta = 50;
sourceImage.convertTo(destination, rtype , alpha, beta);         

参数说明如下-

Sr.No. Parameter & Description
1

destination

It is destination image.

2

rtype

It is desired output matrix type or, rather the depth, since the number of channels are the same as the input has. if rtype is negative, the output matrix will have the same type as the input.

3

alpha

It is optional scale factor.

4

beta

It is optional delta added to the scaled values.

除了convertTo方法,Mat类还提供了其他方法。他们简要描述-

Sr.No. Method & Description
1

adjustROI(int dtop, int dbottom, int dleft, int dright)

It adjusts a submatrix size and position within the parent matrix.

2

copyTo(Mat m)

It copies the matrix to another one.

3

diag()

It extracts a diagonal from a matrix, or creates a diagonal matrix.

4

dot(Mat m)

It computes a dot-product of two vectors.

5

reshape(int cn)

It changes the shape and/or the number of channels of a 2D matrix without copying the data.

6

submat(Range rowRange, Range colRange)

It extracts a rectangular sub matrix.

以下示例演示了如何使用Mat类来增强图像的亮度-

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.highgui.Highgui;

public class Main {
   static int width;
   static int height;
   static double alpha = 2;
   static double beta = 50;
   
   public static void main( String[] args ) {
   
      try{
         System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
         Mat source =  Highgui.imread("digital_image_processing.jpg",Highgui.CV_LOAD_IMAGE_COLOR);
         Mat destination = new Mat(source.rows(),source.cols(),
         
         source.type());
         source.convertTo(destination, -1, alpha, beta);
         Highgui.imwrite("brightWithAlpha2Beta50.jpg", destination);
         
      } catch (Exception e) {
         System.out.println("error:" + e.getMessage());
      }
   }
}

输出

当您执行给定的代码时,将看到以下输出-

原始图片

增强图像亮度教程

增强的明亮图像(Alpha = 1和Beta = 50)

增强图像亮度教程

增强的明亮图像(Alpha = 2和Beta = 50)

增强图像亮度教程