在本文中, 我们将学习如何使用OpenCv库增强图像的亮度。可以通过将图像的每个像素与alpha值相乘, 然后向其添加beta值来增强图像的亮度。
首先, 我们需要为Java设置OpenCV, 我们建议使用eclipse来实现, 因为它易于使用和设置。有关安装, 请参阅这里
亮度增强所需的方法:
convertTo(目标, rtype, alpha, beta):此方法位于OpenCv的Mat包中。
语法如下:
sourceImage.convertTo(destination, rtype, alpha, beta);
parameters:
destination: is the destination image
rtype: is the desired output matrix type
alpha: is optional scale factor multiplied
to each pixel of source image
beta: it is optional beta value added to the scaled values.
imread()–此方法用于将图像读取为由OpenCV渲染的Mat对象。
语法如下:
Imgcodecs.imread(filename);
parameters:
filename: filename of the image file.If the image is
in another directory whole path of image must be mentioned.
imwrite()–此方法用于将Mat对象写入图像文件。
语法如下:
Imgcodecs.imwrite(filename, mat_img);
parameters:
filename: filename of the image file.If the image is
in another directory whole path of image must be mentioned.
mat_img: resultant mat object.
package ocv;
import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.imgcodecs.Imgcodecs;
public class Main {
static int width;
static int height;
static double alpha = 1 ;
static double beta = 50 ;
public static void main( String[] args ) {
try {
//For proper execution of native libraries
//Core.NATIVE_LIBRARY_NAME must be loaded before
//calling any of the opencv methods
System.loadLibrary( Core.NATIVE_LIBRARY_NAME );
//input image
Mat source =
Imgcodecs.imread( "E://input.jpg" , Imgcodecs.CV_LOAD_IMAGE_COLOR);
Mat destination = new Mat(source.rows(), source.cols(), source.type());
//applying brightness enhacement
source.convertTo(destination, - 1 , alpha, beta);
//output image
Imgcodecs.imwrite( "E://output.jpg" , destination);
} catch (Exception e) {
System.out.println( "error: " + e.getMessage());
}
}
}
注意:该代码无法在在线IDE中使用, 因为它需要硬盘中的图像。
Input.jpg
Alpha = 1, Beta = 50
Output.jpg
Alpha = 2, Beta = 50
Output.jpg
Alpha = 2, Beta = 25
Output.jpg
如果发现任何不正确的地方, 或者想分享有关上述主题的更多信息, 请写评论。
评论前必须登录!
注册