图像增强
图片太亮、太暗等问题。
环境:JAVA+Opencv4
直方图均衡
/**
* 增强对比度
*
* @param src BGR格式图像
* @return
*/
public static Mat histEqualize(Mat src) {
Mat dst = src.clone();
Imgproc.cvtColor(dst, dst, Imgproc.COLOR_BGR2YCrCb);
List<Mat> list1 = new ArrayList<>();
Core.split(dst, list1);
Imgproc.equalizeHist(list1.get(0), list1.get(0));
Core.normalize(list1.get(0), list1.get(0), 0, 255, Core.NORM_MINMAX);
Core.merge(list1, dst);
Imgproc.cvtColor(dst, dst, Imgproc.COLOR_YCrCb2BGR);
return dst;
}
对比度受限直方图均衡化CLAHE
public static Mat autoHistEqualize(Mat src) {
Mat dst = src.clone();
Imgproc.cvtColor(dst, dst, Imgproc.COLOR_BGR2YCrCb);
List<Mat> list1 = new ArrayList<>();
Core.split(dst, list1);
CLAHE clahe = Imgproc.createCLAHE();
clahe.setClipLimit(4);
clahe.apply(list1.get(0), list1.get(0));
// Core.normalize(list1.get(0),list1.get(0),0,255,Core.NORM_MINMAX);
Core.merge(list1, dst);
Imgproc.cvtColor(dst, dst, Imgproc.COLOR_YCrCb2BGR);
return dst;
}
拉普拉斯算子增强
/**
* 用于整体偏暗图像的增强,变亮
*
* @param src
* @return
*/
public static Mat laplaceEnhance(Mat src) {
Mat srcClone = src.clone();
float[] kernel = {0, 0, 0, -1, 5f, -1, 0, 0, 0};
Mat kernelMat = new Mat(3, 3, CvType.CV_32FC1);
kernelMat.put(0, 0, kernel);
Imgproc.filter2D(srcClone, srcClone, CvType.CV_8UC3, kernelMat);
return srcClone;
}
对数变换
变换使得暗部映射到大多数的范围,强调图像低灰度值的部分。
/**
* 对数变换可以将图像的低灰度值部分扩展,显示出低灰度部分更多的细节,
* 将其高灰度值部分压缩,减少高灰度值部分的细节,从而达到强调图像低灰度部分的目的。
*
* @param src
* @return
*/
public static Mat logEnhance(Mat src) {
Mat srcClone = src.clone();
Mat imageResult = new Mat(srcClone.size(), CvType.CV_32FC3);
Core.add(srcClone, new Scalar(5, 5, 5), srcClone);
srcClone.convertTo(srcClone, CvType.CV_32F);
Core.log(srcClone, imageResult);
// Core.multiply(imageLog, new Scalar(3,3,3), imageLog);
Core.normalize(imageResult, imageResult, 0, 255, Core.NORM_MINMAX);
Core.convertScaleAbs(imageResult, imageResult);
return imageResult;
}
伽马变换
原理同上,γ大于1可强调高灰度值,γ小于1可强调低灰度值。
public static Mat gammaEnhance(Mat src) {
Mat srcClone = src.clone();
srcClone.convertTo(srcClone, CvType.CV_32F);
Core.pow(srcClone, 4, srcClone);
Core.normalize(srcClone, srcClone, 0, 255, Core.NORM_MINMAX);
Core.convertScaleAbs(srcClone, srcClone);
return srcClone;
}
Test
public class ImageEnhance {
static {
System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
}
public static void main(String[] args) {
Mat dark, light, nopingheng, piandark, src;
src = Imgcodecs.imread("images/1.png");
dark = Imgcodecs.imread("images/dark.jpg");
light = Imgcodecs.imread("images/light.jpg");
nopingheng = Imgcodecs.imread("images/nopingheng.jpg");
piandark = Imgcodecs.imread("images/piandark.jpg");
// Imgcodecs.imwrite("result/1-histEqualize.png", histEqualize(src));
// Imgcodecs.imwrite("result/dark-histEqualize.png", histEqualize(dark));
// Imgcodecs.imwrite("result/light-histEqualize.png", histEqualize(light));
// Imgcodecs.imwrite("result/nopingheng-histEqualize.png", histEqualize(nopingheng));
// Imgcodecs.imwrite("result/piandark-histEqualize.png", histEqualize(piandark));
//
// Imgcodecs.imwrite("result/1-autoHistEqualize.png", autoHistEqualize(src));
// Imgcodecs.imwrite("result/dark-autoHistEqualize.png", autoHistEqualize(dark));
// Imgcodecs.imwrite("result/light-autoHistEqualize.png", autoHistEqualize(light));
// Imgcodecs.imwrite("result/nopingheng-autoHistEqualize.png", autoHistEqualize(nopingheng));
// Imgcodecs.imwrite("result/piandark-autoHistEqualize.png", autoHistEqualize(piandark));
//
// Imgcodecs.imwrite("result/1-laplaceEnhance.png", laplaceEnhance(src));
// Imgcodecs.imwrite("result/dark-laplaceEnhance.png", laplaceEnhance(dark));
// Imgcodecs.imwrite("result/light-laplaceEnhance.png", laplaceEnhance(light));
// Imgcodecs.imwrite("result/nopingheng-laplaceEnhance.png", laplaceEnhance(nopingheng));
// Imgcodecs.imwrite("result/piandark-laplaceEnhance.png", laplaceEnhance(piandark));
//
// Imgcodecs.imwrite("result/1-logEnchane.png", logEnhance(src));
// Imgcodecs.imwrite("result/dark-logEnchane.png", logEnhance(dark));
// Imgcodecs.imwrite("result/light-logEnchane.png", logEnhance(light));
// Imgcodecs.imwrite("result/nopingheng-logEnchane.png", logEnhance(nopingheng));
// Imgcodecs.imwrite("result/piandark-logEnchane.png", logEnhance(piandark));
//
// Imgcodecs.imwrite("result/1-gammaEnhance.png", gammaEnhance(src));
// Imgcodecs.imwrite("result/dark-gammaEnhance.png", gammaEnhance(dark));
// Imgcodecs.imwrite("result/light-gammaEnhance.png", gammaEnhance(light));
// Imgcodecs.imwrite("result/nopingheng-gammaEnhance.png", gammaEnhance(nopingheng));
// Imgcodecs.imwrite("result/piandark-gammaEnhance.png", gammaEnhance(piandark));
}
}